#abc235d. [abc235_d]Multiply and Rotate
[abc235_d]Multiply and Rotate
Problem Statement
We have a positive integer . Additionally, there is a blackboard with a number written in base .
Let be the number on the blackboard. Takahashi can do the operations below to change this number.
- Erase and write multiplied by , in base .
- See as a string and move the rightmost digit to the beginning.
This operation can only be done when and is not divisible by .
For example, when , Takahashi can do one of the following.
- Erase and write .
- See as a string and move the rightmost digit
3
of123
to the beginning, changing the number from to .
The number on the blackboard is initially . What is the minimum number of operations needed to change the number on the blackboard to ? If there is no way to change the number to , print .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3 72
Sample Output 1
4
We can change the number on the blackboard from to in four operations, as follows.
- Do the operation of the first type: .
- Do the operation of the first type: .
- Do the operation of the first type: .
- Do the operation of the second type: .
It is impossible to reach in three or fewer operations, so the answer is .
Sample Input 2
2 5
Sample Output 2
-1
It is impossible to change the number on the blackboard to .
Sample Input 3
2 611
Sample Output 3
12
There is a way to change the number on the blackboard to in operations: $1 \\to 2 \\to 4 \\to 8 \\to 16 \\to 32 \\to 64 \\to 46 \\to 92 \\to 29 \\to 58 \\to 116 \\to 611$, which is the minimum possible.
Sample Input 4
2 767090
Sample Output 4
111