atcoder#ABC235D. [ABC235D] Multiply and Rotate

[ABC235D] Multiply and Rotate

Score : 400400 points

Problem Statement

We have a positive integer aa. Additionally, there is a blackboard with a number written in base 1010. Let xx be the number on the blackboard. Takahashi can do the operations below to change this number.

  • Erase xx and write xx multiplied by aa, in base 1010.
  • See xx as a string and move the rightmost digit to the beginning. This operation can only be done when x10x \geq 10 and xx is not divisible by 1010.

For example, when a=2,x=123a = 2, x = 123, Takahashi can do one of the following.

  • Erase xx and write x×a=123×2=246x \times a = 123 \times 2 = 246.
  • See xx as a string and move the rightmost digit 3 of 123 to the beginning, changing the number from 123123 to 312312.

The number on the blackboard is initially 11. What is the minimum number of operations needed to change the number on the blackboard to NN? If there is no way to change the number to NN, print 1-1.

Constraints

  • 2a<1062 \leq a \lt 10^6
  • 2N<1062 \leq N \lt 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

aa NN

Output

Print the answer.

3 72
4

We can change the number on the blackboard from 11 to 7272 in four operations, as follows.

  • Do the operation of the first type: 131 \to 3.
  • Do the operation of the first type: 393 \to 9.
  • Do the operation of the first type: 9279 \to 27.
  • Do the operation of the second type: 277227 \to 72.

It is impossible to reach 7272 in three or fewer operations, so the answer is 44.

2 5
-1

It is impossible to change the number on the blackboard to 55.

2 611
12

There is a way to change the number on the blackboard to 611611 in 1212 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.

2 767090
111