3606.Rotatable Number

Time Limit: 1s Memory Limit: 256MB

Bike is a smart boy who loves math very much. He invented a number called "Rotatable Number" inspired by 142857.

As you can see, 142857 is a magic number because any of its rotatings can be got by multiplying that number by 1,2,...,6 (numbers from one to number's length). Rotating a number means putting its last several digit into first. For example, by rotating number 12345 you can obtain any numbers: 12345,51234,45123,34512,23451. It's worth mentioning that leading-zeroes are allowed. So both 4500123 and 0123450 can be obtained by rotating 0012345. You can see why 142857 satisfies the condition. All of the 6 equations are under base 10.
142857 \cdot 1=142857; 142857 \cdot 2=285714; 142857 \cdot 3=428571; 142857 \cdot 4=571428; 142857 \cdot 5=714285; 142857 \cdot 6=857142.
Now, Bike has a problem. He extends "Rotatable Number" under any base b. As is mentioned above, 142857 is a "Rotatable Number" under base 10. Another example is 0011 under base 2. All of the 4 equations are under base 2.
0011 \cdot 1=0011; 0011 \cdot 10=0110; 0011 \cdot 11=1001; 0011 \cdot 100=1100.
So, he wants to find the largest b (1 \lt b \lt x) so that there is a positive "Rotatable Number" (leading-zeroes allowed) of length n under base b.

Note that any time you multiply a rotatable number by numbers from 1 to its length you should get a rotating of that number.

Input Format(From the terminal/stdin)

The only line contains two space-separated integers n,x (1 \le n \le 5 \cdot 106,2 \le x \le 109).

Output Format(To the terminal/stdout)

Print a single integer - the largest b you found. If no such b exists, print -1 instead.

Sample Input 1

Copy
6 11
 ·  \n

Sample Output 1

Copy
10
  \n

Sample Input 2

Copy
5 8
 · \n

Sample Output 2

Copy
-1
  \n

Submit

请先 登录

© 2025 FAQs