Page not loading? Try clicking here.
Placeholder

#2567

Cycle 1s 128MB

Problems

Given two natural numbers N and P, let us output the numbers produced by the following process in order.

The first number to be output is N, and the numbers output from the second one onward are obtained by repeatedly multiplying by N and taking the remainder when divided by P.

That is, first multiply N by N, and output the remainder when this number is divided by P as the second number.
Next, multiply this remainder by N and output the remainder when divided by P.
Next, multiply this remainder by N and output the remainder after dividing by P.
If we continue this process, the numbers being output will contain a repeating part.

For example, consider the case N = 67, P = 31.
The first output number is 67, and the second number is the remainder of 67×67 = 4489 divided by 31, which is 25.
Next, the remainder of 25×67 = 1675 divided by 31 is 1,
and next, the remainder of 1×67 = 67 divided by 31 is 5.
After that, the remainder of 5×67 = 335 divided by 31 is 25, which is a number already output previously.

If we illustrate this process, it looks as follows.

Thus, if we repeat this process, except for the initial 67, the three numbers 25, 1, and 5 will repeat infinitely.

As another example, if we start with N = 9, P = 3,
then 9×9 = 81 and the remainder when divided by 3 is 0,
and 0×9 = 0 and the remainder when divided by 3 is also 0,
so except for the initial 9, the value 0 will repeat infinitely.

Given N and P, write a program that performs the above-defined operation
and outputs the number of distinct numbers contained in the repeating part.


Input

The first line contains N and P, separated by a space. (1 ≤ N ≤ 1,000, 2 ≤ P ≤ 97)


Output

Output the number of distinct numbers contained in the repeating part.


Example #1

67 31
3

Example #2

96 61
60


Source

KOI 본선 2012 초2

You must sign in to write code.