Algorithm


B. Multiply by 2, divide by 6
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer n. In one move, you can either multiply n by two or divide n by 66 (if it is divisible by 66 without the remainder).

Your task is to find the minimum number of moves needed to obtain 11 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 (1t21041≤�≤2⋅104) — the number of test cases. Then t test cases follow.

The only line of the test case contains one integer n (1n1091≤�≤109).

Output

For each test case, print the answer — the minimum number of moves needed to obtain 11 from n if it's possible to do that or -1 if it's impossible to obtain 11 from n.

Example
input
Copy
7
1
2
3
12
12345
15116544
387420489
output
Copy
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 1511654415116544:

  1. Divide by 66 and get 25194242519424;
  2. divide by 66 and get 419904419904;
  3. divide by 66 and get 6998469984;
  4. divide by 66 and get 1166411664;
  5. multiply by 22 and get 2332823328;
  6. divide by 66 and get 38883888;
  7. divide by 66 and get 648648;
  8. divide by 66 and get 108108;
  9. multiply by 22 and get 216216;
  10. divide by 66 and get 3636;
  11. divide by 66 and get 66;
  12. divide by 66 and get 11.

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  long long n;
  while(t--){
    cin>>n;
    int cnt3 = 0, cnt2 = 0;
    int k = n;
    while(k % 3 == 0 && k > 1){
      k /= 3;
      cnt3++;
    }
    while(k % 2 == 0 && k > 1){
      k /= 2;
      cnt2++;
    }
    if(k != 1){
      cout<<-1<<endl;
      continue;
    }
    if(cnt3 < cnt2){
      cout<<-1<<endl;
      continue;
    }
    int cnt6 = cnt3 - cnt2;
    n *= pow(2, cnt6);
    while(n>1){
      n /= 6;
      cnt6++;
    }
    cout<<cnt6<<endl;
    continue;
  }
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
7
1
2
3
12
12345
15116544
387420489

Output

x
+
cmd
0
-1
2
-1
-1
12
36
Advertisements

Demonstration


Codeforcess solution 1374-B B. Multiply by 2, divide by 6 ,C++, Java, Js and Python,1374-B,Codeforcess solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+