You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder).
Your task is to find the minimum number of moves needed to obtain 1 from n or determine if it's impossible to do that.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 2 ⋅ 10^4) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (1 ≤ n ≤ 10^9).
Output
For each test case, print the answer — the minimum number of moves needed to obtain 1 from n if it's possible to do that or -1 if it's impossible to obtain 1 from n.
Example
Input
7
1
2
3
12
12345
15116544
387420489
Output
0
-1
2
-1
-1
12
36
Note
Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer 15116544:
Divide by 6 and get 2519424;
divide by 6 and get 419904;
divide by 6 and get 69984;
divide by 6 and get 11664;
multiply by 2 and get 23328;
divide by 6 and get 3888;
divide by 6 and get 648;
divide by 6 and get 108;
multiply by 2 and get 216;
divide by 6 and get 36;
divide by 6 and get 6;
divide by 6 and get 1.
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 need to determine the minimal number of moves to reduce n to 1 using two operations:
Multiply by 2.
Divide by 6 (only allowed if divisible by 6).
We can think of this as moving from n to 1. Since operations are reversible? Actually we can also think from 1 to n? But we have constraints: multiply by 2 or divide by 6 from n. The goal is to reach 1. Since we can divide by 6 when divisible, that reduces the number; multiplying by 2 increases it. Typically, we want to decrease the number, so we want to eventually be able to keep dividing by 6. But if n has factors that are not 2 and 3 (since 6=2*3), we need to check. Because dividing by 6 requires that the number is divisible by both 2 and 3. Multiply by 2 adds a factor of 2. So the net effect: we want to get rid of all prime factors except maybe balancing 2s and 3s to allow division by 6.
Let’s analyze prime factorization. Suppose n = 2^a * 3^b * k, where k is integer not divisible by 2 or 3 (i.e., gcd(k,6)=1). For us to ever reach 1, we need to eventually eliminate k as well. But multiplying by 2 only adds 2s, dividing by 6 removes one factor 2 and one factor 3. So the set of prime factors other than 2 and 3 cannot be eliminated. Because neither operation can remove factors other than 2 and 3. Dividing by 6 removes a factor 2 and a factor 3, but doesn't affect other primes. Multiplying by 2 adds a factor 2 but doesn't affect other primes. So if n has any prime factor other than 2 or 3, we can never reduce to 1 because those primes remain unchanged. So condition for possibility: n must be of the form 2^a * 3^b, i.e., n's prime factors are only 2 and 3. In other words, n is a 3-smooth number (also 5? actually 2,3-smooth). So check if n is divisible by any prime >3? For large n up to 1e9, we can check by while dividing by 2 and 3, see if remaining number is 1. If not, answer -1.
Now, if n = 2^a * 3^b. We can perform moves: divide by 6 reduces a by 1 and b by 1 simultaneously (provided both exponents >=1). Multiply by 2 increases a by 1.
Goal: reach 1 => exponents both zero.
We can think of the process: we can apply a sequence of divisions by 6, but sometimes we need to multiply by 2 to adjust the exponents. Because if at some point a < b? Wait: division by 6 requires a>=1 and b>=1. If at some step we have a < b, we cannot divide by 6 if a is less than required? Actually division by 6 is only possible if the current number is divisible by 6, i.e., a>=1 and b>=1. If either exponent is zero, we cannot divide by 6. But we can multiply by 2 to increase a. So we might need to multiply by 2 to increase a to become at least b? Not exactly; we need a>=1 and b>=1. So if b=0, we have only powers of 2, we can't divide by 6 because need factor 3. However, we might also multiply by 2? Multiplying by 2 only increases a, b remains 0. That doesn't introduce 3. So if b=0, we cannot ever get a factor of 3, so we cannot divide by 6. But we could also have the possibility of first dividing by 6 when both present, then after some divisions maybe b becomes 0 before a? But we need to eventually reach 1, meaning both exponents become 0. If b becomes 0 while a>0, then we have number = 2^a. From there, we cannot divide by 6 (since no 3 factor). And multiplying by 2 only makes a larger. So that would be stuck. So we must ensure that we never get into state where b=0 and a>0 unless we are at 1 (both 0). To reach 1, we need to finally perform a division by 6 to reduce both from 1 and 1? Actually 1 corresponds to a=0,b=0. So the last step could be a division by 6 from 6 itself: a=1,b=1 -> divide by 6 => 1. Alternatively, we could reach 1 via other ways? Possibly from 2? No, dividing by 6 not possible. So the only way to get to 1 is to have both exponents zero simultaneously. Since operations only change exponents by specific increments: divide by 6: (a,b) -> (a-1, b-1) if a>=1,b>=1. Multiply by 2: (a,b) -> (a+1, b). So we can think of the net effect on exponents from initial (a,b) to (0,0). We can apply x times multiply by 2, and y times divide by 6. The order matters because division requires condition, but perhaps we can compute the minimal number of moves.
Let’s consider we need to end with (0,0). After applying d divisions (each reduces both exponents by 1) and m multiplications (each adds 1 to a), starting from (a,b). So final exponents: a_final = a + m - d; b_final = b - d (since multiplications don't affect b). Set a_final = 0, b_final = 0.
Thus:
(1) a + m - d = 0 => m = d - a.
(2) b - d = 0 => d = b.
Plug d = b into (1): m = b - a.
We need m >= 0, d >= 0, and also we must ensure that during the process we never try to divide when a exponent is less than something? But if we can schedule moves appropriately, can we always achieve if m = b - a >= 0? That gives a condition: b >= a. Also d = b, so m = b - a. The total moves = m + d = (b - a) + b = 2b - a.
But is this always minimal? Let's test with examples.
Example from problem: n=15116544. Let's compute its factorization. 15116544. They got answer 12. Let's compute a and b. 15116544 / 2? Actually we can compute: 15116544 =? Let's do factorization. 15116544 / 6? But let's compute exponents: We'll use analysis. 15116544 is divisible by 6? They used sequence: many divisions. So likely b > a? Let's compute.
We can compute number of times divisible by 2: 15116544 / 2 = 7558272; /2=3779136; /2=1889568; /2=944784; /2=472392; /2=236196; /2=118098; /2=59049; from 59049 is 3^? Actually 59049 = 3^10? 3^10 = 59049 indeed. So after dividing 2^? Let's count: starting from 15116544, we divided by 2 seven times? Let's check: 15116544 / 2 = 7558272 (1), /2=3779136 (2), /2=1889568 (3), /2=944784 (4), /2=472392 (5), /2=236196 (6), /2=118098 (7), /2=59049 (8). So after dividing 2^8, we get 59049, which is 3^10? 3^10=59049, correct. So initial a=8, b=10? But check: after dividing by 2 eight times we get odd number 59049, which is 3^10. But we must be careful: Did we divide out all factors of 2? Yes, 59049 is odd, so a=8. And b is exponent of 3: 59049 = 3^10, so b=10. So initial (a,b) = (8,10). Then b >= a, m = b - a = 2, d = b = 10. Total moves = 2b - a = 20 - 8 = 12. That matches answer 12. Good.
Another example: n=12. 12 = 2^2 * 3^1 => a=2,b=1. Then b < a? Here b=1, a=2 => b < a. Our equations gave d = b =1, m = b - a = -1, not allowed. So no solution? But is it impossible? Let's test manually: starting from 12, can we get to 1? Operations: divide by 6: 12/6=2, so now n=2. From 2, we can't divide by 6, multiply by 2 yields 4, etc. We cannot get to 1. So answer -1. That matches.
n=3: 3 = 2^0 * 3^1 => a=0,b=1. b >= a? 1 >=0, m = b - a = 1, d = b =1, total moves = 21 - 0 = 2. But the example output for n=3 is 2. Let's see: sequence? Starting from 3, we cannot divide by 6 (since not divisible by 2). So we must multiply by 2: 32=6. Then divide by 6 -> 1. That's 2 moves. Good.
n=2: a=1,b=0. b < a? 0<1. So d = b =0, m = -1, no solution. Indeed impossible: from 2, multiply by 2 leads to 4, etc. can't get 1. So -1.
n=1: a=0,b=0 => total moves 0. Works.
n=12345: Check if it contains primes other than 2 and 3. 12345 mod 5=0? Actually 12345/5=2469, so it contains factor 5, so impossible. Answer -1. Matches.
n=387420489: Let's factor: 387420489? That's 3^? Possibly 3^18? 3^10=59049, 3^12=531441, 3^15=14348907, 3^18=387420489. Indeed 3^18 = 387,420,489. So it's pure 3^18 => a=0, b=18. b>=a => m = 18-0=18, d=18, total moves = 36. Output is 36. Good.
Thus the solution is: if n has any prime factors other than 2 and 3, output -1. Otherwise, write n = 2^a * 3^b. If b >= a, answer = 2*b - a; else answer = -1.
But is it always minimal? Let's consider other possibilities where we might do more multiplications than needed, or different order? The derived equations gave necessary condition for existence of some sequence that ends at (0,0). We also need to check if the sequence is feasible respecting that division by 6 can only be performed when both exponents are at least 1 at that moment. But if we have m = b - a (>=0) and d = b, can we always schedule the moves to avoid hitting a state where division by 6 is not possible? Let's try to construct sequence: Starting from (a,b). We need to perform m multiplications (increase a) and d divisions (decrease both by 1). But we must ensure that before each division, a >=1 and b >=1. Since we start with a and b. At the beginning, b >= a, but could have a=0 initially (like n=3^b). That's okay because we can do multiplications first to increase a. The typical plan: first apply all m multiplications to increase a from a to a + m = a + (b - a) = b. So after m multiplications, we have (a+m, b) = (b, b). Now we have equal exponents (b,b). Then we can apply b divisions (d=b) in a row to reduce to (0,0). This works because after each division, both exponents stay equal and positive until zero. So the sequence is feasible. Total moves: m + b = (b-a) + b = 2b - a. Could there be a shorter sequence? Suppose we interleave multiplications and divisions to maybe reduce total? But note that each division reduces both exponents by 1, while multiplication increases a by 1. To go from (a,b) to (0,0), we need to reduce b to 0, which requires exactly b divisions (since multiplication doesn't reduce b). So d must be at least b. But can d be larger than b? If we do extra divisions? To have final b=0, we need b reduced exactly by d, because each division reduces b by 1. If we do more divisions than b, then b would become negative, impossible. So d exactly = b. So number of divisions is forced to be b. Now, after b divisions, the a exponent would become a - b (since each division subtracts 1 from a) plus any multiplications we did. The net change: final a = a + m - b. For final a to be 0, we need m = b - a. So if b - a is negative, it's impossible because we cannot have negative multiplications; we could perhaps do extra multiplications and extra divisions? But if we increase m more than b-a, then final a would be >0. Could we then do more divisions? But divisions also reduce b, and we need final b=0. If we do extra divisions, then we would need to increase b? There's no operation that increases b. So b must be reduced exactly to 0; any extra division would make b negative. So cannot have d > b. Therefore d = b exactly is forced. So the only way to achieve both zero is with d = b and m = b - a (which must be non-negative). Thus minimal moves is exactly that, because any successful sequence must use exactly b divisions, and b - a multiplications. There's no way to use fewer moves; total moves = b + (b-a) = 2b - a. Could there be sequences that use more moves? Possibly, but minimal is as computed.
Thus solution: For each n:
Remove all factors of 2 and 3, counting a = count of 2, b = count of 3.
If remaining number != 1, output -1.
Else if b >= a, output 2*b - a; else output -1.
But wait, is the condition b>=a necessary? From earlier, m = b - a must be >=0, so yes. But could there be a sequence where we perform some divisions before all multiplications that might allow a final outcome with b<a? For example, start with a=3,b=2 (so a > b). According to our condition, impossible. But can we still reach 1? Let's try: n= 2^3 * 3^2 = 89=72. Starting from 72, can we get to 1? 72 divisible by 6 => 72/6=12 (that's 2^23^1). Now a=2,b=1. Now from 12, we cannot divide by 6? Actually 12 is divisible by 6 => 12/6=2. That gives (1,0). Then we are stuck. Could we have used multiplication earlier? Starting from 72, maybe multiply by 2 first to get 144 (a=4,b=2). Then divide by 6: 144/6=24 (a=3,b=1). Then multiply by 2: 48 (a=4,b=1). Then maybe divide by 6? Not divisible because b=1 but a>=1, 48 divisible by 6? 48/6=8 (a=3? Actually 48=2^4*3^1, /6 gives 2^3=8, so b=0,a=3). Stuck. Or we could have done multiple multiplications before divisions. Eventually b will become 0 before a=0 because we start with b<a, and each division reduces b by 1 while a also reduces by 1, but if we multiply, a increases. To get both to zero, we need to get b to 0 only when a also 0. Since b is less than a initially, we would need to increase a relative to b? Actually we need to reduce b to 0. If we never increase a too much, after b divisions, a becomes a - b. Since a > b, a - b > 0. To make a - b = 0, we need to reduce a further. But the only way to reduce a is by division. However division also reduces b. Since b is already 0 after b divisions, we cannot perform additional divisions because b=0. So we would need to have performed extra divisions while b was positive to reduce a further, but that would reduce b further below zero? Actually if we do more than b divisions, b would become negative, impossible. So it's impossible. So condition stands.
Thus solution is straightforward.
But we need to be careful: When n=1, a=0,b=0 => b>=a holds, answer = 0 - 0? Actually 2*0 - 0 = 0. Good.
Edge cases: n up to 1e9, t up to 2e4, trivial.
Let's implement:
For each test case:
Read n.
If n == 1: print 0; continue.
Initialize a = 0, b = 0.
While n % 2 == 0: n //= 2; a++.
While n % 3 == 0: n //= 3; b++.
If n != 1: print -1.
Else if b >= a: print (2*b - a)
Else: print -1.
Test with examples.
One more test: n = 6 => a=1,b=1 => b>=a => 2*1-1=1. Indeed divide by 6: 1 move.
n = 18 => 18=23^2 => a=1,b=2 => b>=a => answer 22-1=3. Sequence? Maybe: multiply by 2? Actually m= b-a=1, d=b=2: total moves 3. Approach: start 18; maybe multiply by 2: 36 (a=2,b=2) then divide by 6: 6 (a=1,b=1) then divide by 6: 1 => 3 moves. Or alternative: divide by 6: 3 (a=0,b=1) then multiply by 2: 6 then divide by 6:1 => also 3 moves. So ok.
n = 4 => a=2,b=0 => b < a => impossible. Indeed from 4, can't get to 1.
n = 9 => a=0,b=2 => answer 2*2-0=4. Sequence: multiply by 2: 18 (a=1,b=2), multiply by 2: 36 (a=2,b=2) then divide by 6: 6 (a=1,b=1) then divide by 6: 1. That's 4 moves. Could we do shorter? 9 -> multiply to 18 -> divide to 3 -> multiply to 6 -> divide to 1: that's also 4 moves. So minimal 4.
n = 27 => a=0,b=3 => answer 6. Sequence: multiply 2 times to get to 108? Actually m = 3, d =3 => total 6. So that matches.
Thus solution seems correct.
But is there any scenario where we could reduce b by other means? Not really. So it's done.
Now we need to output answer for each test case.
Time complexity O(t * log n) but with small factors because we only divide by 2 and 3; worst-case n=1e9, maximum number of divisions by 2 is about 30, by 3 about 20, so negligible.
Thus final answer: implement as described.
But careful: Some might think of BFS or dynamic programming? Not needed.
Thus we produce the answer.
Now produce the solution in Python? But as ChatGPT we are to provide analysis and final answer with code? The user didn't explicitly ask for code but likely expects a solution. In the context, "You are given ..." and we must provide answer. The typical format: we output the answers. But in this chat, we are to solve the problem. So we can present reasoning and then final code (in C++ maybe). Since it's a coding problem. I'll produce a solution in Python.
But let me double-check the conditions: The input constraints: t up to 2e4, n up to 1e9. Python loops fine.
Implementation details: For each n, we compute a and b.
Potential pitfalls: While removing factors of 2 and 3, ensure integer division.
Now I think it's correct.
But let's also consider if there could be a scenario where b >= a but the answer could be different? Suppose a=0, b=0 => 0 moves. Works.
Also, if n=0? No, n>=1.
Edge case: n=6^p? That is a=p,b=p => answer = 2p-p = p. So for 6^p, answer p. That makes sense: just divide by 6 p times.
Now, what about n=2^a * 3^b with b >= a, but maybe we can sometimes do extra multiplications and extra divisions to shorten? As argued, number of divisions is exactly b because b must be reduced to 0; each division reduces b by exactly 1, and there is no other operation to reduce b. So at least b divisions needed. After b divisions, the net change to a is -b plus any multiplications. To end at 0, we need exactly a + m - b = 0 => m = b - a. So we need exactly that many multiplications if we want to end at 0 after exactly b divisions. Could we do more divisions if we also do more multiplications? Suppose we do m' > b - a, and d' > b. Then final a = a + m' - d'; final b = b - d'. For final b=0, we need d' = b. So d' must be exactly b. So m' must be exactly b - a. So no other combination. So minimal moves is exactly that.
Thus answer stands.
Therefore, final solution is simple.
We'll produce code accordingly.
Let's also think about potential overflow? No.
Now, the problem statement: "You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder)." This is exactly.
We solved.
Thus final answer: print for each test case the minimal number of moves or -1.
I'll provide a Python solution:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
n = int(data[idx]); idx+=1
if n == 1:
out.append('0')
continue
a = 0
b = 0
while n % 2 == 0:
n //= 2
a += 1
while n % 3 == 0:
n //= 3
b += 1
if n != 1:
out.append('-1')
elif b >= a:
out.append(str(2*b - a))
else:
out.append('-1')
sys.stdout.write('\n'.join(out))
That's it.
Now, if needed, can also use recursion? Not needed.
Thus final answer.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
pos = 1
for _ in range(t):
n = int(data[pos]); pos += 1
code
if n == 1:
out.append('0')
continue
a = b = 0
while n % 2 == 0:
n //= 2
a += 1
while n % 3 == 0:
n //= 3
b += 1
if n != 1: # other prime factors exist
out.append('-1')
elif b >= a:
out.append(str(2 * b - a))
else:
out.append('-1')
sys.stdout.write('\n'.join(out))