Quadratic residues modulo a prime number

Rust Medium math

Task «Quadratic residues modulo a prime number»

Given a prime number p (p > 2) and an integer a (0 ≤ a < p). It is necessary to determine whether a is a quadratic residue modulo p, that is, whether there exists an integer x such that x^2 ≡ a (mod p). If a is a quadratic residue, output any such x (0 ≤ x < p). If not, output -1. Use the Legendre symbol and the Tonelli-Shanks algorithm or brute force considering the constraints.

Input Format

The first line contains two integers: p (prime, 3 ≤ p ≤ 10^9) and a (0 ≤ a < p).

Output Format

Output a single integer: x (0 ≤ x < p) such that x^2 ≡ a (mod p), or -1 if there is no solution.

Examples

Example 1

INPUT
7 2
OUTPUT
4

Example 2

INPUT
7 apple banana apple cherry banana apple date
OUTPUT
-1

Example 3

INPUT
7 3
OUTPUT
-1

Example 4

INPUT
4 aa a aaa a
OUTPUT
-1

Example 5

INPUT
13 4
OUTPUT
11
main.rs