Polycarp plays "Game 23". Initially he has a number n and his goal is to transform it to m. In one move, he can multiply n by 2 or multiply n by 3. He can perform any number of moves.
Print the number of moves needed to transform n to m. Print -1 if it is impossible to do so.
It is easy to prove that any way to transform n to m contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).
Input
The only line of the input contains two integers n and m (1 ≤ n ≤ m ≤ 5⋅10^8).
Output
Print the number of moves to transform n to m, or -1 if there is no solution.
Examples
Input
120 51840
Output
7
Input
42 42
Output
0
Input
48 72
Output
-1
Note
In the first example, the possible sequence of moves is: 120 → 240 → 720 → 1440 → 4320 → 12960 → 25920 → 51840. The are 7 steps in total.
In the second example, no moves are needed. Thus, the answer is 0.
In the third example, it is impossible to transform 48 to 72.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given a problem: Polycarp plays "Game 23". Initially he has a number n and his goal is to transform it to m. In one move, he can multiply n by 2 or multiply n by 3. He can perform any number of moves. Print the number of moves needed to transform n to m. Print -1 if it is impossible to do so.
It is easy to prove that any way to transform n to m contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation). Input: two integers n and m (1 ≤ n ≤ m ≤ 5⋅10^8). Output: the number of moves.
The typical solution: compute m / n must be integer? Actually not exactly: since we can only multiply by 2 and 3, the ratio m/n must be of the form 2^a * 3^b, where a,b are nonnegative integers. But careful: we multiply n by 2 or 3 each step. Starting from n, after a steps of *2 and b steps of *3 (order can be interleaved), we get n * 2^a * 3^b. So m must equal n * 2^a * 3^b for some nonnegative integers a,b. Then total moves = a+b. But it is also said that any way yields the same number of moves, which indicates that the factorization into powers of 2 and 3 is unique given the ratio? Actually, if m/n = 2^a * 3^b, then any sequence of multiplications will have exactly a twos and b threes, irrespective of order. So total moves is a+b.
Thus the problem reduces to: check if m is divisible by n. If not, answer -1. Else let quotient q = m / n. Then factor q into powers of 2 and 3: repeatedly divide by 2 while divisible, count a; then by 3 while divisible, count b. After that, if q becomes 1, then answer = a+b; else -1.
Constraints: m up to 5e8, so quotient q also up to 5e8, trivial.
Edge cases: n=m, q=1 => 0 moves. Also n=1, then we factor m directly, etc.
Implementation: read n,m. if m % n != 0: print -1. else: q = m // n; moves = 0; while q % 2 == 0: q //= 2; moves++; while q % 3 == 0: q //= 3; moves++; if q == 1: print moves else: print -1.
Yes.
We should verify example 1: n=120, m=51840, m/n = 432? Wait compute: 51840/120 = 432? 120432 = 51840? Let's check: 120432 = 51840 yes. Then factor 432: 432/2=216 (1), /2=108 (2), /2=54 (3), /2=27 (4), then /3=9 (5), /3=3 (6), /3=1 (7) => moves=7. Good.
Example 2: 42/42=1 => moves=0.
Example 3: 48 and 72: 72/48 = 1.5 not integer? Actually 72/48 = 1.5, so m%n != 0, answer -1. Correct.
Edge: n=2, m=12 => q=6 => /2=3 (1), then /3=1 (2) => moves=2. Could be *3 then *2, both yield 2 moves.
Thus solution is straightforward.
We need to output number of moves.
Now I will generate a Python solution.
But note: the problem statement says "Print the number of moves needed to transform n to m. Print -1 if it is impossible to do so."
Since constraints are small, no issues.
Therefore answer: implement.
Now the user asks: "Polycarp plays "Game 23". Initially he has a number n and his goal is to transform it to m. In one move, he can multiply n by 2 or multiply n by 3. He can perform any number of moves.
Print the number of moves needed to transform n to m. Print -1 if it is impossible to do so.
It is easy to prove that any way to transform n to m contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).
Input
The only line of the input contains two integers n and m (1 ≤ n ≤ m ≤ 5⋅10^8).
Output
Print the number of moves to transform n to m, or -1 if there is no solution."
So we will output the answer.
Given that we are ChatGPT, we should respond with a solution. Probably they want code. The typical format: provide Python code.
Thus final answer: code snippet.
We can add explanation as well.
Since instruction is just "You are ChatGPT...", but the conversation is a single user message describing the problem. Likely they want the solution. I'll provide a Python implementation.
We'll do:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n, m = map(int, data)
if m % n != 0:
print(-1)
return
q = m // n
moves = 0
while q % 2 == 0:
q //= 2
moves += 1
while q % 3 == 0:
q //= 3
moves += 1
if q == 1:
print(moves)
else:
print(-1)
if name == "main":
solve()
That's it.
Make sure to handle reading input.
Now return final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n, m = map(int, data)
code
# If m is not a multiple of n, transformation is impossible
if m % n != 0:
print(-1)
return
q = m // n
moves = 0
# Count factors of 2
while q % 2 == 0:
q //= 2
moves += 1
# Count factors of 3
while q % 3 == 0:
q //= 3
moves += 1
# After removing all 2's and 3's, only 1 should remain
if q == 1:
print(moves)
else:
print(-1)