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 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow. 

The only line of the test case contains one integer $n$ ($1 \le n \le 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$.

Difficulty: EASY

Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	flag = False
	cnt = 0
	if n == 1:
		print('0')
		continue
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			cnt = cnt + 1
		else:
			n = n * 2
			if n % 6 != 0:
				flag = True
				break
			cnt = cnt + 1
	if flag == True:
		print('-1')
	else:
		print(cnt)


T = int(input())
for tc in range(T):
	t = int(input())
	ans = 0
	while t % 6 == 0:
		t = t // 6
		ans += 1
	while t % 3 == 0:
		t = t // 3
		ans += 2
	if t == 1:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	c = 0
	a = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			c += 1
		elif n % 3 == 0:
			n = n * 2
			c += 1
		else:
			a = 1
			break
	if a:
		print(-1)
	else:
		print(c)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	ans = 0
	while n > 1:
		if n % 6 == 0:
			n /= 6
		else:
			n *= 2
			if n % 3 != 0:
				ans = -1
				break
		ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	t1 = 0
	t2 = 0
	while n % 2 == 0:
		n //= 2
		t1 += 1
	while n % 3 == 0:
		n //= 3
		t2 += 1
	if n != 1:
		print(-1)
	elif t1 > t2:
		print(-1)
	else:
		print(t2 - t1 + t2)


t = int(input())
for qw in range(t):
	n = int(input())
	a = b = n
	count2 = count3 = 0
	while a % 2 == 0:
		a /= 2
		count2 += 1
	while b % 3 == 0:
		b /= 3
		count3 += 1
		a /= 3
	if a != 1 and n != 1:
		print(-1)
	elif count3 < count2:
		print(-1)
	else:
		ans = count3 - count2
		n *= 2 ** (count3 - count2)
		while n % 6 == 0:
			n /= 6
			ans += 1
			if n != int(n):
				break
		print(ans)


for _ in range(int(input())):
	n = int(input())
	n1 = n
	c3 = 0
	while n % 3 == 0:
		n //= 3
		c3 += 1
	c2 = 0
	while n % 2 == 0:
		n //= 2
		c2 += 1
	n = n1
	if c2 == c3:
		for _ in range(c2):
			n //= 6
		if n == 1:
			print(c2)
		else:
			print(-1)
	elif c2 > c3:
		print(-1)
	else:
		for _ in range(c3 - c2):
			n *= 2
		for _ in range(c3):
			n //= 6
		if n == 1:
			print(c3 - c2 + c3)
		else:
			print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	k = 0
	while n != 1 and n % 3 == 0:
		if n % 6 != 0:
			n *= 2
		else:
			n //= 6
		k += 1
	if n == 1:
		print(k)
	else:
		print(-1)


t = int(input())
for q in range(0, t):
	n = int(input())
	count2 = 0
	count3 = 0
	while n % 2 == 0:
		count2 += 1
		n = n // 2
	while n % 3 == 0:
		count3 += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif count2 > count3:
		print(-1)
	else:
		print(count3 - count2 + count3)


t = int(input())
while t:
	n = int(input())
	x = 10000000000000
	count = 0
	if n == 1:
		print('0')
	else:
		f = 0
		while n != 1 and n <= x:
			if n % 6 == 0:
				count += 1
				n = n // 6
				f = 1
			else:
				n = n * 2
				count += 1
				f = 0
		if f == 1:
			print(count)
		else:
			print('-1')
	t = t - 1


for i in range(int(input())):
	n = int(input())
	c = 0
	while n % 3 == 0:
		o = n % 2
		c += o + 1
		if o == 0:
			n //= 2
		n //= 3
	if n != 1:
		print(-1)
	else:
		print(c)


def factors(x):
	d = {2: 0, 3: 0}
	while x > 1:
		if x % 2 == 0:
			d[2] += 1
			x = x // 2
		elif x % 3 == 0:
			d[3] += 1
			x = x // 3
		else:
			return False
	return d
for _ in range(int(input())):
	n = int(input())
	f = factors(n)
	if f == False or f[2] > f[3]:
		print(-1)
	else:
		print(f[3] - f[2] + f[3])


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		if n == 1:
			print(0)
		else:
			i = 0
			while n != 1:
				if n % 3 != 0:
					print(-1)
					break
				elif n % 6 == 0:
					n = n // 6
					i += 1
				else:
					n = n // 3
					i += 2
			else:
				print(i)
main()


for i in range(int(input())):
	n = int(input())
	count_2 = 0
	count_3 = 0
	while n % 2 == 0:
		n /= 2
		count_2 += 1
	while n % 3 == 0:
		n /= 3
		count_3 += 1
	if n != 1 or count_2 > count_3:
		print(-1)
	else:
		print(count_3 + (count_3 - count_2))


import sys
import math
import collections

def set_debug(debug_mode=False):
	if debug_mode:
		fin = open('input.txt', 'r')
		sys.stdin = fin

def int_input():
	return list(map(int, input().split()))
t = int(input())
for ti in range(1, t + 1):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
	else:
		res = 0
		while n > 1:
			if n % 6 == 0:
				n = n // 6
				res += 1
			elif n % 3 != 0:
				print(-1)
				break
			else:
				n = n * 2
				res += 1
		else:
			print(res)


t = int(input())
for i in range(t):
	n = int(input())
	hod = 0
	while n != 1:
		if n % 6 == 0:
			n = n / 6
			hod += 1
		else:
			n = n * 2
			hod += 1
			if n % 6 != 0:
				hod = -1
				break
	print(hod)


t = int(input())
for i in range(t):
	n = int(input())
	cnt_2 = cnt_3 = 0
	while n % 3 == 0:
		cnt_3 += 1
		n //= 3
	while n % 2 == 0:
		cnt_2 += 1
		n //= 2
	if n == 1 and cnt_3 >= cnt_2:
		print(2 * cnt_3 - cnt_2)
	else:
		print(-1)


k = int(input())
for i in range(k):
	n = int(input())
	twos = 0
	threes = 0
	possible = True
	while n % 2 == 0:
		n = n // 2
		twos = twos + 1
	while n % 3 == 0:
		n = n // 3
		threes = threes + 1
	if n == 1 and threes >= twos:
		print(threes + (threes - twos))
	else:
		print(-1)


t = int(input())
for j in range(t):
	n = int(input())
	ct = 0
	while not n & 1:
		ct += 1
		n >>= 1
	ct2 = 0
	while not n % 3:
		ct2 += 1
		n //= 3
	if ct > ct2 or n != 1:
		print(-1)
	else:
		print(max(ct, ct2) + abs(ct - ct2))


t = int(input())
for _ in range(t):
	(count2, count3) = (0, 0)
	n = int(input())
	while n % 2 == 0:
		n //= 2
		count2 += 1
	while n % 3 == 0:
		n //= 3
		count3 += 1
	if n == 1 and count2 <= count3:
		print(2 * count3 - count2)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	count = 0
	temp = 0
	while n > 1:
		if n % 3 != 0:
			temp = 1
			break
		elif n % 6 != 0:
			count += 2
			n = n // 3
		else:
			n = n // 6
			count += 1
	if temp == 1:
		print(-1)
	else:
		print(count)


def solve(n):
	count = 0
	flag = True
	while flag:
		if n == 1:
			return count
		elif n % 3 == 0 or n % 6 == 0:
			if n % 6 == 0:
				n = n / 6
			else:
				n *= 2
			count += 1
		elif n % 3 != 0 and n % 6 != 0:
			return -1
tc = int(input())
while tc > 0:
	n = int(input())
	print(solve(n))
	tc -= 1


for i in range(int(input())):
	n = int(input())
	(f2, f3) = (0, 0)
	while not n % 2:
		n //= 2
		f2 += 1
	while not n % 3:
		n //= 3
		f3 += 1
	if n != 1:
		print(-1)
		continue
	if f2 > f3:
		print(-1)
		continue
	if f2 == f3:
		print(f2)
	else:
		print(f3 + (f3 - f2))


import sys
import os
import time
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random
import string

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' ', end='\n'):
	print(sep.join(map(str, a)), end=end)

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)
for _ in range(ii()):
	n = ii()
	c = 0
	f = 1
	while n != 1:
		if n % 3:
			f = 0
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		c += 1
	print(c if f else -1)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	cn2 = 0
	cn3 = 0
	while n % 2 == 0:
		n //= 2
		cn2 += 1
	while n % 3 == 0:
		n //= 3
		cn3 += 1
	if n == 1 and cn2 <= cn3:
		print(cn3 + cn3 - cn2)
	else:
		print(-1)


n = int(input())
ar = list()
for i in range(n):
	x = int(input())
	move = 0
	if x == 1:
		ar.append(0)
	elif x % 3 == 0:
		while x != 1 and x % 3 == 0:
			if x % 6 == 0:
				move += 1
				x /= 6
			else:
				x *= 2
				move += 1
		if x == 1:
			ar.append(move)
		else:
			ar.append(-1)
	else:
		ar.append(-1)
for i in ar:
	print(i)


for t in range(int(input())):
	n = int(input())
	x = 0
	steps = 0
	while n != 1:
		if n == 1:
			break
		elif n % 3 != 0:
			x = 1
			print(-1)
			break
		elif n % 6 == 0:
			n = int(n / 6)
			steps += 1
		else:
			n = n * 2
			steps += 1
	if x == 0:
		print(steps)


t = int(input())
for i in range(t):
	x = int(input())
	three = 0
	two = 0
	while x % 3 == 0:
		x /= 3
		three += 1
	while x % 2 == 0:
		x /= 2
		two += 1
	if x != 1:
		print(-1)
	elif two > three:
		print(-1)
	else:
		print(three - two + three)


t = int(input())
while t:
	t -= 1
	arr = int(input())
	ct = 0
	done = False
	while arr != 1:
		if arr % 6 == 0:
			arr = arr / 6
			ct += 1
		elif arr % 3 == 0:
			arr *= 2
			ct += 1
		else:
			done = True
			arr = 1
	if done == True:
		ct = -1
	print(ct)


for i in range(int(input())):
	x = int(input())
	count = 0
	while x != 1:
		if x % 3 != 0:
			count = -1
			break
		elif x % 6 == 0:
			x = x // 6
			count = count + 1
		else:
			x = x * 2
			count = count + 1
	print(count)


T = int(input())
for _ in range(T):
	n = int(input())
	count = flag = 0
	while n != 1:
		if n % 6 == 0:
			count += 1
			n //= 6
		else:
			n *= 2
			if n % 6 != 0:
				flag = 1
				break
			count += 1
	if flag == 1:
		print(-1)
	else:
		print(count)


def a():
	n = int(input())
	count = 0
	if n == 1:
		print(0)
		return 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			count += 1
			continue
		if n % 3 == 0 and n % 6 != 0:
			n = n * 2
			count += 1
			continue
		if n % 3 != 0 and n % 6 != 0:
			count = 0
			break
	if count == 0:
		print(-1)
	else:
		print(count)
t = int(input())
for i in range(t):
	a()


def main():
	for _ in range(int(input())):
		n = int(input())
		(c3, c2) = (0, 0)
		while n % 2 == 0:
			n //= 2
			c2 += 1
		while n % 3 == 0:
			n //= 3
			c3 += 1
		if n == 1:
			if c3 >= c2:
				print(c3 + c3 - c2)
			else:
				print(-1)
		else:
			print(-1)
main()


t = int(input())
while t:
	n = int(input())
	c = 0
	f = 0
	if n == 1:
		print(0)
	else:
		while n != 1:
			if n == 2 or n == 4 or n == 5:
				f = 1
				break
			if n % 6 == 0:
				n = n // 6
				c += 1
			elif n % 3 == 0:
				n = n * 2
				c += 1
			else:
				f = 1
				break
		if f == 0:
			print(c)
		else:
			print(-1)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	(count, count2) = (0, 0)
	while n % 2 == 0:
		n //= 2
		count += 1
	while n % 3 == 0:
		n //= 3
		count2 += 1
	if count > count2 or n != 1:
		print(-1)
	else:
		print(count2 - count + count2)


t = int(input())
for _ in range(t):
	n = int(input())
	temp = n
	threes = 0
	twos = 0
	while temp % 2 == 0:
		twos += 1
		temp /= 2
	temp = n
	while temp % 3 == 0:
		threes += 1
		temp /= 3
	twos_added = max(threes - twos, 0)
	operations = twos_added + threes
	temp = n
	temp = temp * 2 ** twos_added
	while temp % 6 == 0:
		temp /= 6
	if temp == 1:
		print(operations)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	d = {}
	f = 0
	while n != 1:
		t = n % 6
		if d.get(t) and int(d[t]) <= n:
			f = 1
			break
		d[t] = n
		if t == 0:
			n /= 6
			c += 1
		else:
			n *= 2
			c += 1
	if f == 1:
		print(-1)
	else:
		print(c)


def get(n):
	c2 = 0
	c3 = 0
	while n != 1:
		if n % 2 == 0:
			c2 += 1
			n = n // 2
		elif n % 3 == 0:
			c3 += 1
			n = n // 3
		else:
			return -1
	if c2 > c3:
		return -1
	else:
		return c3 - c2 + c3
for i in range(int(input())):
	n = int(input())
	print(get(n))


t = int(input())
while t > 0:
	inp = int(input())
	two = 0
	while inp % 2 == 0:
		inp = inp // 2
		two += 1
	three = 0
	while inp % 3 == 0:
		inp = inp // 3
		three += 1
	if inp != 1 or two > three:
		print(-1)
	elif two == three:
		print(two)
	elif two < three:
		print(2 * three - two)
	t -= 1


from collections import defaultdict as dft
for _ in range(int(input())):
	n = int(input())
	ans = 0
	case = 0
	if n == 1:
		print(0)
		continue
	while n != 1:
		if n % 3:
			case = 1
			break
		if n % 6 == 0:
			n = n // 6
		else:
			n *= 2
		ans += 1
	if case:
		print(-1)
	else:
		print(ans)


def answer(n):
	num_moves = 0
	while n % 6 == 0:
		n /= 6
		num_moves += 1
	while n % 3 == 0:
		n *= 2
		num_moves += 1
		n /= 6
		num_moves += 1
	if n == 1:
		return num_moves
	else:
		return -1

def main():
	t = int(input())
	n = [0] * t
	for j in range(t):
		n[j] = int(input())
	for j in range(t):
		print(answer(n[j]))
main()


n = int(input())
l = [input() for i in range(n)]

def stepcount(x):
	i = 0
	while x > 1:
		if x < 0:
			i = -6
			print('0, {}'.format(x))
		if x == 1:
			i = 0
		elif x % 6 == 0:
			x = x // 6
		elif (x * 2 % 2 == 0 and x * 2 % 3 == 0) and x > 6:
			x = x * 2
		elif x % 2 == 0 and x < 6:
			x = x * 2
		elif x % 3 == 0 and x < 6:
			x = x * 2
		else:
			return -1
		i += 1
	return i
for j in range(0, len(l)):
	print(stepcount(int(l[j])))


import math
t = int(input())
for _ in range(t):
	n = int(input())
	(f2, f3) = (0, 0)
	while n % 2 == 0:
		f2 += 1
		n /= 2
	while n % 3 == 0:
		f3 += 1
		n /= 3
	if n == 1 and f2 <= f3:
		print(2 * f3 - f2)
	else:
		print(-1)


n = int(input())
for i in range(n):
	x = int(input())
	y = x
	if x == 1:
		print(0)
	elif x % 3 == 0:
		c = False
		a = 0
		while x != 1:
			if round(x / 6) != x / 6:
				x = x * 2
				a += 1
			else:
				x = x / 6
				a += 1
			if x > y * 2:
				c = True
				break
		if c == True:
			print(-1)
		else:
			print(int(a))
	else:
		print(-1)


def countSteps(num):
	count = 0
	while num > 1:
		if num % 6 == 0:
			num = num // 6
			count += 1
		elif num % 2 == 0:
			print(-1)
			return
		else:
			num = 2 * num
			count += 1
	print(count)
	return

def solveA():
	t = int(input())
	lst = []
	for i in range(t):
		lst.append(int(input()))
	for num in lst:
		countSteps(num)
	return
solveA()


for _ in range(int(input())):
	n = int(input())
	f = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			f += 1
		elif n % 2 != 0:
			n = n * 2
			f += 1
		else:
			n = 0
			f = -1
	print(f)


counts = []
n = int(input())
for i in range(n):
	counts.append(int(input()))

def norm_num(num):
	num2 = 0
	num3 = 0
	while num != 1:
		if num % 2 == 0:
			num /= 2
			num2 += 1
		elif num % 3 == 0:
			num /= 3
			num3 += 1
		else:
			return False
	if num2 <= num3:
		return True
	else:
		return False
for num in counts:
	res = num
	all = 0
	ok = norm_num(num)
	while res != 1:
		if res % 6 == 0:
			res /= 6
		else:
			res *= 2
		all += 1
		if not ok:
			all = -1
			break
	print(all)


t = int(input())
for i in range(t):
	n = int(input())
	s2 = 0
	s3 = 0
	while n % 2 == 0:
		n /= 2
		s2 += 1
	while n % 3 == 0:
		n /= 3
		s3 += 1
	if n != 1 or s2 > s3:
		print(-1)
	else:
		print(2 * s3 - s2)


t = int(input())
for i in range(t):
	n = int(input())
	tw = 0
	th = 0
	while n > 1 and n % 2 == 0 or n % 3 == 0:
		if n % 2 == 0:
			n = n // 2
			tw += 1
		if n % 3 == 0:
			n = n // 3
			th += 1
	if n == 1:
		if th >= tw:
			print(max(th - tw, 0) + th)
		else:
			print(-1)
	else:
		print(-1)


import sys
t = 1
t = int(input())
for i in range(t):
	n = int(input())
	f = 1
	a = 0
	while n % 3 == 0:
		n = n // 3
		a += 1
	b = 0
	while n % 2 == 0:
		n = n // 2
		b += 1
	if n == 1:
		f = 0
	if f:
		print(-1)
	elif b > a:
		print(-1)
	else:
		print(a + a - b)


for _ in ' ' * int(input()):
	n = int(input())
	cnt = 0
	if n == 1:
		print(0)
	elif n % 3:
		print(-1)
	else:
		f = 0
		while n != 1:
			if n % 6 == 0:
				n = n // 6
			elif n % 3:
				print(-1)
				f = 1
				break
			else:
				n = n * 2
			cnt += 1
		if f == 0:
			print(cnt)


T = int(input())
for t in range(T):
	n = int(input())
	count = 0
	while n != 1:
		if n % 2 == 0 and n % 3 != 0:
			count = -1
			break
		if n % 6 == 0:
			n = n / 6
		else:
			n = n * 2
		count = count + 1
	print(count)


GI = lambda : int(input())
GIS = lambda : map(int, input().split())
LGIS = lambda : list(GIS())

def main():
	for t in range(GI()):
		n = GI()
		threes = twos = 0
		while not n % 3:
			threes += 1
			n //= 3
		while not n % 2:
			twos += 1
			if twos > threes:
				print(-1)
				break
			n //= 2
		else:
			if n != 1:
				print(-1)
			else:
				print(threes + (threes - twos))
main()


for _ in range(int(input())):
	n = int(input())
	n1 = n
	two = 0
	thee = 0
	if n == 1:
		print(0)
		continue
	while n1 % 2 == 0:
		two += 1
		n1 = n1 // 2
	while n1 % 3 == 0:
		thee += 1
		n1 = n1 // 3
	if n1 != 1 or thee == 0 or two > thee:
		print(-1)
	else:
		print(2 * thee - two)


cases = int(input())
for i in range(cases):
	t = 0
	n = int(input())
	while n % 3 == 0 and n != 1:
		if n % 6 == 0:
			n = n // 6
			t += 1
		else:
			n *= 2
			t += 1
	print(t) if n == 1 else print(-1)


for p in range(int(input())):
	n = int(input())
	x = 0
	y = 0
	while n % 2 == 0:
		x += 1
		n = n // 2
	while n % 3 == 0:
		y += 1
		n = n // 3
	if y >= x and n == 1:
		print(y - x + y)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	m = 0
	while n > 1:
		if n % 3 != 0:
			m = -1
			break
		elif n % 2 == 0:
			n = n / 6
			m = m + 1
		else:
			n = n / 3
			m = m + 2
	print(m)


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	count = 0
	temp = 0
	ans = True
	while n > 1:
		if n % 6 == 0:
			n /= 6
			temp = 0
		else:
			n *= 2
			temp += 1
		count += 1
		if temp == 10:
			print(-1)
			ans = False
			break
	if ans == True:
		print(count)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	cnt = 0
	if n == 1:
		print(0)
		continue
	while n > 0:
		if n == 1:
			break
		if n % 6 == 0:
			n //= 6
			cnt += 1
		elif n % 3 == 0:
			n //= 3
			cnt += 2
		else:
			cnt = -1
			break
	print(cnt)


t = int(input())
for i in range(t):
	n = int(input())
	(s1, s2) = (0, 0)
	while n % 2 == 0:
		n /= 2
		s1 += 1
	while n % 3 == 0:
		n /= 3
		s2 += 1
	if n == 1:
		if s1 <= s2:
			ans = min(s1, s2)
			s2 -= ans
			s1 -= ans
			ans += s2 * 2
			print(ans)
		else:
			print('-1')
	else:
		print('-1')


import math
t = int(input())
while t > 0:
	n = int(input())
	c = 0
	if n == 1:
		print(0)
	elif n == 3:
		print(2)
	elif n == 2:
		print(-1)
	else:
		s = 0
		while True:
			if n % 6 == 0:
				c += 1
				n = int(n / 6)
			elif n * 4 % 6 != 0 and n != 1:
				print(-1)
				break
			elif n == 1:
				print(c)
				break
			else:
				n = n * 2
				c += 1
	t -= 1


n = int(input())

def divide(x):
	count2 = 0
	count3 = 0
	while x % 2 == 0:
		x /= 2
		count2 += 1
	while x % 3 == 0:
		x /= 3
		count3 += 1
	if x != 1 or count2 > count3:
		print(-1)
	else:
		print(count3 * 2 - count2)
for i in range(n):
	divide(int(input()))


for t in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		i = 0
		j = 0
		while n % 2 == 0:
			n = int(n / 2)
			i = i + 1
		while n > 1 and n % 3 == 0:
			n = int(n / 3)
			j = j + 1
		if n != 1 or i > j:
			print(-1)
		else:
			print(2 * j - i)


for _ in range(int(input())):
	n = int(input())
	twos = 0
	while n % 2 == 0 and n // 2 > 0:
		n //= 2
		twos += 1
	threes = 0
	while n % 3 == 0 and n // 3 > 0:
		n //= 3
		threes += 1
	if n != 1 or threes < twos:
		print(-1)
	else:
		print(threes - twos + threes)


import string
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	s = str(n)
	ans = 0
	for j in s:
		j = int(j)
		ans += j
	if ans % 3 != 0:
		print(-1)
		continue
	count2 = 0
	count3 = 0
	x = n
	tmp = 0
	while x == int(x) and x != 1:
		if x % 2 == 0:
			count2 += 1
			x //= 2
		elif x % 3 == 0:
			count3 += 1
			x //= 3
		else:
			print(-1)
			tmp = 1
			break
	if tmp == 1:
		continue
	if count2 == count3:
		print(count3)
	elif count2 > count3:
		print(-1)
	else:
		print(2 * count3 - count2)


from math import log
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		moves = 0
		increase = 0
		while True:
			if n % 6 == 0:
				n = n // 6
				increase -= 1
			else:
				n = n * 2
				increase += 1
			moves += 1
			if increase >= 30:
				print(-1)
				break
			if n == 1:
				print(moves)
				break


t = int(input())
for i in range(t):
	m = int(input())
	ans = 0
	while m % 6 == 0:
		m = m // 6
		ans += 1
	while m % 3 == 0:
		m = m // 3
		ans += 2
	if m == 1:
		print(ans)
	else:
		print(-1)


def get(n):
	if n == 1:
		return 0
	if n % 3 != 0:
		return -1
	l = 0
	k = 0
	(n, r) = divmod(n, 3)
	while r == 0:
		l += 1
		(n, r) = divmod(n, 3)
	n = int(3 * n + r)
	(n, r) = divmod(n, 2)
	while r == 0:
		k += 1
		(n, r) = divmod(n, 2)
	n = int(2 * n + r)
	if n != 1:
		return -1
	if k > l:
		return -1
	return 2 * l - k
m = int(input())
for _ in range(m):
	n = int(input())
	print(get(n))


for I in range(int(input())):
	n = int(input())
	res = 0
	while 1:
		if n == 6:
			n = n // 6
			res += 1
			break
		if n % 6 == 0:
			if n // 6 % 6 == 0:
				n = n // 6
			elif n * 2 // 6 % 6 == 0:
				n = n * 2
			else:
				break
		elif n * 2 % 6 == 0:
			n = n * 2
		else:
			break
		res += 1
	if n != 1:
		print(-1)
	else:
		print(res)


def power(n, x):
	result = 0
	while n % x == 0:
		result += 1
		n = n // x
	return result

def road_to_one(n):
	if n == 1:
		return 0
	elif n > 1 and n % 3 == 0:
		if power(n, 3) >= power(n, 2) and 3 ** power(n, 3) * 2 ** power(n, 2) == n:
			return 2 * power(n, 3) - power(n, 2)
		else:
			return -1
	else:
		return -1
t = int(input())
for i in range(t):
	n = int(input())
	print(road_to_one(n))


n = int(input())
for i in range(n):
	t = int(input())
	a = b = 0
	while t % 2 == 0:
		t /= 2
		a += 1
	while t % 3 == 0:
		t /= 3
		b += 1
	if t != 1 or a > b:
		print('-1')
	else:
		print(2 * b - a)


def solve(x):
	(a, b) = (0, 0)
	while x % 2 == 0:
		x /= 2
		a += 1
	while x % 3 == 0:
		x /= 3
		b += 1
	if x != 1 or b < a:
		return -1
	return b + (b - a)
t = int(input())
for _ in range(t):
	print(solve(int(input())))


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return list(map(int, input().split()))
n = inp()
for i in range(n):
	n = inp()
	if n == 1:
		print(0)
	else:
		t = 2
		cnt = 0
		while n % 2 == 0:
			cnt += 1
			n //= 2
		t = 3
		cnt2 = 0
		while n % 3 == 0:
			cnt2 += 1
			n //= 3
		if cnt2 == 0 or n != 1 or cnt > cnt2:
			print(-1)
		else:
			print(cnt2 - cnt + cnt2)


t = int(input())
while t > 0:
	n = int(input())
	ans = 0
	if n % 6 == 0 or n % 3 == 0 or n == 1:
		while n != 1:
			if n % 6 == 0:
				n = n // 6
			elif n % 3 == 0:
				n = n * 2
			else:
				ans = -1
				break
			ans += 1
	else:
		ans = -1
	print(ans)
	t -= 1


def solve(n):
	steps = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			steps += 1
		elif n % 3 == 0:
			n = n // 3
			steps += 2
		else:
			steps = -1
			break
	print(steps)
t = int(input())
for _ in range(t):
	n = int(input())
	solve(n)


t = int(input())
import math

def primeFactors(n):
	f = False
	ans = []
	two = 0
	three = 0
	while n % 2 == 0:
		f = True
		n = n / 2
		two += 1
	if f:
		ans.append(2)
	while n % 3 == 0:
		c = True
		n = n / 3
		three += 1
	a = [two, three]
	if n == 1:
		return a
	else:
		return -1

def func(n):
	a = primeFactors(n)
	twos = 0
	threes = 0
	if a == -1:
		return -1
	elif a[0] > a[1]:
		return -1
	else:
		return 2 * a[1] - a[0]
for i in range(t):
	n = int(input())
	print(func(n))


t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	c1 = 0
	while n % 3 == 0:
		n //= 3
		c1 += 1
	if n == 1:
		if c1 >= c:
			ans = c1 - c
			ans += c1
			print(ans)
		else:
			print(-1)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print('0')
		continue
	t = 0
	th = 0
	while True:
		if not n % 3:
			th += 1
			n = n / 3
		elif not n % 2:
			t += 1
			n = n / 2
		else:
			break
	if int(n) != 1:
		print('-1')
	elif th >= t:
		print('{}'.format(2 * th - t))
	else:
		print('-1')


t = int(input())
for _ in range(t):
	n = int(input())
	a = 0
	b = 0
	while n % 3 == 0:
		n //= 3
		a += 1
	while n % 2 == 0:
		b += 1
		n //= 2
	if n != 1:
		print(-1)
	elif b > a:
		print(-1)
	else:
		print(2 * a - b)


t = int(input(''))
while t > 0:
	n = int(input(''))
	threecount = twocount = 0
	flag = 0
	ans = 1
	while n > 1:
		if n % 3 == 0 or n % 2 == 0:
			if n % 3 == 0:
				n = n // 3
				threecount += 1
			if n % 2 == 0:
				n = n // 2
				twocount += 1
		else:
			ans = -1
			flag = 1
			break
	if flag == 0:
		if twocount > threecount:
			ans = -1
		else:
			ans = threecount + (threecount - twocount)
	print(ans)
	t -= 1


t = int(input())
while t:
	n = int(input())
	a = 0
	b = 0
	while n % 2 == 0:
		a += 1
		n = n // 2
	while n % 3 == 0:
		b += 1
		n = n // 3
	if n == 1:
		if a > b:
			print(-1)
		else:
			x = abs(b - a)
			print(max(b, a) + x)
	else:
		print(-1)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	flag = 1
	num = 0
	if n == 1:
		print(0)
	else:
		while flag:
			if n % 6 == 0:
				n = n // 6
				num += 1
			elif n * 2 % 6 == 0:
				n = n * 2 // 6
				num += 2
			else:
				flag = 0
		if n != 1:
			print(-1)
		else:
			print(num)


num_exa = input()
for i in range(int(num_exa)):
	n = input()
	n = int(n)
	moves = 0
	while True:
		if n == 1:
			break
		elif n % 3 != 0:
			moves = -1
			break
		elif n % 6 != 0:
			n = n * 2
			moves = moves + 1
		elif n % 6 == 0:
			n = n / 6
			moves = moves + 1
	print(moves)


for _ in range(int(input())):
	n = int(input())
	cnt2 = 0
	while n % 2 == 0:
		n = n // 2
		cnt2 = cnt2 + 1
	cnt3 = 0
	while n % 3 == 0:
		n = n // 3
		cnt3 = cnt3 + 1
	if n > 1:
		print(-1)
		continue
	if cnt2 > cnt3:
		print(-1)
		continue
	d = cnt3 - cnt2
	print(cnt3 + d)


import sys
input = sys.stdin.readline
f = lambda : list(map(int, input().strip('\n').split()))
res = []
for _ in range(int(input())):
	n = int(input())
	_2 = _3 = 0
	while not n % 3:
		n //= 3
		_3 += 1
	while not n % 2:
		n //= 2
		_2 += 1
	if n != 1:
		res.append(-1)
	elif _2 > _3:
		res.append(-1)
	else:
		res.append(_3 + _3 - _2)
print('\n'.join(map(str, res)))


for i in range(int(input())):
	n = int(input())
	c = 0
	d = 0
	if n == 1:
		print(0)
	else:
		while n % 3 == 0:
			n = n / 3
			c = c + 1
		while n % 2 == 0:
			n = n // 2
			d = d + 1
		if n != 1:
			print(-1)
		elif d > c:
			print(-1)
		elif d == c:
			print(c)
		else:
			print(d + 2 * (c - d))


ans = []
for i in range(int(input())):
	n = int(input())
	count = 0
	while n != 1:
		if n * 2 % 6 != 0:
			count = -1
			break
		elif n % 6 == 0:
			n /= 6
			count += 1
		else:
			n *= 2
			count += 1
	ans.append(count)
for i in ans:
	print(i)


t = int(input())
for s in range(t):
	n = int(input())
	count = 0
	while True:
		if n % 6 == 0:
			n = n // 6
			count += 1
		elif n == 1:
			break
		else:
			break
	while True:
		if n % 3 == 0:
			n //= 3
			count += 2
		else:
			break
	if n == 1:
		print(count)
	else:
		print(-1)


n = int(input())
for i in range(n):
	x = int(input())
	m = 0
	n = 0
	while x % 2 == 0:
		x = x / 2
		m += 1
	while x % 3 == 0:
		x = x / 3
		n += 1
	if x == 1 and n >= m:
		print(2 * n - m)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	(tw, tr) = (0, 0)
	while n % 2 == 0:
		n //= 2
		tw += 1
	while n % 3 == 0:
		n //= 3
		tr += 1
	if n != 1 or tr < tw:
		print(-1)
	else:
		print(tr + tr - tw)


import collections
from string import ascii_lowercase
import math

def solve(n):
	cnt2 = 0
	cnt3 = 0
	while n % 2 == 0:
		cnt2 += 1
		n = n // 2
	while n % 3 == 0:
		cnt3 += 1
		n = n // 3
	if n == 1 and cnt2 <= cnt3:
		return cnt3 - cnt2 + cnt3
	else:
		return -1
t = int(input())
while t:
	n = int(input())
	print(solve(n))
	t -= 1


tests = int(input())
for i in range(tests):
	num = int(input())
	count = 0
	while num != 1:
		if num % 3 == 0:
			if num % 2 == 0:
				num = num / 6
				count += 1
			else:
				num = num * 2
				count += 1
		else:
			count = -1
			break
	print(count)


c = int(input())
for i in range(c):
	n = int(input())
	k = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			k += 1
		elif n % 2 != 0:
			n = n * 2
			k += 1
		else:
			n = 0
			k = -1
	print(k)


for _ in range(int(input())):
	n = int(input())
	res = 0
	while n % 6 == 0:
		res += 1
		n //= 6
	while n % 3 == 0:
		res += 2
		n //= 3
	if n == 1:
		print(res)
	else:
		print(-1)


t = int(input())
for i in range(t):
	k2 = 0
	k3 = 0
	x = int(input())
	l = x
	while l % 2 == 0:
		k2 += 1
		l = l // 2
	while l % 3 == 0:
		k3 += 1
		l /= 3
	if l != 1 or k2 > k3:
		print(-1)
	else:
		print(k2 + (k3 - k2) * 2)


s = int(input())
for a in range(s):
	h = int(input())
	i = 0
	while h > 1:
		if h % 6 == 0:
			h = h // 6
			i += 1
		elif h % 2 != 0:
			h = h * 2
			i += 1
		else:
			h = 0
			i = -1
	print(i)


t = int(input())
while t:
	t -= 1
	n = int(input())
	q = [n]
	count = 0
	flag = False
	while n > 1 and n < 10000000000:
		if n % 6 == 0:
			n = n // 6
			count += 1
		else:
			n *= 2
			count += 1
	if n == 1:
		print(count)
	else:
		print(-1)


def solve(n):
	ct3 = 0
	ct2 = 0
	while n % 3 == 0:
		n = n // 3
		ct3 += 1
	while n % 2 == 0:
		n = n // 2
		ct2 += 1
	if n != 1 or ct2 > ct3:
		return -1
	else:
		return ct3 - ct2 + ct3
for i in range(int(input())):
	n = int(input())
	print(solve(n))


t = int(input())
for i in range(t):
	n = int(input())
	p = 0
	q = n
	while q != 0:
		if q % 3 ** (p + 1) == 0:
			p += 1
		else:
			q = 0
			break
	x = 0
	q = n
	while q != 0:
		if q % 2 ** (x + 1) == 0:
			x += 1
		else:
			q = 0
			break
	if p >= x and n // (3 ** p * 2 ** x) == 1:
		print(p - x + p)
	else:
		print(-1)


t = int(input())
for j in range(t):
	x = int(input())
	(n, m) = (0, 0)
	f = 0
	while x != 1:
		if x % 2 == 0:
			x /= 2
			n += 1
		elif x % 3 == 0:
			x /= 3
			m += 1
		else:
			print('-1')
			f = 1
			break
	if f == 0:
		if m < n:
			print('-1')
		else:
			print(2 * m - n)


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from copy import copy
from io import BytesIO, IOBase

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		t = 0
		th = 0
		while n % 2 == 0:
			n //= 2
			t += 1
		while n % 3 == 0:
			n //= 3
			th += 1
		if n != 1:
			print(-1)
		elif t > th:
			print(-1)
		elif th == t:
			print(th)
		else:
			ans = t + (th - t) * 2
			print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for k in range(int(input())):
	a = int(input())
	if a == 1:
		print(0)
	else:
		h = 0
		u = 0
		while a != 1:
			if a % 6 == 0:
				a //= 6
				u += 1
			else:
				a *= 2
				if a % 3 != 0:
					print(-1)
					h += 1
					break
				u += 1
		if h == 0:
			print(u)


t = int(input())

def solve(n):
	moves = 0
	curr = n
	while curr != 1:
		if curr % 3 != 0:
			return -1
		if curr % 2 == 0:
			curr = curr // 6
		else:
			curr *= 2
		moves += 1
	return moves
for _ in range(t):
	n = int(input())
	print(solve(n))


for _ in range(int(input())):
	n = int(input())
	z = n
	f = 0
	if n == 1:
		print(0)
	else:
		(c2, c3) = (0, 0)
		while n % 2 == 0:
			n //= 2
			c2 += 1
		while n % 3 == 0:
			n //= 3
			c3 += 1
		if c3 == c2:
			if z == pow(6, c3):
				print(c3)
			else:
				print(-1)
		elif c3 > c2:
			x = c3 - c2
			z *= pow(2, x)
			if z == pow(6, c3):
				print(x + c3)
			else:
				print(-1)
		else:
			print(-1)


for _ in range(int(input())):
	n = int(input())
	(ans, ans1, t) = (0, 0, n * 2)
	while n > 1 and t >= n:
		if n % 6 == 0:
			n = n // 6
			ans += 1
		else:
			n = n * 2
			ans1 += 1
	if n == 1:
		print(ans + ans1)
	else:
		print(-1)


m = {6: 1}
p = 6
for i in range(2, 1000):
	m[6 * p] = i
	p *= 6
s = [1, 2]
for i in range(1000):
	s.append(2 * s[-1])
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	ans = -1
	for j in range(0, 100):
		if n * s[j] % 6 == 0:
			if n * s[j] in m:
				ans = max(ans, m[n * s[j]] + j)
	print(ans)


def m2d6(n):
	e2 = 0
	e3 = 0
	while n % 2 == 0:
		n = n // 2
		e2 += 1
	while n % 3 == 0:
		n = n // 3
		e3 += 1
	if e2 > e3 or n > 1:
		return -1
	return (e3 << 1) - e2
t = int(input())
for _ in range(t):
	n = int(input())
	print(m2d6(n))


t = int(input())
for _ in range(t):
	n = int(input())
	(cnt2, cnt3) = (0, 0)
	while n % 2 == 0:
		cnt2 += 1
		n //= 2
	while n % 3 == 0:
		cnt3 += 1
		n //= 3
	if n != 1:
		print(-1)
	elif cnt2 > cnt3:
		print(-1)
	else:
		print(2 * cnt3 - cnt2)


def prime(n):
	for i in range(2, n):
		if n % i == 0:
			return False
	return True

def hi_th(y):
	count = 0
	while y % 3 == 0:
		count = count + 1
		y = y / 3
	return count

def hi_tw(z):
	c = 0
	while z % 2 == 0:
		c = c + 1
		z = z / 2
	return c
T = int(input())
for _ in range(1, T + 1):
	N = int(input())
	if N / (3 ** hi_th(N) * 2 ** hi_tw(N)) == 1:
		if hi_th(N) >= hi_tw(N):
			print(hi_th(N) - hi_tw(N) + hi_th(N))
		else:
			print(-1)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		two += 1
		n /= 2
	while n % 3 == 0:
		three += 1
		n /= 3
	if n > 1:
		print(-1)
	elif two > three:
		print(-1)
	else:
		print(three + three - two)


t = int(input())
for _ in range(t):
	n = int(input())
	n3 = n2 = 0
	while n % 3 == 0:
		n /= 3
		n3 += 1
	while n % 2 == 0:
		n /= 2
		n2 += 1
	result = 2 * n3 - n2
	if n != 1 or n3 < n2:
		result = -1
	print(result)


t = int(input())
while t:
	t -= 1
	n = int(input())
	if n == 1:
		print(0)
	else:
		count = 0
		flag = 0
		while flag == 0:
			if n % 6 == 0:
				count += 1
				n = n // 6
			elif n % 6 == 3:
				count += 1
				n = n * 2
			else:
				if n == 1:
					print(count)
				elif n == 3:
					print(count + 2)
				else:
					print(-1)
				flag = 1


import collections
import itertools
import sys
from collections import defaultdict, Counter
from math import sqrt, ceil
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def ds(d):
	return int(str(d).replace('0', ''))

def main():
	n = inp()
	for _ in range(n):
		d = inp()
		c = 0
		while True:
			if d % 6 == 0:
				d //= 6
				c += 1
			elif d % 3 == 0:
				d //= 3
				c += 2
			else:
				break
		if d == 1:
			print(c)
		else:
			print(-1)
main()


import math
for _ in range(int(input())):
	n = int(input())
	c = 0
	while n % 3 == 0:
		n = n / 3
		c += 1
	x = math.log(int(n), 2)
	if x == int(x):
		if x > c:
			print(-1)
		elif x == c:
			print(c)
		else:
			print(int(2 * c - x))
	else:
		print(-1)


n = int(input())

def judge(x):
	n3 = n2 = 0
	while x % 3 == 0:
		x //= 3
		n3 += 1
	while x % 2 == 0:
		x //= 2
		n2 += 1
	if x != 1 or n2 > n3:
		return -1
	else:
		return 2 * n3 - n2
for i in range(n):
	print(judge(int(input())))


t = int(input())
for _ in range(t):
	n = int(input())
	(q2, q3) = (0, 0)
	while n % 2 == 0 and n > 0:
		n //= 2
		q2 += 1
	while n % 3 == 0 and n > 0:
		n //= 3
		q3 += 1
	if n != 1 or q2 > q3:
		print(-1)
	else:
		ans = 2 * q3 - q2
		print(ans)


for _ in range(int(input())):
	n = int(input())
	(s3, s2, a) = (0, 0, 0)
	while n % 3 == 0:
		s3 += 1
		n /= 3
	while n % 2 == 0:
		s2 += 1
		n /= 2
	if n == 1:
		if s3 >= s2:
			a += s3 + s3 - s2
			print(a)
		else:
			print(-1)
	else:
		print(-1)


import math
for _ in range(int(input())):
	n = int(input())
	a3 = n
	a2 = n
	th = vv = count = 0
	while True:
		if a3 % 3 == 0:
			th += 1
			a3 /= 3
		else:
			break
	while True:
		if a2 % 2 == 0:
			vv += 1
			a2 /= 2
		else:
			break
	if th < vv or 3 ** th * 2 ** vv != n:
		print(-1)
	else:
		print(2 * th - vv)


t = int(input())
for i in range(t):
	c = 0
	num = int(input())
	while num % 3 == 0:
		if num % 6 == 0:
			num = num // 6
		else:
			num *= 2
		c += 1
	if num == 1:
		print(c)
	else:
		print(-1)


ll = lambda : list(map(int, input().split()))
st = lambda : input()
v = lambda : map(int, input().split())
ii = lambda : int(input())
from collections import *
from math import *
for _ in range(ii()):
	n = ii()
	c = 0
	z = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			c += 1
		elif n * 2 % 6 == 0:
			n = n * 2
			c = c + 1
		else:
			z = -1
			break
	if not z:
		print(c)
	else:
		print(-1)


def delim(n):
	ans = 0
	while n % 2 == 0:
		ans += 1
		n = n // 2
	return ans

def count(a):
	ans = 0
	while a % 3 == 0:
		ans += 1
		a /= 3
	return ans
t = int(input())
for i in range(t):
	a = int(input())
	if a == 1:
		print(0)
	elif a % 3 == 0:
		ans = count(a)
		much = delim(a)
		if ans < much:
			print(-1)
		else:
			h = a
			h *= 2 ** (ans - much)
			for j in range(ans):
				h /= 6
			if h == 1 or h == 1.0:
				print(ans + ans - much)
			else:
				print(-1)
	else:
		print(-1)


z = input
from math import ceil, log, gcd
n1 = int(z())
for _ in range(n1):
	n = int(z())
	i = 0
	c = 0
	if n == 1:
		print(0)
		continue
	while True:
		if n % 6 == 0:
			i += 1
			n = n // 6
			c = 0
		elif n % 6 != 0:
			n = n * 2
			c += 1
			i += 1
		if c == 10 and n != 1:
			print(-1)
			break
		if n == 1:
			print(i)
			break


for i in range(int(input())):
	n = int(input())
	s = 0
	p = 0
	sol = 0
	if n == 1:
		print(0)
	else:
		while n % 3 == 0:
			s = s + 1
			n = n // 3
		while n % 2 == 0:
			p = p + 1
			n = n // 2
		if n > 1:
			sol = -1
		elif p > s:
			sol = -1
		else:
			sol = s + (s - p)
		print(sol)


for i in range(int(input())):
	n = int(input())
	three = 0
	two = 0
	while n % 2 == 0:
		n /= 2
		two += 1
	while n % 3 == 0:
		n /= 3
		three += 1
	y = two
	x = three
	ans = 0
	if y <= x:
		ans += x - y
		ans += x
	else:
		ans = -1
	if n != 1:
		ans = -1
	print(ans)


def main():
	for t in range(int(input())):
		x = int(input())
		ansFound = False
		moves = 0
		while x > 1:
			if x % 6 == 0:
				x = x // 6
				moves += 1
			elif x % 3 != 0:
				print(-1)
				ansFound = True
				break
			else:
				x *= 2
				moves += 1
		if not ansFound:
			print(moves)
main()


def read_int():
	n = int(input())
	return n

def read_ints():
	ints = [int(x) for x in input().split(' ')]
	return ints

def solve(n):
	if n == 1:
		return 0
	moves = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			moves += 1
		elif n * 2 % 6 == 0:
			n *= 2
			moves += 1
		else:
			break
	if n != 1:
		return -1
	else:
		return moves
t = read_int()
for case in range(t):
	n = read_int()
	sol = solve(n)
	print(sol)


n1 = int(input())
for x in range(n1):
	n2 = int(input())
	count = 0
	while n2 > 1:
		if n2 % 6 == 0:
			n2 = n2 / 6
			count = count + 1
		elif n2 % 2 != 0:
			n2 = n2 * 2
			count = count + 1
		else:
			n2 = 0
			count = -1
	print(count)


t = int(input())
while t:
	count = 0
	f = 0
	n = int(input())
	while True:
		if n == 1:
			break
		if n < 1 or n % 3 != 0:
			f = 1
			break
		if n % 6 == 0:
			n = n / 6
			count += 1
		else:
			n = n * 2
			count += 1
	if f:
		print(-1)
	else:
		print(count)
	t = t - 1


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())
while t:
	t -= 1
	n = int(fastinput())
	if n == 1:
		print(0)
	else:
		cpunt6 = 0
		count3 = 0
		while n % 6 == 0:
			n //= 6
			cpunt6 += 1
		while n % 3 == 0:
			n //= 3
			count3 += 1
		if n != 1:
			print(-1)
		else:
			print(cpunt6 + count3 * 2)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
dgnl = lambda x: sqrt(2 * x ** 2)
for _ in range(I()):
	n = I()
	cnt = 0
	while n % 3 == 0:
		if n & 1:
			n *= 2
		else:
			n //= 6
		cnt += 1
	if n == 1:
		print(cnt)
	else:
		print(-1)


for _ in range(int(input())):
	x = int(input())
	out = 0
	while x != 1 and x % 6 == 0:
		x //= 6
		out += 1
	while x != 1 and x % 3 == 0:
		x //= 3
		out += 2
	if x != 1:
		print(-1)
	else:
		print(out)


def minSteps(n):
	if n == 1:
		return 0
	thr = 0
	two = 0
	while n % 3 == 0:
		thr += 1
		n = n // 3
	while n % 2 == 0:
		two += 1
		n = n // 2
	ans = thr - two + thr
	if n != 1 or two > thr or ans < 1:
		return -1
	return ans
T = int(input())
for i in range(T):
	n = int(input())
	print(minSteps(n))


import math

class Solution:

	def solve(self, n):
		cnt_2 = 0
		cnt_3 = 0
		while n > 1:
			if n % 2 == 0:
				n = n // 2
				cnt_2 += 1
				continue
			if n % 3 == 0:
				n = n // 3
				cnt_3 += 1
				continue
			return -1
		if cnt_2 > cnt_3:
			return -1
		return (cnt_3 - cnt_2) * 2 + cnt_2
for _ in range(int(input())):
	n = int(input())
	ans = Solution().solve(n)
	print(ans)


def solve():
	n = int(input())
	num_2 = 0
	num_3 = 0
	while n % 2 == 0:
		n /= 2
		num_2 += 1
	while n % 3 == 0:
		n /= 3
		num_3 += 1
	if n > 1 or num_2 > num_3:
		print(-1)
	else:
		max_value = max(num_2, num_3)
		min_value = min(num_2, num_3)
		print(max_value - min_value + max_value)
num_test = int(input())
for _ in range(num_test):
	solve()


import math as mt

def tran(s):
	n = int(s)
	c = 0
	while mt.floor(n) == mt.ceil(n):
		c = c + 1
		n = n / 2
	c = c - 1
	n = n * 2
	p = mt.log10(n) / mt.log10(3)
	if mt.floor(p) == mt.ceil(p):
		if c <= p:
			return int(2 * p - c)
		else:
			return -1
	else:
		return -1
t = int(input())
ans = []
for i in range(t):
	s = int(input())
	r = tran(s)
	ans.append(r)
for i in ans:
	print(i)


s = int(input())
for i in range(0, s):
	x = int(input())
	s = 0
	while True:
		if x == 1:
			print(s)
			break
		n = x / 6
		if n != int(x / 6):
			m = x * 2 / 6
			if m != int(x * 2 / 6):
				print(-1)
				break
			else:
				x = int(x * 2 / 6)
				s += 2
		else:
			x = int(n)
			s += 1


t = int(input())
for q in range(t):
	n = int(input())
	i = 2
	k_2 = 0
	k_3 = 0
	while n % i == 0:
		k_2 += 1
		n = n // i
	i = 3
	while n % i == 0:
		k_3 += 1
		n = n // i
	if n == 1 and k_3 > k_2:
		print(2 * k_3 - k_2)
	elif n == 1 and k_2 == k_3:
		print(k_3)
	else:
		print(-1)


def dl(n, a):
	for i in range(n):
		if n / a ** i == n // a ** i:
			continue
		else:
			return int(i - 1)
t = int(input())
l = []
for i in range(t):
	n = int(input())
	if n == 1:
		l.append(0)
	else:
		two = dl(n, 2)
		six = dl(n, 3)
		if six == 0:
			l.append(-1)
		elif 2 ** two * 3 ** six == n:
			if two > six:
				l.append(-1)
			else:
				l.append(2 * six - two)
		else:
			l.append(-1)
for i in l:
	print(i)


def power(v, m):
	result = 0
	while v % m == 0:
		v = v // m
		result += 1
	return (v, result)
for _ in range(int(input())):
	v = int(input())
	(v, p2) = power(v, 2)
	(v, p3) = power(v, 3)
	if v > 1 or p2 > p3:
		print(-1)
	else:
		print(2 * p3 - p2)


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		k1 = 0
		k2 = 0
		ans = 0
		while n % 3 == 0 or n % 2 == 0:
			ans += 1
			if n % 3 == 0:
				n = n / 3
				k1 += 1
			elif n % 2 == 0:
				n = n / 2
				k2 += 1
		if k1 >= k2 and n == 1:
			print(k1 + (k1 - k2))
		else:
			print(-1)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	d = 0
	t = 0
	while not n % 2:
		n //= 2
		d += 1
	while n % 3 == 0:
		n //= 3
		t += 1
	if n != 1 or d > t:
		print(-1)
	else:
		print(t + (t - d))


for case in range(int(input())):
	n = int(input())
	twos = 0
	threes = 0
	while n % 2 == 0 and n > 0:
		twos += 1
		n //= 2
	while n % 3 == 0 and n > 0:
		threes += 1
		n //= 3
	if n > 1 or twos > threes:
		print(-1)
		continue
	print(threes + threes - twos)


def solution(x):
	cnt = 0
	current = 1
	while True:
		if current < x:
			current *= 6
		elif current > x:
			if current % 2 == 0:
				current /= 2
			else:
				print('-1')
				return
		else:
			print(cnt)
			return
		cnt += 1
inp = int(input())
for _ in range(inp):
	x = int(input())
	solution(x)


_MULTITEST = True

def solve():
	n = int(input())
	factor3 = 0
	factor2 = 0
	while True:
		if n % 3 == 0:
			factor3 += 1
			n /= 3
			continue
		if n % 2 == 0:
			factor2 += 1
			n /= 2
			continue
		if n == 1:
			break
		else:
			print(-1)
			return
	print(factor3 - factor2 + factor3 if factor3 >= factor2 else -1)
t = int(input()) if _MULTITEST else 1
for tt in range(t):
	solve()


n = int(input())
for i in range(n):
	s = int(input())
	j = 0
	while s > 1 and s % 3 == 0:
		if s % 6 == 0:
			s /= 6
		else:
			s *= 2
		j += 1
	if s == 1:
		print(j)
	else:
		print(-1)


def FindCount2(n):
	count3 = 0
	count2 = 0
	while n % 3 == 0:
		count3 += 1
		n //= 3
	while n % 2 == 0:
		count2 += 1
		n //= 2
	if n != 1 or count2 > count3:
		return -1
	return count2 + 2 * (count3 - count2)

def FindCount(n):
	count6 = 0
	count3 = 0
	while n % 6 == 0:
		count6 += 1
		n //= 6
	while n % 3 == 0:
		count3 += 1
		n //= 3
	return -1 if n != 1 else count6 + 2 * count3
for _ in range(int(input())):
	print(FindCount(int(input())))


from collections import defaultdict
from math import ceil

def prime_factors(n):
	ret = defaultdict(int)
	for i in {2, 3}:
		while n > 1 and n % i == 0:
			ret[i] += 1
			n //= i
	if n != 1:
		raise ValueError
	return ret
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		try:
			f = prime_factors(n)
		except:
			print(-1)
			continue
		k = set(f.keys())
		if k != {2, 3} and k != {2} and (k != {3}):
			print(-1)
		elif f[2] > f[3]:
			print(-1)
		else:
			steps = 0
			while True:
				if f[2] == f[3]:
					print(steps + f[2])
					break
				elif f[2] < f[3]:
					steps += f[3] - f[2]
					f[2] += f[3] - f[2]


for _ in range(int(input())):
	x = int(input())
	ans = 0
	tc = 0
	thc = 0
	while x % 6 == 0:
		ans += 1
		x //= 6
	if x > 1:
		while x % 2 == 0:
			tc += 1
			x //= 2
		while x % 3 == 0:
			thc += 1
			x //= 3
		if thc < tc or x > 1:
			print(-1)
		else:
			d = thc - tc
			ans += thc + d
			print(ans)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	x = n
	count = 0
	ans = -1
	while x >= 6:
		if x % 6 == 0:
			x = x // 6
			count += 1
		else:
			break
	if x == 1:
		ans = count
	else:
		while x >= 3:
			if x % 3 == 0:
				x = x // 3
				count += 2
			else:
				break
		if x == 1:
			ans = count
	print(ans)


import bisect
import sys
import math
input = sys.stdin.readline
import functools
from collections import defaultdict

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def solve(case):
	n = inp()
	three = 0
	two = 0
	while n % 3 == 0:
		n = n // 3
		three += 1
	while n % 2 == 0:
		n = n // 2
		two += 1
	if n != 1 or two > three:
		return -1
	return three + (three - two)
if len(sys.argv) > 1 and sys.argv[1].startswith('input'):
	f = open('./' + sys.argv[1], 'r')
	input = f.readline
T = inp()
for i in range(T):
	res = solve(i + 1)
	print(str(res))


for t in range(int(input())):
	n = int(input())
	c = 0
	while n % 6 == 0:
		n = n // 6
		c += 1
	while n % 3 == 0:
		n = n // 3
		c += 2
	if n == 1:
		print(c)
	else:
		print(-1)


def sample(n):
	if n == 1:
		return 0
	k = n
	if k % 2 == 0 and k % 3 != 0:
		return -1
	cnt = 0
	while k > 1:
		if k % 6 == 0:
			k = k // 6
		else:
			k = k * 2
		if k % 2 == 0 and k % 3 != 0:
			return -1
		cnt += 1
	return cnt
t = int(input())
for p in range(t):
	n = int(input())
	print(sample(n))


t = int(input())
for T in range(t):
	a = int(input())
	c = 0
	while a > 1 and a % 3 == 0:
		if a % 6 == 0:
			a //= 6
		else:
			a *= 2
		c += 1
	if a == 1:
		print(c)
	else:
		print(-1)


def getMinSteps(num):
	if num == 1:
		return 0
	count = 0
	flag = 0
	while num != 1:
		while num % 6 == 0:
			num = num // 6
			count += 1
			flag = 0
		if num % 6 != 0 and num != 1:
			num = num * 2
			count += 1
			flag += 1
		if flag == 3:
			return -1
	return count

def rmain():
	q = int(input())
	for _ in range(q):
		num = int(input())
		print(getMinSteps(num))
rmain()


n = int(input())
for i in range(n):
	m = int(input())
	count_3 = 0
	count_2 = 0
	p = m
	while p % 3 == 0 or p % 2 == 0:
		if p / 3 == p // 3:
			count_3 += 1
			p = p / 3
		elif p / 2 == p // 2:
			count_2 += 1
			p = p / 2
	if count_3 >= count_2:
		k = count_3 - count_2
		if m * 2 ** k / 6 ** count_3 == 1:
			print(k + count_3)
		else:
			print(-1)
	else:
		print(-1)


line = input()
for c in range(1, int(line) + 1):
	no = int(input())
	ind = 0
	flg = True
	while no != 1:
		if no % 3 != 0:
			print(-1)
			flg = False
			break
		else:
			if no % 6 == 0:
				no /= 6
			else:
				no *= 2
			ind += 1
	if flg:
		print(ind)


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	d = {2: 0, 3: 0}
	tmp = n
	while tmp % 2 == 0:
		tmp = tmp // 2
		d[2] += 1
	while tmp % 3 == 0:
		tmp = tmp // 3
		d[3] += 1
	if 2 ** d[2] * 3 ** d[3] != n:
		print(-1)
		continue
	elif d[2] > d[3]:
		print(-1)
		continue
	else:
		print(d[3] + d[3] - d[2])


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	count = 0
	ex = 0
	if n == 1:
		stdout.write('0\n')
	else:
		while n > 1:
			if n % 6 == 0:
				n = n // 6
				count += 1
			elif n % 3 != 0:
				stdout.write('-1\n')
				ex = 1
				break
			else:
				n *= 2
				count += 1
		if ex == 0:
			stdout.write(str(count) + '\n')


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
n1 = int(z())
for _ in range(n1):
	n = int(z())
	i = 0
	c = 0
	if n == 1:
		print(0)
		continue
	while True:
		if n % 6 == 0:
			i += 1
			n = n // 6
			c = 0
		elif n % 6 != 0:
			n = n * 2
			c += 1
			i += 1
		if c == 10 and n != 1:
			print(-1)
			break
		if n == 1:
			print(i)
			break


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	n2 = 0
	n3 = 0
	while n % 2 == 0:
		n2 += 1
		n //= 2
	while n % 3 == 0:
		n3 += 1
		n //= 3
	if n != 1 or n2 > n3:
		print(-1)
	else:
		ans = n2
		n3 -= n2
		ans += n3 * 2
		print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	(count, count2, flag) = (0, 0, True)
	while True:
		if n == 1:
			break
		if n % 6 == 0:
			n //= 6
			count += 1
			count2 = 0
		else:
			n *= 2
			count += 1
			count2 += 1
		if n == 1:
			break
		if count2 > 2:
			flag = False
			break
	if flag:
		print(count)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	(a, b) = (0, 0)
	while True:
		if n % 2 == 0:
			a += 1
			n = n // 2
		elif n % 3 == 0:
			b += 1
			n = n // 3
		else:
			break
	if n != 1 or a > b:
		print(-1)
	else:
		print(b + max(b - a, 0))


t = int(input())
for _ in range(t):
	n = int(input())
	count = 0
	while n != 1:
		if not (n % 6 == 0 or n % 6 == 3):
			count = -1
			break
		if n % 6 != 0:
			n *= 2
			count += 1
		else:
			n /= 6
			count += 1
	print(count)


import sys
(t, *N) = map(int, sys.stdin.read().split())
for n in N:
	if n == 1:
		print(0)
		continue
	cnt3 = 0
	while n % 3 == 0:
		cnt3 += 1
		n //= 3
	if cnt3 == 0 and n % 2 == 1:
		print(-1)
		continue
	cnt2 = 0
	while n % 2 == 0:
		cnt2 += 1
		n //= 2
	if cnt2 <= cnt3 and n == 1:
		print(cnt3 - cnt2 + cnt3)
	else:
		print(-1)


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	if n == 1:
		print(0)
	else:
		boo = True
		x = 0
		y = 0
		while n != 1:
			if n % 6 == 0:
				x += 1
				n //= 6
			elif n % 3 == 0:
				y += 1
				n //= 3
			else:
				boo = False
				break
		if boo == False:
			print(-1)
		else:
			print(x + y * 2)


t = int(input())
list_ans = []
dict1 = {1: 0, 3: 1, 9: 2, 27: 3, 81: 4, 243: 5, 729: 6, 2187: 7, 6561: 8, 19683: 9, 59049: 10, 177147: 11, 531441: 12, 1594323: 13, 4782969: 14, 14348907: 15, 43046721: 16, 129140163: 17, 387420489: 18}
for i in range(t):
	x = int(input())
	a = 0
	while x % 2 == 0:
		x = x // 2
		a += 1
	if x in dict1:
		if a <= dict1[x]:
			list_ans.append(dict1[x] * 2 - a)
		else:
			list_ans.append(-1)
	else:
		list_ans.append(-1)
for item in list_ans:
	print(item)


T = int(input())
for tc in range(1, T + 1):
	n = int(input())
	if n == 1:
		print(0)
		continue
	cntTwo = 0
	cntThree = 0
	flag = 0
	while n != 1:
		if not n % 2:
			n = n / 2
			cntTwo += 1
		elif not n % 3:
			n = n / 3
			cntThree += 1
		else:
			flag = 1
			break
	if flag == 1 or cntTwo > cntThree:
		print(-1)
		continue
	print(2 * cntThree - cntTwo)


import math
from collections import deque

def inpt():
	return int(input())

def inpl():
	return list(map(int, input().split()))

def inpm():
	return map(int, input().split())

def solve():
	n = inpt()
	x = 0
	y = 0
	while n % 2 == 0:
		n = n / 2
		x += 1
	while n % 3 == 0:
		n = n / 3
		y += 1
	if n > 1:
		print(-1)
		return
	if x > y:
		print(-1)
		return
	else:
		print(2 * y - x)

def main():
	m = 10 ** 9 + 7
	t = int(input())
	while t:
		t -= 1
		solve()
main()


def check(n):
	ans = 0
	while True:
		if n == 1:
			return ans
		elif n % 3 != 0:
			return -1
		elif n % 6 == 0:
			n //= 6
			ans += 1
		else:
			n *= 2
			ans += 1
	return ans
t = int(input())
while t:
	n = int(input())
	print(check(n))
	t -= 1


import math
testcases = int(input())
for i in range(testcases):
	n = int(input())
	count = 0
	steps = 0
	if n == 1:
		print(0)
	else:
		while n != 1:
			if count == 2:
				print(-1)
				steps = 0
				break
			if n % 6 == 0:
				n = int(n / 6)
				count = 0
			else:
				n = n * 2
				count += 1
			steps += 1
		if steps != 0:
			print(steps)


for _ in range(int(input())):
	n = int(input())
	pp = False
	if n == 1:
		print(0)
		pp = True
	else:
		count = 0
		while n != 1:
			count += 1
			if n % 6 == 0:
				n = n // 6
			elif n % 3 != 0:
				print(-1)
				pp = True
				break
			else:
				n = n * 2
	if pp == False:
		print(count)


INT_MAX = 10 ** 30 + 7
MOD = 10 ** 9 + 7

def INPUT():
	return list((int(i) for i in input().split()))

def LIST_1D_ARRAY(n):
	return [0 for _ in range(n)]

def LIST_2D_ARRAY(m, n):
	return [[0 for _ in range(n)] for _ in range(m)]
for i in range(int(input())):
	n = int(input())
	x = 0
	y = 0
	while n % 2 == 0:
		n //= 2
		x += 1
	while n % 3 == 0:
		n //= 3
		y += 1
	if n == 1:
		if x == 0 and y == 0:
			print(0)
		elif x != 0 and y == 0:
			print(-1)
		elif x == 0 and y != 0:
			print(2 * y)
		elif x != 0 and y != 0:
			if x > y:
				print(-1)
			elif x == y:
				print(x)
			else:
				print(x + (y - x) * 2)
	else:
		print(-1)


t = int(input())

def ans(n):
	x = 0
	y = 0
	while True:
		if n % 3 == 0:
			n = n / 3
			x += 1
		elif n % 2 == 0:
			n = n / 2
			y += 1
		else:
			break
	if x < y or n != 1:
		return -1
	else:
		return 2 * x - y
li = []
for i in range(t):
	li = li + [int(input())]
for j in li:
	print(ans(j))


t = int(input())
s = ''
for i in range(1, t + 1):
	n = int(input())
	a = 0
	b = 0
	while n % 6 == 0:
		a += 1
		n //= 6
	while n % 3 == 0:
		b += 1
		n //= 3
	if n == 1:
		s += str(a + 2 * b) + '\n'
	else:
		s += '-1\n'
print(s)


def f(n):
	i = 6
	c = 1
	while i <= n:
		if i == n:
			return c
		else:
			i = i * 6
			c = c + 1
	return -1

def g(n):
	i = 2
	c = 0
	while n % 2 == 0:
		n = n // 2
		c = c + 1
	d = 0
	while n % 3 == 0:
		n = n // 3
		d = d + 1
	if n != 1:
		return -1
	else:
		return [d, c]
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	if n == 1:
		print(0)
	elif f(n) != -1:
		print(f(n))
	elif g(n) == -1:
		print(-1)
	else:
		ls = g(n)
		if ls[0] < ls[1]:
			print(-1)
		else:
			ans = ls[0] + (ls[0] - ls[1])
			print(ans)


t = int(input())
for i in range(t):
	m = n = int(input())
	if n == 1:
		print(0)
	else:
		a = 0
		while True:
			if m % 3 == 0:
				m = m // 3
				a += 1
			else:
				break
		if a > 0:
			import math
			b = math.log(n // 3 ** a, 2)
			if (b % 1 == 0 or b == 0) and a >= b:
				print(2 * a - int(b))
			else:
				print(-1)
		else:
			print(-1)


import sys
sys.setrecursionlimit(10 ** 8)
test_cases = int(input())

def check(n, count):
	if n == 1:
		print(count)
	elif n % 3 == 0:
		count += 1
		if n % 2 == 0:
			check(n // 6, count)
		else:
			check(n * 2, count)
	else:
		print(-1)
for i in range(test_cases):
	n = int(input())
	check(n, 0)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		c1 = 0
		while n % 2 == 0:
			n //= 2
			c1 += 1
		c2 = 0
		while n % 3 == 0:
			n //= 3
			c2 += 1
		if n != 1:
			print(-1)
		elif c1 > c2:
			print(-1)
		else:
			print(c2 - c1 + c2)


for _ in range(int(input())):
	n = int(input())
	pow3 = 0
	pow2 = 0
	if n == 1:
		print(0)
		continue
	while n % 3 == 0 and n > 0:
		n = n / 3
		pow3 += 1
	while n % 2 == 0 and n > 0:
		n = n / 2
		pow2 += 1
	if pow3 == 0 or pow2 > pow3 or n > 1:
		print(-1)
	else:
		print(pow3 - pow2 + pow3)


for i in range(int(input())):
	n = int(input())
	k1 = 0
	k2 = 0
	while n % 2 == 0 or n % 3 == 0:
		if n % 3 == 0:
			n //= 3
			k2 += 1
		if n % 2 == 0:
			n //= 2
			k1 += 1
	if k1 > k2 or n != 1:
		print(-1)
	else:
		print(k2 - k1 + k2)


import math
t = int(input())
for i in range(t):
	n = int(input())
	k = n
	f6 = 0
	f3 = 0
	f = 0
	while k > 1:
		if k % 6 == 0:
			f6 += 1
			k = k / 6
		else:
			break
	while k > 1:
		if k % 3 == 0:
			f3 += 1
			k = k / 3
		else:
			f = 1
			break
	if f == 1:
		print(-1)
	elif n == 1:
		print(0)
	else:
		print(f6 + 2 * f3)


import sys, math
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	a = int(input())
	if a == 1:
		print(0)
	else:
		ans = 0
		if a != 3:
			while True:
				if a % 6 == 0:
					a //= 6
					ans += 1
				elif a % 3 == 0:
					a *= 2
					ans += 1
				else:
					ans = -1
					break
				if a == 1:
					break
			print(ans)
		else:
			print(2)


for i in range(int(input())):
	n = int(input())
	c = 0
	while n > 2:
		if n * 2 % 6 == 0 or n % 6 == 0:
			if n % 6 == 0:
				c = c + 1
				n = n // 6
			else:
				c = c + 2
				n = n * 2
				n = n // 6
		else:
			break
	if n == 1:
		print(c)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	m = 0
	fla = 0
	if n == 1:
		print(0)
		fla = 8
	while n >= 1:
		if n % 6 == 0:
			m += 1
			n //= 6
		elif n % 3 == 0:
			n *= 2
			n //= 6
			m += 2
		else:
			break
	if n > 1:
		print(-1)
	elif fla == 0:
		print(m)


for _ in range(int(input())):
	n = int(input())
	y = 0
	x = 0
	z = 0
	answer = -1
	while n % 3 == 0:
		y += 1
		n = n // 3
	while n % 2 == 0:
		z += 1
		n = n // 2
	if n != 1:
		print(answer)
	elif z > y:
		print(answer)
	else:
		x = y - z
		print(x + y)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print('0')
	elif n % 3 != 0:
		print('-1')
	else:
		ans = 0
		c = 0
		while n > 1 and c < 2:
			if n % 6 == 0:
				n = n / 6
				ans += 1
				c = 0
			else:
				n = n * 2
				ans += 1
				c += 1
		if n == 1:
			print(ans)
		else:
			print('-1')


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = []
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors.append(i)
			n //= i
		i += 1
	if n > 1:
		factors.append(n)
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	n = int(input())
	(c3, c2) = (0, 0)
	while n % 3 == 0:
		c3 += 1
		n //= 3
	while n % 2 == 0:
		c2 += 1
		n //= 2
	if c2 > c3 or n > 1:
		print(-1)
	else:
		print(c3 + (c3 - c2))
t = int(input())
for _ in range(t):
	main()


from sys import *
for _ in range(int(stdin.readline())):
	x = int(stdin.readline())
	n = 0
	n1 = 0
	while x % 2 == 0:
		x /= 2
		n1 += 1
	while x % 3 == 0:
		x /= 3
		n += 1
	if x != 1 or n1 > n:
		stdout.write(str(-1) + '\n')
	else:
		stdout.write(str(n + n - n1) + '\n')


T = int(input())
for _ in range(T):
	n = int(input())
	ans = 0
	flag = 0
	while n != 1:
		if n % 6 == 0:
			n = int(n / 6)
			ans += 1
		elif n % 3 == 0:
			n *= 2
			ans += 1
		else:
			flag = 1
			break
	if flag == 1:
		print('-1')
	else:
		print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 == 0:
		(pow2, pow3) = (0, 0)
		while n % 2 == 0:
			pow2 += 1
			n = n // 2
		while n % 3 == 0:
			pow3 += 1
			n = n // 3
		if n == 1:
			if pow2 <= pow3:
				print(pow3 - pow2 + pow3)
			else:
				print(-1)
		else:
			print(-1)
	else:
		print(-1)


k = int(input())
out = ''
for i in range(0, k):
	n2 = 0
	n3 = 0
	n = int(input())
	while True:
		if not n % 3:
			n /= 3
			n3 += 1
		else:
			break
	while True:
		if not n % 2:
			n /= 2
			n2 += 1
		else:
			break
	if n == 1 and n3 >= n2:
		out += str(2 * n3 - n2) + '\n'
	else:
		out += '-1\n'
print(out)


import math
import os
import random
import re
import sys
import collections

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n and i <= 3:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return factors

def solve():
	for _ in range(int(input())):
		n = int(input())
		if n == 1:
			print(0)
			continue
		else:
			l = prime_factors(n)
			c = dict(collections.Counter(l))
			if 2 not in c and 3 not in c:
				print(-1)
			elif 2 in c and 3 not in c:
				print(-1)
			elif 3 in c and 2 not in c:
				n = n * 2 ** c[3]
				n = n // 6 ** c[3]
				if n == 1:
					print(c[3] * 2)
				else:
					print(-1)
			elif c[2] > c[3]:
				print(-1)
			else:
				n = n * 2 ** (c[3] - c[2])
				n = n // 6 ** c[3]
				if n == 1:
					print(c[3] - c[2] + c[3])
				else:
					print(-1)
solve()


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		two = True
		three = True
		twocount = 0
		threecount = 0
		q = n
		while two or three:
			if q % 2 == 0:
				twocount += 1
				q /= 2
			else:
				two = False
			if q % 3 == 0:
				threecount += 1
				q /= 3
			else:
				three = False
		if q != 1:
			print(-1)
		elif twocount > threecount:
			print(-1)
		else:
			print(threecount - twocount + threecount)


t = int(input())
a = [''] * t
e = [''] * t
for i in range(0, t):
	a[i] = int(input())
for i in range(0, t):
	q = 1
	c = 0
	while q != 3 and a[i] != 1:
		if a[i] % 6 == 0:
			a[i] //= 6
			q = 1
			c += 1
		else:
			a[i] *= 2
			q += 1
			c += 1
	if a[i] == 1:
		e[i] = c
	else:
		e[i] = -1
for i in range(0, t):
	print(e[i])


m = int(input())
while m:
	m -= 1
	n = int(input())
	(p, o) = (0, 0)
	while n % 2 == 0:
		p += 1
		n = n // 2
	while n % 3 == 0:
		o += 1
		n = n // 3
	if p > o or n != 1:
		print(-1)
	else:
		print(min(p, o) + 2 * (max(o, p) - min(p, o)))


cases = int(input())
for case in range(cases):
	result = 0
	number = int(input())
	if number == 1:
		result = 0
	elif number % 3 != 0:
		result = -1
	else:
		while number != 1:
			if number % 3 != 0:
				result = -1
				break
			elif number % 6 == 0:
				number //= 6
				result += 1
			else:
				number *= 2
				result += 1
	print(result)


t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	d = 0
	if n == 1:
		print('0')
	else:
		while n > 1:
			if n % 6 == 0:
				n = n / 6
				c += 1
			else:
				n *= 2
				d += 1
			if c + 5 < d:
				break
		if n == 1:
			print(c + d)
		else:
			print('-1')


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	c = 0
	while n % 3 == 0:
		c += 1
		n //= 3
	c2 = 0
	while n % 2 == 0:
		c2 += 1
		n //= 2
	if n == 1 and c >= c2:
		print(c + (c - c2))
	else:
		print(-1)


from sys import stdin

def main():
	input = lambda : stdin.readline()[:-1]
	T = int(input())
	for _ in [0] * T:
		N = int(input())
		if N == 1:
			print(0)
			continue
		ans = 1
		while 1:
			n = sum(map(int, list(str(N))))
			if n % 3:
				print(-1)
				break
			(d, m) = divmod(N, 6)
			if d == 1 and (not m):
				print(ans)
				break
			elif not m:
				N = d
			else:
				N *= 2
			ans += 1
main()


for t in range(int(input())):
	n = int(input())
	d2 = d3 = 0
	while n % 3 == 0:
		d3 += 1
		n = n // 3
	while n % 2 == 0:
		d2 += 1
		n = n // 2
	if n != 1 or d2 > d3:
		print('-1')
	else:
		print(2 * d3 - d2)


t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	while n != 1:
		if abs(n / 6 - 1 / 3 - round(n / 6)) <= 1e-10 or abs(n / 6 + 1 / 3 - round(n / 6)) <= 1e-10:
			print(-1)
			break
		if n % 6 == 0:
			n = n // 6
		else:
			n *= 2
		count += 1
	if n == 1:
		print(count)


def is_pw(x):
	d = 1
	while d < x:
		d *= 3
	return d == x
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		k1 = 0
		k2 = 0
		while n % 2 == 0:
			k1 += 1
			n //= 2
		while n % 3 == 0:
			k2 += 1
			n //= 3
		if n == 1 and k1 <= k2:
			print(k2 * 2 - k1)
		else:
			print(-1)


for i in range(int(input())):
	n = int(input())
	n1 = n
	count = 0
	if n == 1:
		print(0)
		continue
	else:
		while n1 > 1:
			if n1 % 6 == 0:
				n1 = int(n1 / 6)
				count += 1
			elif n1 % 6 == 3:
				n1 = n1 * 2
				count += 1
			else:
				print(-1)
				break
	if n1 == 1:
		print(count)


import time, math, bisect, sys
sys.setrecursionlimit(1000)
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x))

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count

def readTree():
	n = II()
	adj = [set() for i in range(n + 1)]
	for i in range(n - 1):
		(u1, u2) = IP()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return adj

def bfs(adj, n, s, d, points):
	visited = [False] * (n + 1)
	q = deque()
	q.append(s)
	visited[s] = True
	parent = [0] * (n + 1)
	parent[s] = s
	while q:
		ele = q.popleft()
		for k in adj[ele]:
			if visited[k] == False:
				visited[k] = True
				q.append(k)
				parent[k] = ele
	win = 0
	for ele in d:
		if d[ele] == parent[ele]:
			win += 1
	if win >= points:
		return True
	return False
mx = 10 ** 9 + 7

def solve():
	n = II()
	mov = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			mov += 1
		elif n % 3 == 0:
			n = n * 2
			mov += 1
		else:
			print(-1)
			return
	else:
		print(mov)
t = int(input())
for i in range(t):
	solve()


import math
t = int(input())
for i in range(t):
	n = int(input())
	sum = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			sum = sum + 1
		elif n % 3 == 0:
			n = n * 2
			sum = sum + 1
		else:
			break
	if sum > 0 and n == 1:
		print(sum)
	elif n == 1 and sum == 0:
		print('0')
	else:
		print('-1')


from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')

def solve():
	for _ in range(ii()):
		n = ii()
		(c1, c2) = (0, 0)
		while n % 2 == 0:
			n //= 2
			c1 += 1
		while n % 3 == 0:
			n //= 3
			c2 += 1
		if n > 1 or c1 > c2:
			print('-1')
		else:
			print(c2 + (c2 - c1))
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
solve()


for _ in range(int(input())):
	n = int(input())
	(tw, th) = (0, 0)
	while n % 3 == 0:
		n //= 3
		th += 1
	while n % 2 == 0:
		n //= 2
		tw += 1
	if th >= tw and n == 1:
		print(2 * th - tw)
	else:
		print(-1)


from math import *
l = int(input())
lo = []
res = 0
reso = []
for i in range(l):
	x = int(input())
	if x % 3 != 0:
		if x == 1:
			res = 0
		else:
			res = -1
	else:
		(j, k) = (0, 0)
		while x == floor(x):
			x = x / 3
			j += 1
		x = x * 3
		while x == floor(x):
			x = x / 2
			k += 1
		x = x * 2
		if x == 1:
			if j >= k:
				res = 2 * (j - 1) - (k - 1)
			else:
				res = -1
		else:
			res = -1
	reso.append(res)
for i in reso:
	print(i)


testcase = int(input())
for test in range(testcase):
	res = 0
	num = int(input())
	while True:
		if num == 1:
			break
		elif num % 6 == 0:
			num = num / 6
			res += 1
		elif num * 2 % 6 == 0:
			num = num * 2
			res += 1
		else:
			res = -1
			break
	print(res)


def divide_by_3(a):
	b = 0
	while a % 3 == 0:
		a = a / 3
		b += 1
	return (a, b)

def divide_by_2(a):
	b = 0
	while a % 2 == 0:
		a = a / 2
		b += 1
	return (a, b)
t = int(input())
while t > 0:
	n = int(input())
	if n % 3 != 0 and n != 1:
		print(-1)
	else:
		(n, a) = divide_by_3(n)
		(n, b) = divide_by_2(n)
		if n == 1:
			if b > a:
				print(-1)
			elif a == b:
				print(a)
			else:
				print(a - b + a)
		else:
			print(-1)
	t = t - 1


t = int(input())
c = []
for i in range(t):
	n = int(input())
	p = 0
	q = 0
	while n % 2 == 0:
		p += 1
		n = n // 2
	while n % 3 == 0:
		q += 1
		n = n // 3
	if n != 1:
		c.append(-1)
	elif p > q:
		c.append(-1)
	else:
		c.append(2 * q - p)
for k in c:
	print(k)


def is3(x):
	if x % 3 == 0:
		return True
	return False
test = int(input())
for t in range(test):
	n = int(input())
	if n == 1:
		print(0)
	else:
		ans = 0
		flag = 1
		while n != 1:
			if n % 6 == 0:
				n //= 6
			elif is3(n):
				n *= 2
			else:
				flag = 0
				break
			ans += 1
		if flag == 0:
			print(-1)
		else:
			print(ans)


for i in range(int(input())):
	n = int(input())
	c2 = c3 = 0
	while n % 2 == 0:
		n = n / 2
		c2 += 1
	while n % 3 == 0:
		n = n / 3
		c3 += 1
	if n == 1 and c2 <= c3:
		print(2 * c3 - c2)
	else:
		print(-1)


def get_input_list() -> str:
	n = int(input())
	raw_input_data = str(n) + '\n'
	for _ in range(n):
		raw_input_data += input() + '\n'
	return raw_input_data

def get_input() -> str:
	raw_input_data = ''
	raw_input_data += input()
	return raw_input_data

def multiply_by_two_divide_by_six(n, steps=0):
	if n == 1:
		return steps
	elif int(n % 6) == 0:
		n = int(n / 6)
		return multiply_by_two_divide_by_six(n, steps + 1)
	elif int(2 * n % 6) == 0:
		n = int(n * 2)
		return multiply_by_two_divide_by_six(n, steps + 1)
	else:
		return -1

def solve(raw_input_data: str):
	lines = [int(n) for n in raw_input_data.split()]
	answer = ''
	for n in lines[1:]:
		answer += str(multiply_by_two_divide_by_six(n)) + '\n'
	return str(answer)
while True:
	try:
		input_data = get_input_list()
		answer = solve(input_data)
		print(answer, end='\n')
	except:
		break


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 6 != 0 and n % 3 != 0:
		print(-1)
		continue
	count = 0
	flag = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
		elif n % 3 == 0:
			n = n * 2
		else:
			flag = 1
			break
		count += 1
	if flag:
		print(-1)
	else:
		print(count)


def factorization_by_three(num):
	cnt = 0
	while num % 3 == 0:
		num = num / 3
		cnt = cnt + 1
	return (cnt, num)

def factorization_by_two(num):
	cnt = 0
	while num % 2 == 0:
		num = num / 2
		cnt = cnt + 1
	x = factorization_by_three(num)
	cnt3 = x[0]
	n = x[1]
	return (cnt, cnt3, n)

def main():
	test_case = int(input())
	while test_case != 0:
		num = int(input())
		no = factorization_by_two(num)
		cnt2 = no[0]
		cnt3 = no[1]
		n = no[2]
		if n == 1 and cnt2 <= cnt3:
			print(cnt3 - cnt2 + cnt3)
		else:
			print(-1)
		test_case = test_case - 1
main()


t = int(input())
while t > 0:
	n = int(input())
	c = 0
	if n == 1:
		print(0)
	else:
		while n > 0:
			if n % 6 == 0:
				n //= 6
				c += 1
			elif n % 6 >= 3:
				n *= 2
				c += 1
			else:
				break
		if n == 1:
			print(c)
		else:
			print(-1)
	t -= 1


import math
total = int(input())
for i in range(0, total):
	number = int(input())
	if number == 1:
		print(0)
	elif number % 3 != 0:
		print(-1)
	else:
		moves = 0
		while number % 6 == 0:
			number = number / 6
			moves += 1
		if number == 1:
			print(moves)
		elif 1162261467 % number == 0:
			print(moves + round(math.log(number) / math.log(3)) * 2)
		else:
			print(-1)


from sys import stdin, stdout

def input():
	return stdin.readline().strip()

def ans(n):
	count2 = 0
	count3 = 0
	while n % 2 == 0:
		n = n // 2
		count2 += 1
	while n % 3 == 0:
		n = n // 3
		count3 += 1
	if n > 1:
		return -1
	if count2 > count3:
		return -1
	return count3 - count2 + count3
	return '?'
T = int(input())
for _ in range(T):
	n = int(input())
	print(ans(n))


from sys import stdin, stdout
from math import log2
test = int(stdin.readline())
for _ in range(test):
	N = int(stdin.readline())
	if N == 1:
		print(0)
		continue
	if N % 3:
		print(-1)
		continue
	cnt2 = 0
	cnt3 = 0
	while N & 1 ^ 1:
		N = N // 2
		cnt2 += 1
	while not N % 3:
		N = N // 3
		cnt3 += 1
	if N != 1 or cnt2 > cnt3:
		print(-1)
		continue
	diff = cnt3 - cnt2
	ans = diff + cnt3
	print(ans)


def FindCount(n):
	count3 = 0
	while n % 3 == 0:
		count3 += 1
		n //= 3
	count2 = 0
	while n % 2 == 0:
		count2 += 1
		n //= 2
	if n != 1 or count2 > count3:
		return -1
	return count2 + 2 * (count3 - count2)
FindCount(3)
for _ in range(int(input())):
	print(FindCount(int(input())))


for n in [*open(0)][1:]:
	n = int(n)
	a = b = 0
	while n % 3 == 0:
		n //= 3
		a += 1
	while n % 2 == 0:
		n //= 2
		b += 1
	print(-1 if n > 1 or b > a else a * 2 - b)


NumberOfTestcases = int(input())
output = []
for i in range(NumberOfTestcases):
	currentTestcase = int(input())
	counter = 0
	appended = False
	if currentTestcase == 1:
		output.append(0)
		appended = True
	elif currentTestcase % 3 != 0:
		output.append(-1)
		appended = True
	while appended == False:
		counter += 1
		if currentTestcase % 2 == 0 and currentTestcase % 3 != 0:
			appended = True
			output.append(-1)
			break
		elif currentTestcase % 6 == 0:
			currentTestcase = currentTestcase // 6
			if currentTestcase == 1:
				break
			else:
				continue
		elif currentTestcase % 2 != 0 and currentTestcase % 3 == 0:
			currentTestcase = currentTestcase * 2
			continue
		elif currentTestcase % 2 != 0 and currentTestcase % 3 != 0:
			appended = True
			output.append(-1)
	if counter != 0 and appended == False:
		output.append(counter)
for i in output:
	print(i)


for i in range(int(input())):
	total = 0
	test = int(input())
	if test == 1:
		print('0')
	else:
		while True:
			if test % 6 == 0:
				test /= 6
				total += 1
			elif test % 3 == 0:
				test /= 3
				total += 2
			else:
				if test != 1:
					total = -1
				break
		print(total)


for tt in range(int(input())):
	n = int(input())
	flag = True
	ctx = 0
	while flag:
		if n == 1:
			break
		if n % 6 == 0:
			n /= 6
			ctx += 1
		elif n % 3 == 0:
			n *= 2
			ctx += 1
		else:
			flag = False
	print(ctx if flag else -1)


for _ in range(int(input())):
	n = int(input())
	ymx = 0
	y = 0
	if n == 1:
		print(0)
		continue
	while n % 2 == 0:
		n /= 2
		ymx += 1
	while n % 3 == 0:
		n /= 3
		y += 1
	if n == 1:
		x = y - ymx
		y = ymx + x
		if x >= 0 and y >= 0:
			print(x + y)
		else:
			print(-1)
	else:
		print(-1)


number = int(input())
for n in range(number):
	x = int(input())
	a = b = 0
	while not x % 2:
		x = x / 2
		a += 1
	while not x % 3:
		x = x / 3
		b += 1
	if x != 1 or a > b:
		print(-1)
	else:
		print(b * 2 - a)


import math
from collections import defaultdict

def f(n):
	num = n
	d = defaultdict(int)
	while n % 2 == 0:
		d[2] = d[2] + 1
		n = n / 2
	for i in range(3, 4):
		while n % i == 0:
			d[i] = d[i] + 1
			n = n / i
	tp = dict(d)
	if 2 ** d[2] * 3 ** d[3] != num:
		return -1
	for i in tp:
		if i not in [2, 3]:
			return -1
	return [d[2], d[3]]
for tc in range(int(input())):
	x = int(input())
	a = f(x)
	if type(a) == int:
		print(-1)
	elif a[0] > a[1]:
		print(-1)
	else:
		print(a[0] + 2 * (a[1] - a[0]))


t = int(input())
for q in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		ans = 0
		while n % 6 == 0:
			n //= 6
			ans += 1
		while n % 3 == 0:
			n //= 3
			ans += 2
		if n != 1:
			print(-1)
		else:
			print(ans)


import sys
input = sys.stdin.readline
from math import ceil
answer = []
for kek in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		two += 1
		n //= 2
	while n % 3 == 0:
		three += 1
		n //= 3
	if n != 1:
		answer.append(-1)
		continue
	if two > three:
		answer.append(-1)
		continue
	answer.append(three + three - two)
print(*answer, sep='\n')


import math

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		p3 = 0
		while n % 3 == 0:
			n //= 3
			p3 += 1
		p2 = n & -n
		if n != p2:
			print(-1)
			continue
		p2 = p2.bit_length() - 1
		if p2 > p3:
			print(-1)
			continue
		elif p2 == p3:
			print(p2)
		else:
			ans = p3 - p2 + p3
			print(ans)
FIO = True

def I():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
if FIO:
	from io import BytesIO, IOBase
	import sys
	import os
en = enumerate
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	cnt = 0
	ans = 0
	cp = n
	while n % 6 == 0:
		n = n // 6
		ans += 1
	while n % 3 == 0:
		n = n // 3
		cnt += 1
	while n % 2 == 0:
		n = n // 2
		cnt -= 1
		if cnt < 0:
			break
	if cnt < 0 or n != 1:
		print(-1)
		continue
	ans += 2 * cnt
	print(ans)


t = int(input())
for s in range(t):
	n = int(input())
	add = 0
	flag = 0
	while True:
		if n == 1:
			flag = 6
			break
		q = n % 3
		if q != 0:
			flag = 0
			break
		n = n // 3
		add += 1
	plus = 0
	if flag == 0:
		while True:
			if n == 1:
				flag = 6
				break
			q = n % 2
			if q != 0:
				flag = 0
				break
			plus += 1
			n = n // 2
		else:
			flag = 0
	if flag == 6 and add >= plus:
		sol = plus + (add - plus) * 2
		print(sol)
	else:
		print(-1)


from math import log

def takePower(x, b):
	ans = 0
	while x % b == 0:
		x = x // b
		ans += 1
	return ans
for i in range(int(input())):
	num = int(input())
	pow2 = takePower(num, 2)
	pow3 = takePower(num, 3)
	if pow(2, pow2) * pow(3, pow3) != num or pow2 > pow3:
		print(-1)
	else:
		print(2 * pow3 - pow2)


for t in range(int(input())):
	n = int(input())
	cnt = 0
	flag = 0
	while n > 9:
		if n % 6 == 0:
			n = n / 6
			cnt += 1
			flag = 0
		else:
			n = n * 2
			cnt += 1
			flag += 1
		if flag > 1:
			break
	if n == 9:
		cnt += 4
	elif n == 6:
		cnt += 1
	elif n == 3:
		cnt += 2
	elif n == 1:
		cnt = 0
	else:
		cnt = -1
	print(cnt)


import time

def solve(val):
	count = 0
	if val == 1:
		return 0
	if val <= 2:
		return -1
	while val != 1:
		if val % 6 == 0:
			val = int(val / 6)
			count += 1
		else:
			val = int(val * 2)
			count += 1
		if val == 1:
			return count
		if val == 0:
			return -1
		if val % 2 == 0 and val % 3 != 0:
			return -1
t = int(input())
values = []
for _ in range(t):
	values.append(int(input()))
for i in range(len(values)):
	print(solve(values[i]))


def main():
	t = int(input())
	for _ in range(t):
		x = int(input())
		z = x
		v1 = v2 = 0
		while x % 2 == 0:
			x /= 2
			v1 += 1
		while x % 3 == 0:
			x /= 3
			v2 += 1
		if v1 > v2 or x != 1:
			print('-1')
		else:
			print(v2 - v1 + v2)
main()


import sys
for _ in range(int(sys.stdin.readline().rstrip())):
	n = int(sys.stdin.readline().rstrip())
	two = 0
	three = 0
	re = 0
	while True:
		if n == 1:
			break
		if n % 2 != 0 and n % 3 != 0:
			re = -1
			break
		if n % 3 == 0:
			n = int(n / 3)
			three += 1
		elif n % 2 == 0:
			n = int(n / 2)
			two += 1
	if re != -1:
		if two > three:
			re = -1
		else:
			re = three - two + three
	print(re)


a = int(input())
while a:
	a -= 1
	n = int(input())
	if n == 1:
		print(0)
	else:
		k = 0
		while n % 6 == 0:
			n = n / 6
			k += 1
		while n % 3 == 0:
			n = n / 3
			k += 2
		if n == 1:
			print(k)
		else:
			print(-1)


def solve_2(m):
	cnt = 0
	if m == 1:
		return 0
	while True:
		if m >= 6 and m % 6 == 0:
			m = m // 6
			cnt += 1
			if m == 1:
				return cnt
			elif m == 6:
				return cnt + 1
			elif m < 6:
				m *= 2
				cnt += 1
				if m % 6 == 0:
					return cnt + 1
				else:
					return -1
		else:
			m *= 2
			cnt += 1
			if m % 6 != 0:
				return -1

def main():
	t = int(input())
	for i in range(t):
		print(solve_2(int(input())))
main()


n = int(input())
for i in range(n):
	a = int(input())
	(count1, count2) = (0, 0)
	while a % 2 == 0:
		count1 += 1
		a /= 2
	while a % 3 == 0:
		count2 += 1
		a /= 3
	if a == 1:
		if count1 <= count2:
			ans = (count2 - count1) * 2 + count1
			print(ans)
		else:
			print('-1')
	else:
		print('-1')


n = int(input())
for i in range(n):
	y = -1
	z = -1
	x = int(input())
	x1 = x
	x2 = x
	while int(x1) == x1:
		x1 = x1 / 2
		y += 1
	while int(x2) == x2:
		x2 = x2 / 3
		z += 1
	if x == 2 ** y * 3 ** z and y <= z:
		print(-y + 2 * z)
	else:
		print(-1)


t = int(input())
for i in range(t):
	a = int(input())
	d = 0
	while a > 1:
		if not a % 3 == 0:
			break
		else:
			if a % 6 == 0:
				a = a // 6
			else:
				a *= 2
			d += 1
	if a > 1:
		print(-1)
	else:
		print(d)


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	moves = 0
	if n != 1:
		f2 = 0
		f3 = 0
		while n % 3 == 0:
			f3 += 1
			n = n // 3
		while n % 2 == 0:
			f2 += 1
			n = n // 2
		if n > 1 or f3 == 0:
			moves = -1
		elif f2 == 0:
			moves += 2 * f3
		elif f3 < f2:
			moves = -1
		else:
			moves += 2 * f3 - f2
	print(moves)


for _ in range(int(input())):
	a = int(input())
	if a == 1:
		print(0)
	else:
		(v, r, t) = ([], 0, 0)
		while a % 2 == 0:
			r += 1
			a //= 2
		while a % 3 == 0:
			t += 1
			a //= 3
		if a > 1:
			print(-1)
		elif t < r:
			print(-1)
		else:
			print(min(t, r) + 2 * abs(t - r))


import math
import os
import random
import re
import sys
import heapq
import random
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	moves = 0
	while n % 3 == 0:
		if n % 6 == 0:
			n = n // 6
			moves += 1
		else:
			n = n // 3
			moves += 2
	if n == 1:
		print(moves)
	else:
		print(-1)


import sys

class BMultiplyBy2DivideBy6:

	def solve(self, tc=0):
		for _ in range(int(input())):
			n = int(input())
			two = 0
			while n % 2 == 0:
				n //= 2
				two += 1
			three = 0
			while n % 3 == 0:
				n //= 3
				three += 1
			if n != 1 or two > three:
				print(-1)
			else:
				print(2 * three - two)
solver = BMultiplyBy2DivideBy6()
input = sys.stdin.readline
solver.solve()


t = int(input())
for _ in range(t):
	n = int(input())
	(d2, d3) = (0, 0)
	while True:
		if not n % 2 == 0:
			break
		d2 += 1
		n //= 2
	while True:
		if not n % 3 == 0:
			break
		d3 += 1
		n //= 3
	ans = -1
	if n == 1:
		if d2 <= d3:
			ans = 2 * d3 - d2
	print(ans)


def divsix(n, count):
	if n == 1:
		return count
	elif not n % 3 == 0:
		return -1
	else:
		if n % 6 == 0:
			k = divsix(n / 6, count + 1)
		else:
			k = divsix(n * 2, count + 1)
		if k == -1:
			return -1
		else:
			return k
m = int(input())
for i in range(0, m):
	k = int(input())
	print(divsix(k, 0))


t = int(input())

def func(n, c, m):
	if n == 1:
		return c
	if n % 6 == 2 or n % 6 == 4 or n % 6 == 5 or (n % 6 == 7) or (n % 6 == 8) or (n % 6 == 9):
		return -1
	if c > 10000:
		return -1
	if n % 6 == 0:
		m = 1
		c += 1
		return func(n // 6, c, m)
		c += 1
		return func(n * 2, c, m)
	else:
		c += 1
		return func(n * 2, c, m)
for i in range(t):
	n = int(input())
	print(func(n, 0, 0))


t = int(input())
for _ in range(t):
	n = int(input())
	s = 0
	while n != 1:
		if n > 1000000000:
			s = -1
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		s += 1
	print(s)


for _ in range(int(input().strip())):
	n = int(input().strip())
	xn = n
	(cnt2, cnt3) = (0, 0)
	while n % 2 == 0:
		n = n / 2
		cnt2 += 1
	while n % 3 == 0:
		n = n / 3
		cnt3 += 1
	if int(n) == 1 and cnt2 <= cnt3:
		print(int(2 * cnt3 - cnt2))
	else:
		print(-1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	k = 0
	while n % 6 == 0:
		n = n // 6
		k += 1
	m = 0
	while n % 3 == 0:
		n = n // 3
		m += 1
	if n != 1:
		print(-1)
	else:
		print(k + 2 * m)


for _ in range(int(input())):
	n = int(input())
	num = n
	cnt1 = cnt2 = 0
	while num % 3 == 0:
		num //= 3
		cnt1 += 1
	while num % 2 == 0:
		num //= 2
		cnt2 += 1
	if num != 1 or cnt2 > cnt1:
		print('-1')
	else:
		print(cnt1 + cnt1 - cnt2)


for tc in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		result = 0
		while n != 1:
			flag = True
			while n % 6 == 0:
				flag = False
				n /= 6
				result += 1
			if n % 3 == 0:
				flag = False
				n *= 2
				result += 1
			if flag:
				print(-1)
				break
		if n == 1:
			print(result)


Test = int(input())
while Test > 0:
	n = int(input())
	ans = 0
	while n % 6 == 0:
		n /= 6
		ans += 1
	while n % 3 == 0:
		n /= 3
		ans += 2
	if n == 1:
		print(ans)
	else:
		print(-1)
	Test -= 1


for t in range(int(input())):
	n = int(input())
	(c2, c3) = (0, 0)
	while n % 2 == 0:
		c2 += 1
		n = n // 2
	while n % 3 == 0:
		c3 += 1
		n = n // 3
	if c2 > c3 or n != 1:
		print(-1)
	else:
		print(c3 - c2 + c3)


for x in range(int(input())):
	n = int(input())
	i = 0
	j = 0
	while n % 2 == 0:
		i += 1
		n = n // 2
	while n % 3 == 0:
		j += 1
		n = n // 3
	print(-1 if n != 1 or i > j else 2 * j - i)


N = int(input())
for i in range(N):
	t = int(input())
	if t == 1:
		output = 0
	count = 0
	while t != 1:
		while t % 6 == 0:
			count += 1
			t /= 6
		if t == 1:
			output = count
			break
		if t % 6 == 1 or t % 6 == 2 or t % 6 == 4 or (t % 6 == 5):
			output = -1
			break
		if t % 6 == 3:
			t *= 2
			count += 1
	print(output)


for i in range(int(input())):
	num = int(input())
	if num == 1:
		print(0)
		continue
	n2 = 0
	n3 = 0
	while num % 2 == 0:
		n2 += 1
		num //= 2
	while num % 3 == 0:
		n3 += 1
		num //= 3
	if num != 1 or n2 > n3:
		print(-1)
	else:
		print(n3 - n2 + n3)


import sys
input = sys.stdin.readline

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()

def lower_letters():
	lowercase = [chr(i) for i in range(97, 97 + 26)]
	return lowercase

def how_frequent(lis):
	last = None
	count = []
	each = 1
	for i in range(len(lis)):
		if last != None:
			last = lis[i - 1]
		if last == lis[i]:
			each += 1
		else:
			count.append(each)
			each = 0
		last = lis[i]
	return max(count)

def upper_letters():
	uppercase = [chr(i) for i in range(65, 65 + 26)]
	return uppercase
for _ in range(int(input())):
	n = int(input())
	count = 0
	while True:
		if n == 1:
			print(count)
			break
		if n % 6 == 0:
			n = n / 6
		else:
			if n % 3 != 0:
				print(-1)
				break
			n *= 2
		count += 1


t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	f = 0
	while n != 1:
		if n % 6 == 0:
			n = n / 6
			c += 1
		elif n * 2 % 6 == 0:
			n = n * 2
			c += 1
		else:
			f = 1
			print(-1)
			break
	if f == 0:
		print(c)


from math import log

def turns(n):
	if n == 1:
		return 0
	l = log(n, 3)
	if l.is_integer():
		return round(l) * 2
	l = log(n, 6)
	if l.is_integer():
		return round(l)
	counter = 0
	while n % 6 == 0:
		n //= 6
		counter += 1
	while n % 3 == 0:
		n //= 3
		counter += 2
	if n == 1:
		return counter
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(turns(n))


import sys
read = lambda : sys.stdin.readline()
T = int(read())
for t in range(T):
	n = int(read())
	n3 = 0
	while n % 3 == 0:
		n = n // 3
		n3 += 1
	n2 = 0
	while n % 2 == 0:
		n = n // 2
		n2 += 1
	if n != 1 or n2 > n3:
		print(-1)
		continue
	print(n3 + (n3 - n2))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	k = 0
	ans = 0
	while n != 1:
		if n % 6 == 0:
			n = int(n / 6)
			ans += 1
		elif int(n * 2) % 6 == 0:
			n = n * 2
			ans += 1
		else:
			k = 1
			break
	if k == 1:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	t = int(input())
	cnt = 0
	while t != 1:
		if t % 3 == 0 and t % 2 == 0:
			t = t // 6
			cnt += 1
		elif t % 3 == 0 and t % 2 != 0:
			t *= 2
			cnt += 1
		else:
			print(-1)
			break
	else:
		print(cnt)


t = int(input())
for qwe in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		ans = 0
		while True:
			ans += 1
			if n % 6 == 0:
				n //= 6
				if n == 1:
					break
			else:
				n *= 2
				if n % 6 != 0:
					ans = -1
					break
		print(ans)


for _ in range(int(input())):
	n = int(input())
	(a2, a3) = (0, 0)
	while n % 3 == 0:
		a3 += 1
		n = n // 3
	while n % 2 == 0:
		a2 += 1
		n = n // 2
	if n > 1 or a2 > a3:
		print(-1)
	else:
		k = a3 - a2
		a2 = a2 + k
		k = k + a3
		print(k)


t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	flag = 0
	while n > 1:
		if n % 6 == 0:
			n = n / 6
			count += 1
		elif n * 2 % 6 == 0:
			n = n * 2
			count += 1
		else:
			flag = 1
			break
	if flag == 0:
		print(count)
	else:
		print(-1)


for test_case in range(int(input())):
	num = int(input())
	flag = True
	counter = 1
	if num == 1:
		print(0)
	else:
		while True:
			if num % 6 == 0:
				num = num / 6
				flag = True
			elif flag:
				num = num * 2
				flag = False
			else:
				print(-1)
				break
			if num == 1:
				print(counter)
				break
			counter += 1


import math
for _ in range(int(input())):
	n = int(input())
	i = 0
	b = n
	while b % 6 == 0:
		b = b / 6
		i = i + 1
	if math.ceil(math.log(n) / math.log(3)) - math.log(n) / math.log(3) <= 10 ** (-12):
		print(int(2 * math.ceil(math.log(n) / math.log(3))))
	elif math.ceil(math.log(b) / math.log(3)) - math.log(b) / math.log(3) <= 10 ** (-12):
		print(int(i + 2 * math.ceil(math.log(b) / math.log(3))))
	else:
		print(-1)


from sys import stdin, stdout
from math import floor, ceil
t = stdin.readline()

def moves(n):
	tmp = n
	seen = set()
	steps = 0
	while tmp not in seen and tmp % 3 == 0:
		seen.add(tmp)
		if tmp % 6 == 0:
			steps += 1
			tmp //= 6
		else:
			steps += 1
			tmp *= 2
	if tmp == 1:
		return steps
	else:
		return -1
for line in stdin:
	n = int(line)
	stdout.write(str(moves(n)) + '\n')


import math
for _ in range(int(input())):
	n = int(input())
	count1 = count2 = 0
	while n % 3 == 0:
		n //= 3
		count1 += 1
	while n % 2 == 0:
		n //= 2
		count2 += 1
	if n != 1 or count1 < count2:
		print(-1)
	else:
		print(count1 * 2 - count2)


def f(x, t):
	if x == 1:
		return t
	elif x % 6 == 0:
		t += 1
		x = x // 6
		return f(x, t)
	elif x % 3 == 0 and x % 2 != 0:
		x = x * 2
		t += 1
		return f(x, t)
	else:
		return -1
for _ in range(int(input())):
	x = int(input())
	if x == 1:
		print(0)
	elif x == 2 or x % 3 != 0:
		print(-1)
	else:
		print(f(x, 0))


t = int(input())
for i in range(t):
	n = int(input())
	exp2 = exp3 = 0
	while n % 2 == 0:
		exp2 += 1
		n //= 2
	while n % 3 == 0:
		exp3 += 1
		n //= 3
	if n == 1 and exp2 <= exp3:
		print(2 * exp3 - exp2)
	else:
		print(-1)


t = int(input())
for i in range(t):
	x = int(input())
	cnt = 0
	while x % 6 == 0:
		x = x / 6
		cnt += 1
	while x % 3 == 0:
		x = x / 3
		cnt += 2
	print([-1, cnt][x == 1])


dic = {}
j = 6
k = 1
while k < 22:
	dic[j] = k
	j = j * 6
	k += 1
for i in range(int(input())):
	n = int(input())
	nn = n + 1 - 1
	j = 0
	ans = 0
	while j < 35 and dic.get(n, 0) == 0:
		ans += 1
		j += 1
		n *= 2
	if dic.get(n, 0) > 0:
		ans += dic[n]
	else:
		ans = -1
	if nn == 1:
		ans = 0
	print(ans)


a = int(input())
for i in range(a):
	b = int(input())
	s2 = 0
	s3 = 0
	while b % 2 == 0 and b > 0:
		s2 += 1
		b = b // 2
	while b % 3 == 0 and b > 0:
		s3 += 1
		b = b // 3
	if b == 1:
		if s2 < s3:
			x = s3 - s2
			y = max(s3, s2)
			print(y + x)
		elif s2 == s3:
			print(s2)
		else:
			print('-1')
	else:
		print('-1')


from math import *

def _in():
	return [int(x) for x in input().split(' ')]
tt = int(input())
for _ in range(tt):
	n = int(input())
	(n2, n3, ans) = (0, 0, 0)
	while n % 2 == 0:
		n2 = n2 + 1
		n = n // 2
	while n % 3 == 0:
		n3 = n3 + 1
		n = n // 3
	if n2 > n3 or n > 1:
		print(-1)
	else:
		ans = n3 - n2 + n3
		print(ans)


test_cases = int(input())
for _ in range(test_cases):
	steps = 0
	n = int(input())
	while n % 3 == 0:
		n = n / 3
		steps += 1
		if n % 2 == 0:
			n = n / 2
		else:
			steps += 1
	if n != 1:
		print(-1)
	else:
		print(steps)


t = int(input())
for _ in range(t):
	n = int(input())
	twos = 0
	while n % 2 == 0:
		n /= 2
		twos += 1
	tres = 0
	while n % 3 == 0:
		n /= 3
		tres += 1
	if n != 1:
		print(-1)
		continue
	if twos > tres:
		print(-1)
		continue
	print(2 * tres - twos)


def solve(a):
	count2 = 0
	count3 = 0
	while a % 2 == 0:
		count2 += 1
		a //= 2
	while a % 3 == 0:
		count3 += 1
		a //= 3
	if a != 1:
		return -1
	y = count3
	x = y - count2
	if x >= 0 and y >= 0:
		return x + y
	else:
		return -1
testCases = int(input())
for case in range(testCases):
	n = int(input())
	print(solve(n))


for _ in range(int(input())):
	n = int(input())
	ct2 = 0
	while n % 2 == 0:
		ct2 += 1
		n = n // 2
	ct3 = 0
	while n % 3 == 0:
		ct3 += 1
		n = n // 3
	if n != 1 or ct2 > ct3:
		print(-1)
	else:
		we = ct3 - ct2
		print(ct3 + we)


from sys import stdin
import math
A = int(stdin.readline())
for t in range(0, A):
	B = int(stdin.readline())
	count2 = 0
	count3 = 0
	while B % 2 == 0:
		B = int(B / 2)
		count2 += 1
	while B % 3 == 0:
		B = int(B / 3)
		count3 += 1
	if B != 1:
		print(-1)
	elif count3 >= count2:
		print(2 * count3 - count2)
	else:
		print(-1)


n = int(input())

def f(a):
	res = 0
	while a % 6 == 0:
		a /= 6
		res += 1
	while a % 3 == 0:
		a /= 3
		res += 2
	if a == 1:
		return res
	else:
		return -1
for i in range(n):
	num = int(input())
	print(f(num))


mas = []
for i in range(int(input())):
	n = int(input())
	kol = 0
	ekzemp = n
	while n > 1 and n < ekzemp ** 2:
		if n % 6 == 0:
			n = n // 6
			kol += 1
		else:
			n = n * 2
			kol += 1
	if n == 1:
		mas.append(kol)
	else:
		mas.append(-1)
for element in mas:
	print(element)


for i in range(int(input())):
	n = int(input())
	c = 0
	flag = True
	while n > 1:
		if n % 6 == 0:
			n //= 6
			c += 1
		elif n % 3 == 0:
			n *= 2
			c += 1
		else:
			flag = False
			break
	if flag:
		print(c)
	else:
		print('-1')


def fun1(n):
	i = 0
	while True:
		if n == 1:
			return i
		elif n % 6 == 0:
			n = n / 6
			i += 1
		elif n % 3 == 0:
			n = n * 2
			i += 1
		else:
			return -1
t = int(input())
for i in range(t):
	n = int(input())
	if n >= 1:
		ans = fun1(n)
		print(ans)
	else:
		print('Invalid Input')


import sys, math

def file():
	sys.stdin = open('input.py', 'r')
	sys.stdout = open('output.py', 'w')

def get_array():
	l = list(map(int, input().split()))
	return l

def main():
	for tt in range(int(input())):
		n = int(input())
		(two, thr) = (0, 0)
		while n % 2 == 0:
			n = n // 2
			two += 1
		while n % 3 == 0:
			n = n // 3
			thr += 1
		if n != 1:
			print(-1)
		elif two > thr:
			print(-1)
		else:
			ans = thr
			ans += thr - two
			print(ans)
main()


def func(n):
	for i in range(n):
		x = int(input())
		counter = 0
		for i in range(1, 50):
			if x == 1:
				break
			if x % 6 == 0:
				x //= 6
				counter += 1
			else:
				x *= 2
				counter += 1
		if i == counter:
			print(-1)
		else:
			print(counter)
n = int(input())
func(n)


import math
for i1 in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	three = 0
	two = 0
	t1 = n
	t2 = n
	while t1 % 3 == 0:
		three += 1
		t1 = t1 // 3
	while t2 % 2 == 0:
		two += 1
		t2 = t2 // 2
	check = 3 ** three * 2 ** two
	if check != n:
		print(-1)
		continue
	elif three == two:
		print(three)
		continue
	elif three > two:
		ans = three - two
		print(three + ans)
		continue
	else:
		print(-1)
		continue


t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	if n == 1:
		print(c)
		continue
	while n % 3 == 0:
		if n % 6 == 0:
			n = n / 6
			c += 1
		else:
			n += n
			c += 1
		if n == 1:
			print(c)
			break
	else:
		print(-1)


for testcase in range(int(input())):
	n = int(input())
	(cnt2, cnt3) = (0, 0)
	while n % 2 == 0:
		n //= 2
		cnt2 += 1
	while n % 3 == 0:
		n //= 3
		cnt3 += 1
	if n > 1 or cnt3 < cnt2:
		print(-1)
		continue
	print(2 * cnt3 - cnt2)


for i in range(int(input())):
	n = int(input())
	times = 0
	canDo = True
	while n != 1:
		if n % 6 == 0:
			n = n // 6
		elif n % 3 == 0:
			n *= 2
		else:
			canDo = False
			break
		times += 1
	if canDo:
		print(times)
	else:
		print('-1')


def Fact(x):
	cntof2 = 0
	cntof3 = 0
	while x % 2 == 0:
		cntof2 += 1
		x //= 2
	while x % 3 == 0:
		cntof3 += 1
		x //= 3
	if x != 1 or cntof2 > cntof3:
		return -1
	if cntof2 == cntof3:
		return cntof2
	return cntof3 - cntof2 + cntof3
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	if n == 1:
		ans += [0]
	else:
		ans += [Fact(n)]
print(*ans, sep='\n')


for o in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		c = 0
		f = 1
		while n != 1:
			if n % 6 == 0:
				n = n // 6
				c = c + 1
			else:
				n = n * 2
				c = c + 1
				if n % 6 != 0:
					f = 0
					break
		if f == 1:
			print(c)
		else:
			print(-1)


k = int(input())
while k != 0:
	t = int(input())
	n = 0
	m = 0
	while t % 3 == 0:
		t = t / 3
		n = n + 1
	while t % 2 == 0:
		t = t / 2
		m = m + 1
	if n >= m and t == 1:
		print(2 * n - m)
	else:
		print(-1)
	k = k - 1


for i in range(int(input())):
	n = int(input())
	total = 0
	while n > 1:
		if n % 3 == 0:
			if n % 2 == 0:
				n = n / 6
				total += 1
			else:
				n = n * 2
				total += 1
		else:
			total = -1
			break
	print(total)


from sys import stdin, stdout
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
S = lambda : input()
import math
from collections import Counter
for _ in range(IN()):
	n = IN()
	ans = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			ans += 1
		elif n % 3 == 0:
			n *= 2
			ans += 1
		else:
			break
	if n == 1:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	p = 1
	c = 0
	f = 0
	while True:
		if n == 1:
			f = -1
			print(c)
			break
		elif n % 6 == 0:
			n = n // 6
			c += 1
		elif n % 3 == 0:
			n = n * 2
			c += 1
		else:
			f = -1
			print(-1)
			break
	if f != -1:
		print(c)


def run():
	import sys
	sys.stdin = open('/home/punit/Documents/Cpp Programs/input.txt', 'r')
	sys.stdout = open('/home/punit/Documents/Cpp Programs/output.txt', 'w')
	from math import ceil, sqrt, floor
import math
for _ in range(int(input())):
	n = int(input())
	count1 = 0
	count2 = 0
	while n > 1:
		if n % 6 == 0:
			n /= 6
			count1 += 1
		else:
			count2 += 1
			n *= 2
		if n % 3 != 0:
			flag = 1
			break
	if n == 1:
		print(count2 + count1)
	else:
		print(-1)


def check(n, c):
	if n == 1:
		print(c)
	elif n % 6 == 0:
		c += 1
		check(n // 6, c)
	elif n * 2 % 6 == 0:
		c += 1
		check(n * 2, c)
	else:
		print(-1)
for ii in range(int(input())):
	x = int(input())
	check(x, 0)


t = int(input())
for r in range(t):
	n = int(input())
	c = 0
	if n % 5 == 0:
		c = -1
	elif n == 1:
		c = 0
	elif n % 2 == 0 and n % 3 != 0:
		c = -1
	else:
		for i in range(n):
			if n % 2 == 0 and n % 3 == 0:
				n = n // 6
				c += 1
			elif n % 3 == 0:
				c += 1
				n = n * 2
			else:
				c = -1
				break
			if n == 1:
				break
	print(c)


def primefac(n):
	for i in [2, 3]:
		if n % i == 0:
			while n % i == 0:
				n //= i
				res[i] += 1
	if n > 1 or res[2] > res[3]:
		return False
	else:
		return True
for _ in range(int(input())):
	res = [0] * 4
	if primefac(int(input())):
		print(res[3] + (res[3] - res[2]))
	else:
		print(-1)


def ques2(n):
	cnt2 = cnt3 = 0
	while n % 2 == 0:
		n = n / 2
		cnt2 += 1
	while n % 3 == 0:
		n = n / 3
		cnt3 += 1
	if n == 1 and cnt3 >= cnt2:
		print(2 * cnt3 - cnt2)
	else:
		print(-1)
t = int(input())
for i in range(t):
	n = int(input())
	ans = ques2(n)
	ans


for i in range(int(input())):
	n = int(input())
	ct1 = 0
	while n % 2 == 0:
		ct1 += 1
		n = n // 2
	ct2 = 0
	while n % 3 == 0:
		ct2 += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif ct2 < ct1:
		print(-1)
	else:
		print(ct2 - ct1 + ct2)


import sys
input = sys.stdin.readline
from math import ceil, floor, factorial

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	n = int(input())
	ans = 0
	while n % 6 == 0:
		n //= 6
		ans += 1
	while n % 3 == 0:
		n //= 3
		ans += 2
	if n == 1:
		print(ans)
	else:
		print(-1)


for t in range(int(input())):
	n = int(input())
	count = 0
	check = 0
	while n > 1:
		if n % 6 == 0:
			n //= 6
			count += 1
			check = 0
		else:
			n *= 2
			count += 1
			if check == 1:
				count = -1
				break
			check = 1
	print(count)


t = int(input())
for T in range(t):
	num = int(input())
	ans = 0
	while num % 6 == 0:
		ans += 1
		num /= 6
	if num == 1:
		print(ans)
	else:
		n = m = 0
		while num % 2 == 0:
			n += 1
			num /= 2
		while num % 3 == 0:
			m += 1
			num /= 3
		if num != 1 or n > m:
			print(-1)
		else:
			print(ans + m - n + m)


def make_one(n):
	if n == 1:
		return 0
	else:
		moves = 0
		while n != 1:
			if n % 6 == 0:
				n /= 6
				moves += 1
			elif n % 6 in [2, 4, 5]:
				return -1
			else:
				n *= 2
				moves += 1
	return moves
t = int(input())
for t_itr in range(t):
	n = int(input())
	result = make_one(n)
	print(result)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		a = 0
		b = 0
		while n % 2 == 0:
			n = n // 2
			a += 1
		while n % 3 == 0:
			n = n // 3
			b += 1
		if n == 1:
			x = b - a
			y = b
			if x >= 0 and y >= 0:
				print(x + y)
			else:
				print(-1)
		else:
			print(-1)


for i in range(int(input())):
	a = int(input())
	j = 0
	while True:
		if a == 1:
			print(j)
			break
		elif a % 3 != 0:
			print(-1)
			break
		elif a % 6 == 0:
			a = a / 6
		else:
			a = a * 2
		j = j + 1


from sys import stdin, stdout
input = stdin.readline

def solve(cs):
	n = int(input())
	two = 0
	three = 0
	while n % 3 == 0:
		three += 1
		n /= 3
	while n % 2 == 0:
		two += 1
		n /= 2
	if n > 1:
		print(-1)
	elif two > three:
		print(-1)
	else:
		print(three + (three - two))
for tc in range(int(input())):
	solve(tc + 1)


def solve(n):
	c = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			c += 1
		elif n % 6 != 0 and n % 3 == 0:
			n = 2 * n
			c += 1
		elif n % 6 != 0 and n % 3 != 0:
			return -1
	return c
for _ in range(int(input())):
	n = int(input())
	print(solve(n))


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	elif n == 3:
		print(2)
		continue
	elif n == 2 or n == 4 or n == 5:
		print(-1)
		continue
	c = 0
	d = 0
	while 1:
		if n == 1:
			print(c + d)
			break
		if n % 6 == 0:
			n //= 6
			c += 1
		else:
			n *= 2
			d += 1
		if d - c > 2:
			print(-1)
			break


amountTests = int(input())
ans = ''
for x in range(amountTests):
	num = int(input())
	n = num
	twos = 0
	while n % 2 == 0:
		n /= 2
		twos += 1
	threes = 0
	while n % 3 == 0:
		n /= 3
		threes += 1
	if n != 1 or twos > threes:
		ans += '-1' + '\n'
		continue
	moves = threes - twos + threes
	ans += str(moves) + '\n'
print(ans)


def p3(x):
	p3c = 0
	while 1 > 0:
		if x % 3 == 0:
			x = x // 3
			p3c += 1
		else:
			break
	return p3c

def p2(x):
	p2c = 0
	while 1 > 0:
		if x % 2 == 0:
			x = x // 2
			p2c += 1
		else:
			break
	return p2c
t = int(input())
for i in range(t):
	n = int(input())
	po3 = p3(n)
	po2 = p2(n)
	if po3 < po2:
		print(-1)
	else:
		reduced = n / (3 ** po3 * 2 ** po2)
		if reduced != 1:
			print(-1)
		else:
			print(2 * po3 - po2)


t = int(input())

def min_xodlar_soni(n):
	k2 = 0
	k3 = 0
	while n % 2 == 0:
		n = n // 2
		k2 += 1
	while n % 3 == 0:
		n = n // 3
		k3 += 1
	if k2 > k3 or n != 1:
		return -1
	else:
		return k3 - k2 + k3
natija = []
for i in range(t):
	n = int(input())
	natija.append(min_xodlar_soni(n))
for i in range(t):
	print(natija[i])


L = int(input())

def diveq(n):
	count = 0
	while n > 1:
		if n == 1:
			return 0
		if n % 6 == 0:
			n //= 6
			count += 1
		else:
			n *= 2
			count += 1
		if n > 10 ** 9:
			return -1
	return count
for i in range(L):
	n = int(input())
	ans = diveq(n)
	print(ans)


import os
from io import BytesIO, IOBase
import sys

def main():
	for _ in range(int(input())):
		n = int(input())
		c1 = 0
		while not n & 1:
			c1 += 1
			n >>= 1
		c2 = 0
		while n % 3 == 0:
			c2 += 1
			n //= 3
		if n != 1:
			print('-1')
		else:
			print(c1 + (c2 - c1) * 2 if c1 <= c2 else '-1')
	return
BUFSIZE = 1048576

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


for i in range(int(input())):
	(n, q, w) = (int(input()), 0, 0)
	while n % 2 == 0:
		n //= 2
		q += 1
	while n % 3 == 0:
		n //= 3
		w += 1
	print(2 * w - q if n == 1 and w >= q else -1)


def solve1():
	n = int(input())
	cnt = 0
	while n > 1:
		if n % 3 != 0:
			print(-1)
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		cnt += 1
	else:
		print(cnt)
for _ in range(int(input())):
	solve1()


n = int(input())
while n:
	targ = int(input())
	i = 0
	while targ % 3 == 0:
		if targ % 2:
			targ *= 2
		else:
			targ /= 6
		i += 1
	if targ != 1:
		print(-1)
	else:
		print(i)
	n -= 1


for _ in range(int(input())):
	value = int(input())
	count = 0
	while True:
		if value % 2 != 0 and value != 1:
			value = value * 2
			count += 1
		elif value % 6 == 0:
			value = value / 6
			count += 1
		else:
			if value == 1:
				print(count)
			else:
				print(-1)
			break


def f(x, ans):
	if x % 6 == 0:
		return [x // 6, ans + 1]
	if x % 3 == 0:
		return [2 * x // 6, ans + 2]
	return -1
for i in range(int(input())):
	z = int(input())
	p = -234
	ans = 0
	while 1:
		if z == 1:
			print(0)
			break
		res = f(z, ans)
		if res == -1:
			print(-1)
			break
		elif res[0] == 1:
			print(res[1])
			break
		else:
			ans = res[1]
			z = res[0]


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
sys.setrecursionlimit(int(pow(10, 6)))
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
t = 1
t = int(input())
for _ in range(t):
	n = l()[0]
	if n == 1:
		print(0)
	else:
		c2 = c3 = 0
		while n % 3 == 0:
			c3 += 1
			n //= 3
		while n % 2 == 0:
			c2 += 1
			n //= 2
		if n != 1:
			print(-1)
			continue
		elif c3 < c2:
			print(-1)
			continue
		else:
			print(c3 + (c3 - c2))


for i in range(int(input())):
	n = int(input())
	(cnt2, cnt3) = (0, 0)
	while n % 2 == 0:
		n //= 2
		cnt2 += 1
	while n % 3 == 0:
		n //= 3
		cnt3 += 1
	if n == 1 and cnt2 <= cnt3:
		print(2 * cnt3 - cnt2)
	else:
		print(-1)


import math
from collections import defaultdict

def primeFactors(num):
	lis = []
	while num % 2 == 0:
		lis.append(2)
		num = num // 2
	for i in range(3, int(math.sqrt(num)) + 1, 2):
		while num % i == 0:
			if i > 3:
				return []
			lis.append(i)
			num = num // i
	if num > 2:
		lis.append(num)
	return lis
t = int(input())
Dict = defaultdict(list)
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		temp = Dict[n]
		if len(temp) == 0:
			x = primeFactors(n)
			Dict[n] = x
		else:
			x = temp
		if len(x) == 0:
			print(-1)
		else:
			x.sort()
			if x[-1] > 3:
				print(-1)
			else:
				two = x.count(2)
				three = x.count(3)
				if two > three:
					print(-1)
				else:
					comm = two
					rem = three - comm
					print(comm + rem * 2)


t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	f = True
	if n % 6 == 0 or n % 6 == 3:
		while n > 1:
			if n % 6 != 0 and n % 6 != 3:
				print(-1)
				f = False
				break
			elif n % 6 == 0:
				n = n // 6
			else:
				n *= 2
			c += 1
		if f:
			print(c)
	elif n != 1:
		print(-1)
	else:
		print(0)


from math import floor, ceil, log2, log

def hp_2(n):
	ans = log2(n & ~(n - 1))
	return ans

def hp_3(n):
	ans = 0
	while n % 3 == 0:
		n /= 3
		ans += 1
	return ans
for i in range(int(input())):
	n = int(input())
	if 2 ** hp_2(n) * 3 ** hp_3(n) != n:
		print(-1)
	elif hp_2(n) <= hp_3(n):
		print(int(2 * hp_3(n) - hp_2(n)))
	else:
		print(-1)


from sys import stdin
from math import *
from random import randint
from collections import deque

def splt():
	return map(int, stdin.readline().split())
a = 1
ara = {1: 0}
for i in range(1, 100000, 1):
	ara[a * 6] = i
	a = a * 6
	if a > 1000000000000000000000:
		break
test = int(input())
for i in range(test):
	n = int(input())
	flag = 0
	for j in range(1000000000):
		if ara.get(n) or n == 1:
			print(j + ara[n])
			flag = 1
			break
		n = n * 2
		if n > 10000000000000000000:
			break
	if flag == 0:
		print(-1)


from collections import defaultdict as dd
from math import log

def solve():
	n = int(input())
	ans = 0
	flag = 0
	while n > 1:
		while n % 6 == 0:
			n = n // 6
			ans += 1
		if n == 1:
			break
		if n % 3 == 0:
			n *= 2
			ans += 1
			continue
		else:
			flag = 1
			break
	if flag == 1:
		print(-1)
	else:
		print(ans)
t = 1
t = int(input())
for _ in range(t):
	solve()


def num_divisers(m, k):
	counter = 0
	while m >= k and m % k == 0:
		m = m // k
		counter += 1
	return counter
g = int(input())
for i in range(1, g + 1):
	n = int(input())
	a = num_divisers(n, 2)
	b = num_divisers(n, 3)
	if n % 3 != 0 and n != 1:
		print(-1)
	elif a > b:
		print(-1)
	elif 2 ** a * 3 ** b != n:
		print(-1)
	else:
		c = 2 * b - a
		print(c)


for _ in range(int(input())):
	n = int(input())
	n2 = 0
	while n % 2 == 0:
		n2 += 1
		n = n // 2
	n3 = 0
	while n % 3 == 0:
		n3 += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif n2 <= n3:
		print(n3 - n2 + n3)
	else:
		print(-1)


def find_move_count(num):
	moves = 0
	while num != 1 and moves != -1:
		if num % 6 == 0:
			num /= 6
			moves += 1
		elif num * 2 % 6 == 0:
			num *= 2
			moves += 1
		else:
			moves = -1
	return moves
t = int(input())
for i in range(t):
	n = int(input())
	print(find_move_count(n))


t = int(input())
for _ in range(t):
	n = int(input())
	count = 0
	temp = n
	while 1:
		if temp % 3 == 0:
			temp = temp // 3
			count += 1
		else:
			break
	count2 = 0
	temp = n
	while 1:
		if temp % 2 == 0:
			temp = temp // 2
			count2 += 1
		else:
			break
	if count >= count2:
		temp = n * pow(2, count - count2)
		temp = temp / pow(6, count)
		if temp == 1:
			print(count + (count - count2))
		else:
			print(-1)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print('-1')
	else:
		col2 = 0
		col3 = 0
		while n % 2 == 0:
			n /= 2
			col2 += 1
		while n % 3 == 0:
			n /= 3
			col3 += 1
		if n > 1:
			print('-1')
		elif col2 > col3:
			print('-1')
		else:
			print(2 * col3 - col2)


T = int(input())
for _ in range(T):
	n = int(input())
	threes = 0
	while n % 3 == 0:
		n /= 3
		threes += 1
	twos = 0
	while n % 2 == 0:
		n /= 2
		twos += 1
	if n > 1:
		print(-1)
	elif twos > threes:
		print(-1)
	else:
		print(2 * threes - twos)


def read_list():
	return list(map(int, input().strip().split(' ')))

def print_list(l):
	print(' '.join(map(str, l)))

def judge_3(n):
	tmp = 0
	for c in str(n):
		tmp += int(c)
	return tmp % 3 == 0
N = int(input())
for _ in range(N):
	n = int(input())
	(two, three) = (0, 0)
	while n & 1 == 0:
		two += 1
		n >>= 1
	while judge_3(n):
		three += 1
		n //= 3
	if n != 1 or two > three:
		print(-1)
	else:
		print(three - two + three)


t = int(input())
for o in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	count = 0
	while n % 3 == 0:
		n //= 3
		count += 1
	c = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	if n == 1 and c <= count:
		print(2 * count - c)
	else:
		print(-1)


from sys import stdin as inp
from math import gcd, sqrt, floor, ceil, log2

def fun():
	n = int(inp.readline())
	dic = {}
	if n == 1:
		return 0
	dic = {2: 0, 3: 0}
	if n % 2 == 0:
		while n % 2 == 0:
			dic[2] += 1
			n //= 2
	if n % 3 == 0:
		while n % 3 == 0:
			dic[3] += 1
			n //= 3
	if n > 2:
		return -1
	if dic[2] <= dic[3]:
		return dic[3] - dic[2] + dic[3]
	return -1
for _ in range(int(inp.readline())):
	print(fun())


t = int(input())
lis = []
out = []

def div2(k):
	c = 0
	i = 2
	while k % i == 0:
		c += 1
		i *= 2
	return c

def div3(k):
	c = 0
	i = 3
	while k % i == 0:
		c += 1
		i *= 3
	return c
for i in range(t):
	lis.append(int(input()))
	n = lis[i]
	a = div2(n)
	b = div3(n)
	if 2 ** a * 3 ** b == n and a <= b:
		out.append(b + b - a)
	else:
		out.append(-1)
for i in out:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	p2 = 0
	p3 = 0
	flag = True
	while True:
		if n == 1:
			break
		if n % 2 == 0:
			p2 += 1
			n //= 2
		elif n % 3 == 0:
			p3 += 1
			n //= 3
		else:
			flag = False
			break
	if flag:
		if p2 <= p3:
			print(p3 - p2 + p3)
		else:
			print(-1)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	while n != 1 and n < 10 ** 9:
		if n % 6 == 0:
			n /= 6
		else:
			n *= 2
		count += 1
	if n == 1:
		print(count)
	else:
		print(-1)


t = int(input())
for tests in range(t):
	case = int(input())
	number_two = 0
	number_three = 0
	while case % 2 == 0:
		case = case / 2
		number_two += 1
	while case % 3 == 0:
		case = case / 3
		number_three += 1
	if case != 1 or number_three < number_two:
		print('-1')
	else:
		print(2 * number_three - number_two)


t = int(input())
for i in range(t):
	n = int(input())
	c = 0
	while n != 1 and n != 2 and (n <= 1000000000):
		if n % 6 == 0:
			c = c + 1
			n = n // 6
		else:
			c = c + 1
			n = n * 2
	if n == 1:
		print(c)
	else:
		print('-1')


t = int(input())
for _ in range(t):
	n = int(input())
	total = 0
	while True:
		if n == 1:
			break
		if n % 6 == 0:
			n /= 6
		elif n * 2 % 6 == 0:
			n *= 2
		else:
			total = -1
			break
		total += 1
	print(total)


def chk(n):
	c = 0
	if n == 3:
		return 2
	while n > 0:
		if n % 6 == 0:
			c += 1
			n //= 6
		elif n * 2 % 6 == 0:
			n = n * 2 // 6
			c += 2
		else:
			if n == 1:
				return c
			return -1
	return -1
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		print(chk(n))


import sys
t = int(sys.stdin.readline().strip())
for _ in range(t):
	n = int(sys.stdin.readline().strip())
	(two, three) = (0, 0)
	while n % 2 == 0:
		n //= 2
		two += 1
	while n % 3 == 0:
		n //= 3
		three += 1
	if n > 1 or two > three:
		print(-1)
		continue
	else:
		print(three - two + three)


for _ in range(int(input())):
	n = int(input())
	k = 0
	f = 0
	while True:
		if n == 1:
			break
		if n % 3 != 0:
			print(-1)
			f = 1
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		k += 1
	if f == 0:
		print(k)


def countTwo(n):
	count = 0
	while n > 1:
		if n % 2 == 0:
			n = n // 2
			count += 1
		else:
			break
	return count

def countThree(n):
	count = 0
	while n > 1:
		if n % 3 == 0:
			n = n // 3
			count += 1
		else:
			break
	return count
for test in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		dictcount = {2: countTwo(n), 3: countThree(n)}
		if 2 ** dictcount[2] * 3 ** dictcount[3] != n:
			print(-1)
		elif dictcount[2] > dictcount[3]:
			print(-1)
		else:
			print(2 * dictcount[3] - dictcount[2])


for a in [*open(0)][1:]:
	b = int(a)
	r = 0
	while b % 3 < 1:
		r += b % 2 + 1
		b //= 6 - b % 2 * 3
	print((r, -1)[b > 1])


from math import log

def solution(x):
	res = 0
	while x % 3 == 0:
		if x % 6 == 0:
			x //= 6
			res += 1
		else:
			x *= 2
			res += 1
	if x == 1:
		return res
	return -1

def main():
	for test in range(int(input())):
		num = int(input())
		result = solution(num)
		print(result)
main()


import math
import bisect
import sys
from collections import OrderedDict
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
T = inp()
for t in range(T):
	N = inp()
	two = 0
	three = 0
	while N % 2 == 0:
		two += 1
		N = N // 2
	while N % 3 == 0:
		three += 1
		N = N // 3
	if N != 1:
		print(-1)
	elif two > three:
		print(-1)
	else:
		print(two + (three - two) * 2)


from collections import Counter, defaultdict, deque
import sys
input = sys.stdin.readline

def primes(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return factors

def solve():
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		n //= 2
		two += 1
	while n % 3 == 0:
		n //= 3
		three += 1
	if n != 1 or two > three:
		print(-1)
		return
	print(three - two + three)
tt = int(input())
for test in range(tt):
	solve()


t = int(input())

def primes(n):
	count = 0
	rl = 0
	while n % 2 == 0:
		n = n // 2
		count += 1
	while n % 3 == 0:
		n = n // 3
		rl += 1
	if n != 1 or count > rl:
		return -1
	else:
		return [rl, count]
from math import *
for xy in range(t):
	n = int(input())
	rq = primes(n)
	if rq == -1:
		print(-1)
	else:
		print(rq[0] - rq[1] + rq[0])


t = int(input())

def checkio(n):
	counter = 0
	while n % 3 == 0 or n % 6 == 0:
		if n % 6 == 0:
			n /= 6
		else:
			n *= 2
		counter += 1
	if n != 1:
		return -1
	else:
		return counter
for i in range(t):
	n = int(input())
	print(checkio(n))


def test(n):
	dv = 0
	tr = 0
	while n != 1:
		if n % 2 == 0:
			dv += 1
			n //= 2
		elif n % 3 == 0:
			tr += 1
			n //= 3
		else:
			return -1
	if tr < dv:
		return -1
	else:
		return 2 * tr - dv
t = int(input())
for _ in range(t):
	print(test(int(input())))


for _ in range(int(input())):
	n = int(input())
	a = [0, 0]
	while n % 2 == 0:
		n //= 2
		a[0] += 1
	while n % 3 == 0:
		n //= 3
		a[1] += 1
	if n != 1 or a[0] > a[1]:
		print(-1)
	else:
		print(2 * a[1] - a[0])


six = [pow(6, i) for i in range(0, 20)]
T = int(input())
for i in range(T):
	n = int(input())
	result = -1
	for k in range(40):
		a = n * pow(2, k)
		if a in six:
			break
	a = n * pow(2, k)
	if a in six:
		print(k + six.index(a))
	else:
		print(-1)


t = int(input())
n = list('0') * t
for x in range(t):
	bul = False
	n[x] = int(input())
	r = 0
	if n[x] == 1:
		print(0)
		continue
	while n[x] != 1:
		if n[x] % 2 == 0 and n[x] % 6 != 0:
			bul = True
			break
		if n[x] % 6 == 0:
			n[x] //= 6
		else:
			n[x] *= 2
		r += 1
	if bul:
		print(-1)
		continue
	print(r)


def solution(n):
	if n == 1:
		return 0
	(i, j) = (0, 0)
	while n % 3 == 0:
		i += 1
		n //= 3
	while n % 2 == 0:
		n //= 2
		j += 1
	if n == 1 and j <= i:
		return i + (i - j)
	return -1
tc = int(input())
while tc > 0:
	n = int(input())
	print(solution(n))
	tc -= 1


t = int(input())
while t:
	n = int(input())
	i = 0
	while n % 2 == 0:
		n = n // 2
		i += 1
	j = 0
	while n % 3 == 0:
		n = n // 3
		j += 1
	if n > 1 or i > j:
		print(-1)
	else:
		print(i + (j - i) * 2)
	t -= 1


import sys
import math
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def Solution(n):
	if n == 1:
		print(0)
		return
	cnt2 = 0
	cnt3 = 0
	d = 2
	while n > 1:
		if d > 3:
			break
		if n % d == 0:
			n = n // d
			if d == 2:
				cnt2 += 1
			elif d == 3:
				cnt3 += 1
		else:
			d += 1
	if d > 3:
		print(-1)
	elif cnt2 > cnt3:
		print(-1)
	else:
		print(cnt3 - cnt2 + cnt3)

def main():
	for _ in range(int(input())):
		n = int(input())
		Solution(n)
main()


def solve():
	global N
	C = 0
	mul = 0
	while N != 1:
		if N % 6 == 0:
			N //= 6
			mul = 0
		elif mul == 2:
			return -1
		else:
			mul += 1
			N *= 2
		C += 1
	return C
import os
for _ in range(int(input())):
	N = int(input())
	sol = solve()
	print(sol)


def solve(n):
	cnt = 0
	if n == 1:
		return 0
	while n != 1:
		if n % 3:
			return -1
		cnt += 1
		if n % 6:
			n *= 2
		else:
			n //= 6
	return cnt
t = int(input())
for i in range(t):
	n = int(input())
	print(solve(n))


for t in range(int(input())):
	n = int(input())
	power_of_2 = 0
	for i in range(1, 50):
		p2 = 2
		if n % p2 == 0:
			power_of_2 += 1
			n = n // 2
		else:
			break
	power_of_3 = 0
	for i in range(1, 50):
		p3 = 3
		if n % p3 == 0:
			power_of_3 += 1
			n = n // 3
		else:
			break
	if power_of_2 > power_of_3 or n > 1:
		print(-1)
	else:
		print(power_of_3 + abs(power_of_3 - power_of_2))


t = int(input())
for i in range(t):
	nn = int(input())
	n = 0
	c = 0
	while nn % 3 == 0:
		nn /= 3
		n += 1
	while nn % 2 == 0:
		nn /= 2
		c += 1
	if nn == 1 and c <= n:
		print(-(c - 2 * n))
	else:
		print(-1)


a = int(input())
l = []
for i in range(a):
	n = int(input())
	if n == 1:
		l.append(0)
	elif n == 2:
		l.append(-1)
	elif n == 6:
		l.append(1)
	else:
		c = 0
		p = 0
		d = 0
		while True:
			if n == 2:
				l.append(-1)
				break
			elif n == 3:
				c += 2
				l.append(c)
				break
			elif n == 6:
				c += 1
				l.append(c)
				break
			elif n % 6 == 0:
				n = n // 6
				p = 0
				c += 1
			else:
				d = 0
				if p == 1:
					l.append(-1)
					d = 1
				else:
					p = 1
					n = n * 2
					c += 1
			if d == 1:
				break
for x in l:
	print(x)


import sys
import math

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		six = 0
		three = 0
		a = n
		while True:
			if a % 6 == 0:
				a = a // 6
				six += 1
			else:
				break
		while True:
			if a % 3 == 0:
				a = a // 3
				three += 1
			else:
				break
		if a == 1:
			print(six + 2 * three)
		else:
			print(-1)
main()


def test(n):
	k = 0
	r = 0
	while n % 3 == 0:
		n = n // 3
		k += 1
	while n % 2 == 0:
		n = n // 2
		r += 1
	if n != 1:
		return -1
	if r <= k:
		return 2 * k - r
	return -1
t = int(input())
for i in range(t):
	n = int(input())
	print(test(n))


t = int(input())
otvet = []
for _ in range(t):
	kol = 0
	kol_2 = 0
	kol_3 = 0
	n = int(input())
	while n != 1 and n % 2 == 0:
		kol_2 += 1
		n //= 2
	while n != 1 and n % 3 == 0:
		kol_3 += 1
		n //= 3
	if kol_2 > kol_3 or n != 1:
		kol = -1
	elif kol_3 > kol_2:
		kol = 2 * (kol_3 - kol_2) + kol_2
	elif kol_3 == kol_2:
		kol = kol_2
	otvet.append(kol)
for i in range(t):
	print(otvet[i])


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		z = 0
		good = True
		while n != 1:
			z += 1
			if n % 2 != 0:
				n *= 2
			elif n % 3 == 0:
				n //= 6
			else:
				good = False
				break
		if not good:
			print(-1)
			continue
		print(z)
main()


n = int(input())
for i in range(n):
	a = int(input())
	i = 0
	while int(a) > 1 and i > -1:
		if int(a) % 3 == 0 and int(a) % 2 == 0:
			a = int(a / 6)
			i = i + 1
		elif int(a) % 3 == 0 and int(a) % 2 != 0:
			i = i + 2
			a = int(a / 3)
		elif int(a) % 3 != 0:
			i = -1
			break
	print(i)


N = int(input())
for i in range(N):
	number = int(input())
	twos = 0
	threes = 0
	while number % 2 == 0:
		number = number // 2
		twos += 1
	while number % 3 == 0:
		number = number // 3
		threes += 1
	if number == 1:
		if threes >= twos:
			print(2 * threes - twos)
		else:
			print('-1')
	else:
		print('-1')


import math

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b / gcd(a, b)

def bs(arr, l, r, x):
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return arr[mid]
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def swap(list, pos1, pos2):
	(list[pos1], list[pos2]) = (list[pos2], list[pos1])
	return list
t = int(input())
for _ in range(t):
	n = int(input())
	x = n / 3
	if n == 1:
		print(0)
		continue
	if n == 3:
		print(2)
	elif x % 1 != 0:
		print(-1)
	else:
		i3 = 0
		nn = n
		while nn % 3 == 0:
			nn = nn / 3
			i3 += 1
		i2 = 0
		while nn % 2 == 0:
			nn = nn / 2
			i2 += 1
		if i3 >= i2:
			z = n * pow(2, i3 - i2)
			x = 0
			while z % 6 == 0:
				z = z / 6
				x += 1
			if z != 1:
				print(-1)
			else:
				print(i3 - i2 + x)
		else:
			print(-1)


Q = int(input())
for i in range(Q):
	n = int(input())
	D2 = 0
	D3 = 0
	while n % 2 == 0:
		D2 += 1
		n //= 2
	while n % 3 == 0:
		D3 += 1
		n //= 3
	if n != 1:
		print(-1)
	elif D3 >= D2:
		print(D3 * 2 - D2)
	else:
		print(-1)


def count(n):
	cnt2 = 0
	cnt3 = 0
	while n % 2 == 0:
		n = n // 2
		cnt2 += 1
	while n % 3 == 0:
		n = n // 3
		cnt3 += 1
	if n == 1 and cnt2 <= cnt3:
		print(2 * cnt3 - cnt2)
	else:
		print(-1)
t = int(input())
l = []
for i in range(t):
	n = int(input())
	l.append(n)
for i in range(len(l)):
	count(l[i])


t = int(input())
while t > 0:
	n = int(input())
	a = 0
	b = 0
	while n % 3 == 0:
		a += 1
		n = n / 3
	while n % 2 == 0:
		b += 1
		n = n / 2
	if a < b:
		print(-1)
	if a >= b:
		if n != 1:
			print(-1)
		else:
			print(2 * a - b)
	t -= 1


for i in range(int(input())):
	n2 = n3 = 0
	num = int(input())
	while num % 2 == 0:
		n2 += 1
		num /= 2
	while num % 3 == 0:
		n3 += 1
		num /= 3
	print((2 * n3 - n2 + 1) * (num == 1) * (n2 <= n3) - 1)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	q = math.log2(n)
	if q == int(q):
		print(-1)
		continue
	if n % 2 != 0 and n % 3 != 0:
		print(-1)
		continue
	k = 1
	c = 0
	w = 0
	while k:
		if n % 6 == 0:
			n = n // 6
			c += 1
			w = 0
		else:
			c += 1
			n = n * 2
			w += 1
		q = math.log2(n)
		if n == 1:
			a = 2
			k = 0
		elif q == int(q) or w >= 2:
			k = 0
			a = 1
	if a == 2:
		print(c)
	else:
		print(-1)


t = int(input())
while t:
	t -= 1
	n = int(input())
	if n == 1:
		print(0)
	ans = 0
	count = 0
	stop = False
	if n == 2:
		print(-1)
		continue
	if n == 3:
		print(2)
		continue
	if n > 3:
		while 1:
			if n % 6 != 0:
				n *= 2
				count += 1
				if n % 6 != 0:
					print(-1)
					stop = True
					break
			n /= 6
			count += 1
			if n == 1:
				print(count)
				stop = True
				break
		if stop:
			continue


t = int(input())
L = []
for i in range(t):
	m = int(input())
	L.append(m)
for e in L:
	c = 0
	while e != 1 and e % 3 == 0:
		if e % 6 == 0:
			e = e / 6
			c += 1
		elif e % 6 != 0:
			e = e * 2
			c += 1
	if e == 1:
		print(c)
	if e % 3 != 0 and e != 1:
		print(-1)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	a = n
	x = k = 0
	while a % 3 == 0:
		x += 1
		a //= 3
	while a % 2 == 0:
		k += 1
		a //= 2
	if x < k or a != 1:
		print(-1)
	else:
		print(k + (x - k) * 2)


import math
from collections import Counter, deque
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
I = lambda : int(input())
IN = lambda : input()
mod = 10 ** 9 + 7

def s(a):
	print(' '.join(list(map(str, a))))

def solve():
	n = I()
	a = n
	x = 0
	y = 0
	while a % 2 == 0:
		a = a // 2
		x += 1
	while a % 3 == 0:
		a = a // 3
		y += 1
	if a != 1 or x > y:
		print(-1)
	else:
		print(2 * (y - x) + x)
for i in range(I()):
	solve()


T = int(input())
for _ in range(T):
	n = int(input())
	count = 0
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	elif n % 3 == 0 and n % 2 != 0:
		while n != 1:
			if n % 3 != 0:
				count = -1
				break
			n = n * 2
			n = n // 6
			count += 2
		print(count)
	elif n % 3 == 0 and n % 2 == 0:
		while n != 1:
			if n % 3 != 0:
				count = -1
				break
			if n % 3 == 0 and n % 2 != 0:
				n = n * 2
				count += 1
			n = n // 6
			count += 1
		print(count)


for i in range(int(input())):
	n = int(input())
	x = 0
	while n % 3 == 0:
		if n % 6 == 0:
			n = n // 6
		else:
			n *= 2
		x += 1
	if n == 1:
		print(x)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 == 0:
		te = 0
		tem = n
		while tem % 3 == 0:
			tem = tem // 3
			te += 1
		te1 = 0
		while tem % 2 == 0:
			tem = tem // 2
			te1 += 1
		if te1 > te or tem != 1:
			print(-1)
		else:
			print(2 * te - te1)
	else:
		print(-1)


import sys
ii = lambda : sys.stdin.readline().strip()
idata = lambda : [int(x) for x in ii().split()]

def solve():
	a = int(ii())
	(c1, c2) = (0, 0)
	while a % 2 == 0:
		a //= 2
		c1 += 1
	while a % 3 == 0:
		a //= 3
		c2 += 1
	if a == 1:
		if c2 < c1:
			print(-1)
		else:
			print(c2 * 2 - c1)
	else:
		print(-1)
	return
for t in range(int(ii())):
	solve()


t = int(input())
for z in range(t):
	n = int(input())
	c = 0
	f = False
	while True:
		if n == 1:
			break
		if n % 6 == 0:
			n = n // 6
			c += 1
		elif n % 3 == 0:
			n = n // 3
			c += 2
		else:
			f = True
			break
	if f:
		print(-1)
	else:
		print(c)


import sys
input = sys.stdin.readline
testcases = int(input())

def moves(n):
	twos = 0
	threes = 0
	while True:
		if n == 1:
			break
		elif n % 2 == 0:
			twos += 1
			n = n // 2
		elif n % 3 == 0:
			threes += 1
			n = n // 3
		else:
			return -1
	if twos > threes:
		return -1
	return max(twos, threes) + threes - twos
for x in range(testcases):
	print(moves(int(input())))


num = int(input())
for i in range(num):
	n = int(input())
	count1 = 0
	count2 = 0
	ac = 1
	ac2 = 1
	while n != 1:
		if n % 3 == 0:
			n /= 3
			count1 += 1
		else:
			ac = 0
			break
	while n != 1:
		if n % 2 == 0:
			n /= 2
			count2 += 1
		else:
			ac2 = 0
			break
	if ac == 0:
		if ac2 == 0 or count1 < count2:
			print(-1)
		else:
			print(2 * count1 - count2)
	else:
		print(2 * count1 - count2)


t = int(input())
while t > 0:
	n = int(input())
	flag = 0
	cnt = 0
	if n == 1:
		print(0)
	elif n == 2:
		print(-1)
	elif n % 3 != 0:
		print(-1)
	else:
		while 1:
			if n == 1:
				break
			elif n == 2:
				flag = 1
				break
			if n % 3 != 0:
				flag = 1
				break
			if n % 6 == 0:
				n = n // 6
			else:
				n = n * 2
			cnt += 1
		if flag == 0:
			print(cnt)
		else:
			print(-1)
	t -= 1


def get_result(n):
	if n == 1:
		return 0
	m = 0
	while n % 3 == 0:
		n //= 3
		m += 1
	k = 0
	while n % 2 == 0:
		n //= 2
		k += 1
	if m < k or n > 1:
		return -1
	return 2 * m - k
t = int(input())
res = [0] * t
for i in range(t):
	n = int(input())
	res[i] = get_result(n)
for i in range(t):
	print(res[i])


def solve():
	n = int(input())
	c = 0
	b = 0
	if n == 1:
		print(0)
	elif n < 3:
		print(-1)
	else:
		while n % 3 == 0:
			n = n // 3
			c += 1
		while n % 2 == 0:
			n = n // 2
			b += 1
		if c == 0 or n != 1 or c < b:
			print(-1)
		else:
			print((c - b) * 2 + b)
T = 1
T = int(input())
for _ in range(1, T + 1):
	solve()


def one_get_not_get(arr):
	for i in range(len(arr)):
		j = 0
		count = 0
		count1 = 0
		value = arr[i]
		while j < value * 6:
			if arr[i] % 6 == 0:
				arr[i] = int(arr[i] / 6)
				count += 1
				count1 -= 1
			elif arr[i] - 6 == 2:
				print(-1)
				break
			elif count1 > 6:
				print(-1)
				break
			elif arr[i] == 1:
				print(count)
				break
			else:
				arr[i] = arr[i] * 2
				count += 1
				count1 += 1
			j += 1
t = int(input())
arr = []
for i in range(t):
	n = int(input())
	arr.append(n)
one_get_not_get(arr)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	tw = 0
	th = 0
	while n % 2 == 0:
		n //= 2
		tw += 1
	while n % 3 == 0:
		n //= 3
		th += 1
	if n != 1:
		print(-1)
		continue
	if th == tw:
		print(th)
	elif th > tw:
		print(th - tw + th)
	else:
		print(-1)


from math import log2
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 == 0:
		three = 0
		while 1:
			temp = n
			temp = temp / 3
			if temp == int(temp):
				three += 1
				n = temp
			else:
				break
		two = log2(n)
		if two == int(log2(n)):
			if three >= two:
				print(int(three + (three - two)))
			else:
				print(-1)
		else:
			print(-1)
	else:
		print(-1)


t = int(input())
for you in range(t):
	n = int(input())
	x = 0
	z = n
	o = n
	while z > 0:
		if z % 3 == 0:
			z = z // 3
			x += 1
		else:
			break
	y = 0
	while o > 0:
		if o % 2 == 0:
			o = o // 2
			y += 1
		else:
			break
	if 2 ** y * 3 ** x != n or x < y:
		print(-1)
	else:
		print(2 * x - y)


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
for _ in range(iinput()):
	n = iinput()
	ans = 0
	while n != 1:
		if n % 6 == 0:
			n /= 6
			ans += 1
		elif n % 3 == 0:
			n /= 3
			ans += 2
		else:
			ans = -1
			break
	print(ans)


def ninja(n):
	k = 0
	while True:
		if n == 1:
			return k
		elif n % 6 == 0:
			n //= 6
			k += 1
		elif n % 3 == 0:
			n *= 2
			k += 1
		else:
			return -1
for _ in range(int(input())):
	n = int(input())
	print(ninja(n))


import math

def test(n, c):
	if n == 1:
		return c
	elif n % 3 == 0 or n % 6 == 0:
		if n % 6 == 0:
			return test(n / 6, c + 1)
		else:
			return test(n * 2, c + 1)
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(test(n, 0))


t = int(input())
for case in range(t):
	n = int(input())
	n2 = n
	cnt2 = 0
	cnt3 = 0
	while n2 % 2 == 0:
		n2 //= 2
		cnt2 += 1
	while n2 % 3 == 0:
		n2 //= 3
		cnt3 += 1
	if n2 != 1 or cnt2 > cnt3:
		print(-1)
	else:
		ans = abs(cnt2 - cnt3) + max(cnt2, cnt3)
		print(ans)


for _ in range(int(input())):
	N = int(input())
	(Num1, Num2) = (0, 0)
	while N % 2 == 0:
		Num1 = Num1 + 1
		N = N // 2
	while N % 3 == 0:
		Num2 = Num2 + 1
		N = N // 3
	if N == 1 and Num2 >= Num1:
		print(2 * Num2 - Num1)
	else:
		print(-1)


n = int(input())
for _ in range(n):
	x = int(input())
	kol3 = 0
	kol2 = 0
	while x % 3 == 0:
		kol3 += 1
		x //= 3
	while x % 2 == 0:
		kol2 += 1
		x //= 2
	if x != 1 or kol3 < kol2:
		print(-1)
	else:
		print(kol3 - kol2 + kol3)


cases = int(input())
item = []
for i in range(cases):
	item.append(int(input()))
for i in item:
	count3 = 0
	count2 = 0
	while i % 3 == 0:
		i /= 3
		count3 += 1
	while i % 2 == 0:
		i /= 2
		count2 += 1
	if i != 1:
		print(-1)
		continue
	if count2 > count3:
		print(-1)
		continue
	print(count3 * 2 - count2)


a = int(input())
for i in range(a):
	b = int(input())
	c = 0
	for i in range(100):
		if b == 1:
			print(c)
			break
		if b % 6 == 0:
			b = b // 6
			c += 1
		else:
			b = b * 2
			c += 1
		if c > 50:
			print(-1)
			break


t = int(input())
for caseNr in range(1, t + 1):
	n = int(input())
	threes = 0
	while n % 3 == 0:
		n /= 3
		threes += 1
	twos = 0
	while n % 2 == 0:
		n /= 2
		twos += 1
	moves = threes
	if threes - twos >= 0 and n == 1:
		moves += threes - twos
		print(moves)
	else:
		print(-1)


t = int(input())
i = 0
while i < t:
	n = int(input())
	count2 = 0
	count3 = 0
	while n != 1:
		div2 = True
		div3 = True
		if n % 3 == 0:
			n = n // 3
			count3 = count3 + 1
		else:
			div3 = False
		if n % 2 == 0:
			n = n // 2
			count2 = count2 + 1
		else:
			div2 = False
		if div2 == False and div3 == False:
			break
	if n == 1:
		if count2 > count3:
			print(-1)
		else:
			print(count3 - count2 + count3)
	else:
		print(-1)
	i = i + 1


import math
import collections
import sys

def inpu():
	return input().split(' ')

def inti(a):
	for i in range(len(a)):
		a[i] = int(a[i])
	return a

def inp():
	a = inpu()
	a = inti(a)
	return a
for _ in range(int(input())):
	n = int(input())
	moves = 0
	if n % 6 == 0:
		while n % 6 == 0:
			n = n // 6
			moves += 1
	if n % 3 == 0:
		while n % 3 == 0:
			n = n // 3
			moves += 2
	if n == 1:
		print(moves)
	else:
		print(-1)


t = int(input())
while t > 0:
	n = int(input())
	count = 0
	while n > 1:
		if n % 6 == 0:
			n //= 6
			count += 1
			continue
		elif n % 2 != 0:
			n *= 2
			count += 1
			continue
		else:
			break
	if n != 1:
		print(-1)
	else:
		print(count)
	t -= 1


t = input()
t = int(t)
while t:
	t = t - 1
	n = input()
	x = 0
	y = 0
	tmp = int(n)
	while tmp:
		if tmp != 1 and tmp % 2 == 0:
			tmp = tmp / 2
			x = x + 1
		else:
			break
	while tmp:
		if tmp != 1 and tmp % 3 == 0:
			tmp = tmp / 3
			y = y + 1
		else:
			break
	ans = -1
	if tmp == 1:
		if x <= y:
			ans = y + y - x
	print(ans)


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def factor(n):
	res = []
	d = 2
	while d * d <= n:
		if n % d == 0:
			res.append(d)
			n //= d
		else:
			d += 1
		if d > 3:
			raise ValueError
	if n > 1:
		if n > 3:
			raise ValueError
		res.append(n)
	return res

def solve(n):
	try:
		f = factor(n)
	except ValueError:
		return -1
	s3 = f.count(3)
	s2 = f.count(2)
	if s3 < s2:
		return -1
	s3 = s3 - s2
	return 2 * s3 + s2
counts = int(input())
for i in range(counts):
	n = int(input())
	print(solve(n))


import math
for i in range(int(input())):
	n = int(input())
	ans = 0
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
		continue
	while True:
		while n % 6 == 0:
			n /= 6
			ans += 1
		if n == 1 or n % 3 != 0:
			break
		ans += 1
		n *= 2
	if n % 3 != 0 and n != 1:
		print(-1)
		continue
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [0, 0]
	while n % 3 == 0:
		arr[0] += 1
		n = n // 3
	while n % 2 == 0:
		arr[1] += 1
		n = n // 2
	if n != 1:
		print(-1)
	elif arr[1] > arr[0]:
		print(-1)
	else:
		print(arr[1] + 2 * (arr[0] - arr[1]))


def ans():
	n = int(input())
	n3 = 0
	n2 = 0
	while n != 1:
		x = n % 3
		if x == 0:
			n3 += 1
			n = n / 3
		else:
			break
	while n != 1:
		x = n % 2
		if x == 0:
			n2 += 1
			n = n / 2
		else:
			break
	if n != 1:
		print(-1)
		return
	if n2 > n3:
		print(-1)
		return
	ans = 0
	ans += n3 - n2
	ans += n3
	print(ans)
	return
t = int(input())
for i in range(t):
	ans()


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		f = False
		(two, three) = (0, 0)
		while n != 1:
			if n % 2 != 0 and n % 3 != 0:
				f = True
				break
			if n % 2 == 0:
				two += 1
				n = n // 2
				continue
			if n % 3 == 0:
				three += 1
				n = n // 3
				continue
		if f or two > three:
			print(-1)
		else:
			print(2 * three - two)
main()


t = int(input())
input_index = 0
while input_index < t:
	n = int(input())
	k = n
	x = 0
	y = 0
	if k == 1:
		print('0')
	else:
		while k > 1:
			if k % 3 == 0:
				k = k / 3
				x = x + 1
			elif k % 2 == 0:
				k = k / 2
				y = y + 1
			else:
				break
		if x >= y and k == 1:
			print(str(2 * x - y))
		else:
			print('-1')
	input_index = input_index + 1


t = int(input())
for i in range(t):
	n = int(input())
	twos = int(n / 2)
	threes = int(n / 3)
	k = 0
	while 1:
		if n == 1:
			print(k)
			break
		if not n % 6:
			n /= 6
			k += 1
		elif not n % 3:
			n *= 2
			k += 1
		else:
			print(-1)
			break


t = int(input())
for i in range(t):
	n = int(input())
	f = 0
	ans = 0
	while n != 1 and f == 0:
		if n % 6 == 0:
			n = int(n / 6)
		else:
			n *= 2
			if n % 6 != 0:
				f = 1
		ans += 1
	if f == 1:
		print(-1)
	else:
		print(ans)


from sys import stdin, stdout
import heapq as hq
from collections import defaultdict
t = int(input())
for tc in range(t):
	n = int(stdin.readline())
	nc = n
	c2 = 0
	c3 = 0
	while nc % 2 == 0:
		nc //= 2
		c2 += 1
	while nc % 3 == 0:
		nc //= 3
		c3 += 1
	if nc > 1 or c2 > c3:
		res = -1
	else:
		res = c3 - c2 + c3
	stdout.write(str(res) + '\n')


t = int(input())
for _ in range(t):
	n = int(input())
	two = 0
	while n % 2 == 0:
		n //= 2
		two += 1
	three = 0
	while n % 3 == 0:
		n //= 3
		three += 1
	if n != 1 or two > three:
		print(-1)
		continue
	print(2 * three - two)


from sys import stdin
from math import log
inp = lambda : stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	counter = 0
	while True:
		if n == 1:
			print(counter)
			break
		if n % 6 == 0:
			n //= 6
			counter += 1
		elif n % 3 == 0:
			n *= 2
			n //= 6
			counter += 2
		else:
			print(-1)
			break


from copy import copy, deepcopy

def read_int():
	return int(input())

def read_arr():
	return [int(x) for x in input().split(' ')]

def read_mx(n, m):
	return [[int(num) for num in input().split(' ')] for _ in range(0, n)]

def pow_of(p, x):
	if x % p == 0:
		return pow_of(p, x // p) + 1
	else:
		return 0

def work(p_case):
	n = read_int()
	a3 = pow_of(3, n)
	b2 = pow_of(2, n)
	if b2 > a3 or pow(3, a3) * pow(2, b2) != n:
		print(-1)
		return
	k = a3 + (a3 - b2)
	print(k)
t = read_int()
for case in range(0, t):
	work(case + 1)


for _ in range(int(input())):
	n = int(input())
	powof3 = 0
	while n % 3 == 0:
		n //= 3
		powof3 += 1
	powof2 = 0
	while n % 2 == 0:
		n //= 2
		powof2 += 1
	if n != 1:
		print(-1)
	elif powof3 < powof2:
		print(-1)
	elif powof3 == powof2:
		print(powof3)
	else:
		print(2 * powof3 - powof2)


n = int(input())
for i in range(n):
	o = z = int(input())
	count = 0
	while z != 1:
		if o * 6 < z:
			count = -1
			break
		if z % 6 == 0:
			z /= 6
			count += 1
		else:
			z *= 2
			count += 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	else:
		a = b = 0
		while n % 2 == 0:
			n //= 2
			a += 1
		while n % 3 == 0:
			n //= 3
			b += 1
		if n == 1 and a <= b:
			print(2 * b - a)
		else:
			print(-1)


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
import statistics
from statistics import mode
mod = 1000000007

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def most_frequent(List):
	occurence_count = Counter(List)
	return occurence_count.most_common(1)[0][0]

def most_common(List):
	return mode(List)

def createMatrix(rowCount, colCount, dataList):
	mat = []
	for i in range(rowCount):
		rowList = []
		for j in range(colCount):
			if dataList[j] not in mat:
				rowList.append(dataList[j])
		mat.append(rowList)
	return mat

def isprime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
import collections

def minimalFilps1(bits):
	n = len(bits)
	flipsFromLeft = [0 for i in range(n)]
	flipsFromRight = [0 for i in range(n)]
	flips = 0
	for i in range(0, n, 1):
		if bits[i] == '1':
			flips = flips + 1
		flipsFromLeft[i] = flips
	flips = 0
	i = n - 1
	while i >= 0:
		if bits[i] == '0':
			flips = flips + 1
		i = i - 1
		flipsFromRight[i] = flips
	minFlips1 = sys.maxsize
	for i in range(1, n, 1):
		if flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips1:
			minFlips1 = flipsFromLeft[i - 1] + flipsFromRight[i]
	return minFlips1

def minimalFilps(bits):
	n = len(bits)
	flipsFromLeft = [0 for i in range(n)]
	flipsFromRight = [0 for i in range(n)]
	flips = 0
	for i in range(0, n, 1):
		if bits[i] == '0':
			flips = flips + 1
		flipsFromLeft[i] = flips
	flips = 0
	i = n - 1
	while i >= 0:
		if bits[i] == '1':
			flips = flips + 1
		i = i - 1
		flipsFromRight[i] = flips
	minFlips = sys.maxsize
	for i in range(1, n, 1):
		if flipsFromLeft[i - 1] + flipsFromRight[i] < minFlips:
			minFlips = flipsFromLeft[i - 1] + flipsFromRight[i]
	return minFlips

def oddn(n):
	for i in range(2, n + 1):
		if i % 2 != 0 and n % i == 0:
			return i
	return 3

def memoized(*args):
	if args in stored_results:
		return stored_results[args]
	else:
		result = stored_results[args] = fn(*args)
		return result
	return memoized
import operator
for i in range(int(inp())):
	n = int(inp())
	a = 0
	if n == 1:
		a = 0
	else:
		while n > 1:
			if n % 6 == 0:
				n //= 6
				a += 1
			elif 2 * n % 6 == 0 and n % 3 == 0:
				n *= 2
				a += 1
			else:
				a = -1
				break
	print(a)
open_list = ['(']
close_list = [')']

def check(myStr):
	stack = []
	for i in myStr:
		if i in open_list:
			stack.append(i)
		elif i in close_list:
			pos = close_list.index(i)
			if len(stack) > 0 and open_list[pos] == stack[len(stack) - 1]:
				stack.pop()
			else:
				return -1
	if len(stack) == 0:
		return 0
	else:
		return -1

def Balanced_Substring(s, n):
	ans = 0
	arr = [0] * (int(n / 2) + 1)
	d = 0
	for i in range(n):
		if s[i] == '(':
			d += 1
		else:
			if d == 1:
				j = 2
				while j <= n // 2 + 1 and arr[j] != 0:
					arr[j] = 0
			ans += 1
			ans += arr[d]
			arr[d] += 1
			d -= 1
	return ans


def primeFactor(n, k):
	ans = 0
	while n % k == 0:
		n /= k
		ans += 1
	return (ans, n)
t = int(input())
for _ in range(t):
	n = int(input())
	(n2, n) = primeFactor(n, 2)
	(n3, n) = primeFactor(n, 3)
	b = n3
	a = n3 - n2
	if b < 0 or a < 0 or int(n) != 1:
		print(-1)
	else:
		print(a + b)


I = input
for _ in range(int(I())):
	n = int(I())
	try:
		c = 0
		while n != 1:
			if n % 6 == 0:
				c += 1
				n //= 6
			elif n % 3 == 0:
				n *= 2
				c += 1
			else:
				assert 1 == 0
		print(c)
	except AssertionError:
		print('-1')


a = input()
for x in range(int(a)):
	b = input()
	b = int(b)
	tag = 0
	tag1 = 0
	while b % 3 == 0:
		b /= 3
		tag += 1
	while b % 2 == 0:
		b /= 2
		tag1 += 1
	if b != 1 or tag1 > tag:
		print(-1)
	else:
		print(tag - tag1 + tag)


def f(n, k):
	if n == 1:
		return k
	if n < 6:
		if not n % 3:
			return f(n * 2, k + 1)
		else:
			return -1
	if not n % 6:
		return f(n // 6, k + 1)
	elif not n % 3:
		return f(2 * n, k + 1)
	else:
		return -1

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		print(f(n, 0))
main()


for _ in range(int(input())):
	n = int(input())
	count_2 = 0
	count_3 = 0
	if n == 1:
		print(0)
	else:
		while n != 1:
			if n % 3 == 0:
				n = n // 3
				count_3 = count_3 + 1
			else:
				break
		while n != 1:
			if n % 2 == 0:
				n = n // 2
				count_2 = count_2 + 1
			else:
				break
		if count_2 > count_3:
			print(-1)
		elif count_2 <= count_3 and n == 1:
			print(count_3 * 2 - count_2)
		else:
			print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	doubled = False
	count = 0
	while True:
		if n == 1:
			break
		if n == 2 or n == 4 or n == 5:
			count = -1
			break
		if n % 6 == 0:
			n = n / 6
			count += 1
			doubled = False
		else:
			if doubled:
				count = -1
				break
			n = n * 2
			doubled = True
			count += 1
	print(count)


def fn(n, k):
	c = 0
	while n % k == 0:
		c = c + 1
		n = int(n // k)
	return c
t = int(input())
for _ in range(t):
	x = int(input())
	a = fn(x, 2)
	b = fn(x, 3)
	if 2 ** a * 3 ** b == x:
		if a > b:
			print(-1)
		elif a == b:
			print(a)
		elif a < b:
			print(b - a + b)
	else:
		print(-1)


def F(n):
	if n == 1:
		return 0
	c = 0
	while n != 1:
		c = c + 1
		if n % 6 == 0:
			n = n // 6
		else:
			n = n * 2
		if n % 3 != 0 and n != 1:
			return -1
			break
	return c
	if n % 3 != 0:
		return -1
	return F(n)
for i in range(0, int(input())):
	print(F(int(input())))


def check(n):
	(p2, p3) = (0, 0)
	while n % 2 == 0:
		n = n >> 1
		p2 += 1
	while n % 3 == 0:
		n = n // 3
		p3 += 1
	return [n, p2, p3]

def solve():
	n = int(input())
	[one, p2, p3] = check(n)
	if one != 1:
		return -1
	if p2 > p3:
		return -1
	return p2 + 2 * (p3 - p2)
for _ in range(int(input())):
	print(solve())


for t in range(int(input())):
	(two, three) = (0, 0)
	(n,) = map(int, input().rstrip().split())
	while n % 2 == 0:
		two += 1
		n //= 2
	while n % 3 == 0:
		three += 1
		n //= 3
	if n == 1:
		if two <= three:
			ans = min(two, three)
			two -= ans
			three -= ans
			ans += three * 2
			print(ans)
		else:
			print('-1')
	else:
		print('-1')


def solve():
	n = int(input())
	th = 0
	tw = 0
	while n % 3 == 0:
		th += 1
		n //= 3
	while n % 2 == 0:
		tw += 1
		n //= 2
	if n != 1:
		print(-1)
	elif tw <= th:
		print(th - tw + th)
	else:
		print(-1)
t = int(input())
for _ in range(t):
	solve()


t = int(input())
while t > 0:
	n = int(input())
	two_count = 0
	three_count = 0
	while n % 2 == 0:
		n = n / 2
		two_count += 1
	while n % 3 == 0:
		n = n / 3
		three_count += 1
	if n != 1:
		print('-1')
	elif three_count < two_count:
		print('-1')
	else:
		steps = three_count - two_count + three_count
		print(steps)
	t = t - 1


n = int(input())
for i in range(n):
	x = int(input())
	counta = 0
	countb = 0
	if x == 1:
		print('0')
	else:
		while 1:
			if x % 3 == 0:
				x = int(x / 3)
				counta += 1
			elif x % 2 == 0:
				x = int(x / 2)
				countb += 1
			else:
				break
		if x == 1 and counta >= countb:
			print(counta * 2 - countb)
		else:
			print('-1')


n = int(input())
s = 0
s2 = 0
s3 = 0
for cycle in range(n):
	last = s
	s = int(input())
	if not s == last:
		t = s
		s2 = 0
		s3 = 0
		while t % 3 == 0:
			t = t // 3
			s3 = s3 + 1
		while t % 2 == 0:
			t = t // 2
			s2 = s2 + 1
	if t > 1 or s2 > s3:
		print(-1)
	else:
		print(2 * s3 - s2)


def ans(a):
	count = 0
	k = 0
	while a != 1:
		if a % 6 == 0:
			a = a // 6
			count += 1
		else:
			a = a * 2
			count += 1
			if a % 6 != 0 or a == 1:
				break
		if a == 1:
			k = 1
			break
	if k == 1:
		print(count)
	else:
		print(-1)
for _ in range(int(input())):
	a = int(input())
	if a == 1:
		print(0)
	elif a % 10 == 5 or (a % 6 != 0 and a * 2 % 6 != 0):
		print(-1)
	else:
		ans(a)


number = int(input())
for i in range(number):
	number2 = int(input())
	count2 = 0
	count3 = 0
	while number2 % 2 == 0:
		number2 = number2 // 2
		count2 += 1
	while number2 % 3 == 0:
		number2 = number2 // 3
		count3 += 1
	if number2 == 1 and count2 <= count3:
		print(2 * count3 - count2)
	else:
		print(-1)


import copy

def func(a):
	s = 0
	ss = 0
	b = copy.deepcopy(a)
	while b % 2 == 0:
		ss += 1
		b //= 2
	while b % 3 == 0:
		s += 1
		b //= 3
	return (ss, s, b)
n = int(input())
while n > 0:
	n -= 1
	a = int(input())
	(a2, a3, b) = func(a)
	if a2 == a3 == 0 and b == 1:
		print(0)
	elif a2 > a3 or b != 1:
		print(-1)
	elif b == 1:
		print(a3 + (a3 - a2))


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	n = int(input())
	count3 = 0
	count2 = 0
	while n % 3 == 0:
		n //= 3
		count3 += 1
	while n % 2 == 0:
		n //= 2
		count2 += 1
	if n != 1 or count2 > count3:
		print(-1)
	else:
		print(count3 + count3 - count2)


try:

	def checktwothree(x):
		cor = 1
		twoc = 0
		threec = 0
		if x == 1:
			return [True, 0]
		while x > 1:
			if x % 2 == 0:
				twoc += 1
				x = int(x / 2)
			elif x % 3 == 0:
				threec += 1
				x = int(x / 3)
			else:
				cor = 0
				break
		if cor == 1 and twoc <= threec:
			return [True, threec - twoc + threec]
		else:
			return [False, 0, 0]
	k = int(input())
	for i in range(k):
		t = int(input())
		verd = checktwothree(t)
		if verd[0]:
			print(verd[1])
		else:
			print(-1)
except Exception:
	pass


for t in range(int(input())):
	n = int(input())
	t = 0
	th = 0
	while n % 2 == 0:
		t += 1
		n //= 2
	while n % 3 == 0:
		th += 1
		n //= 3
	if n > 1 or t > th:
		print(-1)
	else:
		print(th - t + th)


import math
for _ in range(int(input())):
	n = int(input())
	k3 = 0
	k2 = 0
	t = n
	while n % 3 == 0:
		n /= 3
		k3 += 1
	while n % 2 == 0:
		n /= 2
		k2 += 1
	if k2 > k3:
		print(-1)
	else:
		t = int(n * 3 ** k3 * 2 ** k3)
		kk = 0
		while t % 6 == 0:
			t /= 6
			kk += 1
		if t > 1:
			print(-1)
		else:
			print(kk + k3 - k2)


it = lambda : list(map(int, input().strip().split()))

def solve():
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		two += 1
		n = n // 2
	while n % 3 == 0:
		three += 1
		n = n // 3
	if two > three or n > 1:
		return -1
	return three + three - two
T = int(input())
for _ in range(T):
	print(solve())


import sys
inp = [int(x) for x in sys.stdin.read().split()]
ii = 0
ttt = inp[ii]
ii += 1
for _ in range(ttt):
	n = inp[ii]
	ii += 1
	(e2, e3) = (0, 0)
	while n % 2 == 0:
		n /= 2
		e2 += 1
	while n % 3 == 0:
		n /= 3
		e3 += 1
	if n > 1 or e2 > e3:
		print(-1)
	else:
		print(e3 - e2 + e3)


t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	while n % 6 == 0:
		n = n / 6
		count = count + 1
	while n % 3 == 0:
		n = n / 3
		count = count + 2
	if n != 1:
		count = -1
	print(count)


def codeforces_round():
	for i in range(int(input())):
		x = int(input())
		counter = 0
		if x == 1:
			print(0)
			continue
		for j in range(50):
			if x == 1:
				break
			if x % 6 == 0:
				x = x // 6
			else:
				x = x * 2
			counter += 1
		if j + 1 == counter:
			print(-1)
		else:
			print(counter)
codeforces_round()


t = int(input())

def fac(n):
	res_2 = 0
	res_3 = 0
	while n % 2 == 0:
		res_2 += 1
		n //= 2
	while n % 3 == 0:
		res_3 += 1
		n //= 3
	if n != 1:
		return (0, -1)
	return (res_2, res_3)
for _ in range(t):
	n = int(input())
	(r2, r3) = fac(n)
	if r2 > r3:
		print(-1)
		continue
	print(r3 + r3 - r2)


t = int(input())

def func(n, count):
	if n == 1:
		return count
	if n % 6 == 0:
		n = n // 6
		count += 1
		return func(n, count)
	elif n % 3 == 0:
		n = n * 2
		count += 1
		return func(n, count)
	else:
		return -1
while t > 0:
	t -= 1
	x = int(input())
	count = 0
	ans = func(x, count)
	print(ans)


for i in range(int(input())):
	a = int(input())
	s = 0
	p = 0
	ans = 0
	if a == 1:
		print(0)
	else:
		while a % 3 == 0:
			s = s + 1
			a = a // 3
		while a % 2 == 0:
			p = p + 1
			a = a // 2
		if a > 1:
			ans = -1
		elif p > s:
			ans = -1
		else:
			ans = s + (s - p)
		print(ans)


for i in range(int(input())):
	a = int(input())
	count = 0
	while a != 1:
		if a % 6 == 0 and a >= 6:
			a = a / 6
			count += 1
		else:
			a = a * 2
			count += 1
		if a > 1000000000000:
			count = -1
			break
	print(count)


from math import log, floor
num = int(input())
res = []
for i in range(num):
	n = int(input())
	twos = threes = 0
	tmp = n
	while tmp % 2 == 0:
		tmp /= 2
		twos += 1
	tmp = n
	while tmp % 3 == 0:
		tmp /= 3
		threes += 1
	b = n / 2 ** twos / 3 ** threes
	if twos <= threes and b == 1:
		res.append(threes + (threes - twos))
	else:
		res.append(-1)
for elem in res:
	print(elem)


for test in range(int(input())):
	n = int(input())
	x = 0
	y = 0
	while n % 2 == 0:
		n //= 2
		x += 1
	while n % 3 == 0:
		n //= 3
		y += 1
	if n > 1 or x > y:
		print(-1)
	else:
		print(2 * y - x)


T = int(input())
for t in range(T):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 2 != 0 and n % 3 != 0:
		print(-1)
	else:
		(x, y) = (0, 0)
		while n > 1:
			if n % 2 == 0:
				n //= 2
				x += 1
			elif n % 3 == 0:
				n //= 3
				y += 1
			else:
				break
		if n > 1 or x > y:
			print(-1)
		else:
			print(2 * y - x)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		n1 = n
		one = 0
		while n1 >= 6:
			if n1 % 6 == 0:
				n1 //= 6
				one += 1
			else:
				break
		n1 = n // 6 ** one
		two = 0
		while n1 >= 3:
			if n1 % 3 == 0:
				n1 //= 3
				two += 1
			else:
				break
		if 6 ** one * 3 ** two == n:
			print(one + two * 2)
		else:
			print(-1)


t = int(input())

def fun(n):
	ans = 0
	two = 0
	three = 0
	while n % 2 == 0:
		n /= 2
		two += 1
	while n % 3 == 0:
		n /= 3
		three += 1
	if n != 1:
		print(-1)
	elif two > three:
		print(-1)
	else:
		print(2 * three - two)
while t:
	t -= 1
	n = int(input())
	fun(n)


t = int(input())
for _ in range(t):
	n = int(input())
	cnt = 0
	while n > 1:
		if n % 6 == 0:
			n //= 6
			cnt += 1
		elif n % 2 != 0:
			n *= 2
			cnt += 1
		else:
			n = 0
			cnt = -1
	print(cnt)


from sys import stdin
from math import log
p6 = []
if True:
	st = set()
	for i in range(30):
		p6.append(6 ** i)
		st.add(6 ** i)
	aux = []
	for i in st:
		for j in range(30):
			if i % 2 ** j == 0:
				aux.append(i // 2 ** j)
	for i in aux:
		st.add(i)
st = list(st)
T = int(stdin.readline().strip())
for caso in range(T):
	x = int(stdin.readline().strip())
	ans = 0
	if x in st:
		while x not in p6:
			x *= 2
			ans += 1
		print(int(ans + log(x, 6)))
	else:
		print(-1)


from sys import stdin
from collections import defaultdict as dd
from collections import deque as dq
import itertools as it
from math import sqrt, log, log2
from fractions import Fraction
t = int(input())
for _ in range(t):
	n = int(input())
	nummoves = 0
	flag = 0
	while n != 1:
		if n % 3 != 0:
			flag = 1
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		nummoves += 1
	if flag:
		print(-1)
	else:
		print(nummoves)


def isPower(x):
	return x and (not x & x - 1)
for _ in range(int(input())):
	n = int(input())
	c = 0
	while n > 0:
		if n == 1:
			break
		elif isPower(n):
			c = -1
			break
		elif n >= 1000000000:
			c = -1
			break
		elif n % 6 == 0:
			c += 1
			n = n // 6
		else:
			c += 1
			n = n * 2
	print(c)


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	count_3 = 0
	while n % 3 == 0:
		n = n / 3
		count_3 = count_3 + 1
	count_2 = 0
	while n % 2 == 0:
		n = n / 2
		count_2 = count_2 + 1
	if n > 1:
		print(-1)
	elif count_2 > count_3:
		print(-1)
	else:
		print(count_3 + count_3 - count_2)


def ispossible(n):
	i = 0
	j = 0
	while n % 2 == 0:
		n /= 2
		i += 1
	while n % 3 == 0:
		n /= 3
		j += 1
	if n != 1:
		return 0
	elif i < j:
		return i + (j - i) * 2
	elif i == j:
		return j
	else:
		return 0
n = int(input())
for i in range(n):
	s = int(input())
	if s == 1:
		print('0')
	elif ispossible(s) == 0:
		print('-1')
	else:
		print(ispossible(s))


for i in range(int(input())):
	n = int(input())
	k = 0
	flag = 0
	while n != 1:
		if n % 2 == 0 and n % 3 == 0:
			n = n // 6
			k += 1
		elif n % 2 != 0 and n % 3 == 0:
			n = n // 3
			k += 2
		else:
			flag = 1
			break
	if flag == 0:
		print(k)
	else:
		print(-1)


from sys import stdin
from collections import Counter
from math import sqrt, factorial, log10, log, floor, ceil
from itertools import permutations, combinations, combinations_with_replacement
input = stdin.readline

def li():
	return list(map(int, input().split()))

def lis():
	return list(map(str, input().split()))

def mp():
	return map(int, input().split())

def inp():
	return int(input())

def inps():
	return str(input().strip())

def pr(n):
	return stdout.write(str(n) + '\n')
INF = float('inf')

def solve():
	n = inp()
	if n == 1:
		return 0
	if n == 2 or n % 5 == 0:
		return -1
	(k, cnt) = (0, 0)
	while n != 1:
		if n % 6 == 0:
			n //= 6
		else:
			x = n % 6
			if x == 2:
				cnt += 1
			if x == 2 and cnt == 2:
				return -1
			n *= 2
		if n == 2:
			return -1
		k += 1
	return k
t = inp()
for i in range(t):
	print(solve())


for t in range(int(input())):
	n = int(input())
	count = 0
	temp = 0
	while n % 2 == 0:
		n = n // 2
		count += 1
	while n % 3 == 0:
		n = n // 3
		temp += 1
	if n == 1 and count <= temp:
		print(2 * temp - count)
	else:
		print(-1)


for _ in range(int(input())):
	x = int(input())
	p = x
	o = 0
	while p % 2 == 0 and p >= 1:
		o += 1
		p = p / 2
	r = 0
	while p % 3 == 0 and p >= 1:
		r += 1
		p = p / 3
	if p != 1:
		print(-1)
		continue
	powof6 = max(r, o)
	a = 0
	while x % 6 == 0 and x >= 1:
		x = x / 6
		a += 1
	if x % 2 == 0:
		print(-1)
		continue
	while x % 3 == 0 and x >= 1:
		a += 2
		x = x / 3
	print(a)


import math as mt
import sys, string
input = sys.stdin.readline
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
from collections import defaultdict
t = I()
for _ in range(t):
	x = I()
	n = x
	c = 0
	d = 0
	while x % 2 == 0:
		c += 1
		x //= 2
	while x % 3 == 0:
		d += 1
		x //= 3
	if x > 1:
		print(-1)
	elif c > d:
		print(-1)
	elif c == d:
		if n == 1:
			print(0)
		else:
			print(c)
	else:
		print(d - c + d)


for _ in range(int(input())):
	n = int(input())
	(r, f) = (0, 0)
	if n == 1:
		print(0)
		continue
	elif n == 2:
		print(-1)
		continue
	while n != 1:
		if n % 3 != 0 and n != 1:
			f = 1
			break
		elif n % 6 == 0:
			n //= 6
			r += 1
		else:
			n *= 2
			r += 1
	print([r, -1][f == 1])


for _ in range(int(input())):
	n = int(input())
	p2 = 0
	p3 = 0
	while n % 2 == 0:
		p2 += 1
		n //= 2
	while n % 3 == 0:
		p3 += 1
		n //= 3
	if n > 1:
		print(-1)
	elif p2 > p3:
		print(-1)
	else:
		print(2 * p3 - p2)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
T = inp()
tests = []
for _ in range(T):
	tests.append(inp())
for n in tests:
	moves = 0
	while n > 1:
		if n % 6 == 0:
			n //= 6
			moves += 1
		else:
			n *= 2
			if n % 6 != 0:
				moves = -1
				break
			else:
				moves += 1
	print(moves)


t = int(input())
for _ in range(t):
	n = int(input())
	alpha = 0
	beta = 0
	while n % 3 == 0 or n % 2 == 0:
		if n % 3 == 0:
			n //= 3
			beta += 1
		if n % 2 == 0:
			n //= 2
			alpha += 1
	if alpha > beta or n > 1:
		print(-1)
	else:
		print(2 * beta - alpha)


for x in range(int(input())):
	n = int(input())
	k = n
	if n == 1:
		print(0)
	else:
		count = 0
		mans = 1
		while k % 3 == 0:
			k = k // 3
			count += 1
			mans *= 3
		count2 = 0
		k = n
		while k % 2 == 0:
			k = k // 2
			count2 += 1
			mans *= 2
		if count2 > count or mans != n:
			print(-1)
		else:
			ans = count - count2
			ans += count
			print(ans)


from sys import stdin
N = int(stdin.readline())
for case in range(N):
	x = int(stdin.readline())
	if x == 1:
		print('0')
		continue
	res = 0
	while x % 6 == 0:
		res += 1
		x = x / 6
	if x == 1:
		print(res)
		continue
	res2 = 0
	while x % 3 == 0:
		res2 += 1
		x = x / 3
	if x == 1:
		print(res + res2 * 2)
	else:
		print('-1')


import sys
from math import log
cont = sys.stdin.readlines()
cont = [i.strip('\n') for i in cont]
numTests = int(cont.pop(0)[0])
for i in cont:
	n = int(i)
	if n == 1:
		print(0)
		continue
	elif n % 2 % 3 == 0 or n % 3 % 2 == 0:
		dupn = n
		num3 = 0
		while dupn % 3 == 0:
			dupn = dupn // 3
			num3 += 1
		num2 = len(bin(dupn)) - 3
		if 2 ** num2 * 3 ** num3 != n:
			print(-1)
			continue
		if num2 > num3:
			print(-1)
		else:
			print(2 * num3 - num2)
	else:
		print(-1)


from sys import stdin, stdout
from os import path
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
	stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def f(n):
	p = n
	if n == 1:
		return 0
	three = two = six = 0
	while p > 1 and p % 3 == 0:
		p = p // 3
		three += 1
	p = n
	while p > 1 and p & 1 == 0:
		p = p >> 1
		two += 1
	if three < two:
		return -1
	p = n * (1 << three - two) // pow(6, three)
	if p == 1:
		return three + three - two
	return -1

def func():
	t = I()
	st = ''
	for _ in range(t):
		n = I()
		st = '\n'.join((st, str(f(n))))
	stdout.write(st.lstrip())
func()


import sys
(n, *data) = (int(x) for x in sys.stdin.read().strip().split('\n'))
for x in data:
	ans = 0
	while x % 6 == 0:
		x /= 6
		ans += 1
	while x % 3 == 0:
		x /= 3
		ans += 2
	if x == 1:
		print(ans)
	else:
		print(-1)


def dvd(num):
	if num == 1:
		return 0
	if num == 2:
		return -1
	if num == 3:
		return 2
	g = 0
	h = 0
	num0 = num
	while num0 % 2 == 0:
		num0 /= 2
		g += 1
	while num0 % 3 == 0:
		num0 /= 3
		h += 1
	if num0 != 1:
		return -1
	elif g > h:
		return -1
	else:
		return 2 * h - g
l = int(input())
m = 1
while m <= l:
	print(dvd(int(input())))
	m += 1


t = int(input())
out = []
for i in range(t):
	n = int(input())
	flag = 1
	(x, y, o) = (0, 0, 1)
	if n == 0:
		flag = 0
	while n > 1:
		if n % 2 == 0:
			x += 1
			n = n // 2
		elif n % 3 == 0:
			y += 1
			n = n // 3
		else:
			flag = 0
			break
	if x > y:
		flag = 0
	elif x < y:
		o = 2 * y - x
	else:
		o = y
	if flag == 0:
		out.append(-1)
	else:
		out.append(o)
for i in out:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	p = 0
	q = 0
	while n % 2 == 0:
		n //= 2
		p += 1
	while n % 3 == 0:
		n //= 3
		q += 1
	if n != 1:
		print(-1)
		continue
	if p > q:
		print(-1)
		continue
	print(2 * q - p)


for t in range(int(input())):
	n = int(input())
	moves = 0
	twos = 0
	x = n
	while x % 2 == 0:
		twos += 1
		x = x // 2
	threes = 0
	while x % 3 == 0:
		threes += 1
		x = x // 3
	if x != 1 or twos > threes:
		print(-1)
		continue
	moves += 2 * threes - twos
	print(moves)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	cnt1 = 0
	while n % 2 == 0:
		cnt1 += 1
		n //= 2
	cnt2 = 0
	while n % 3 == 0:
		cnt2 += 1
		n //= 3
	if n != 1:
		print(-1)
		continue
	if cnt1 > cnt2:
		print(-1)
		continue
	if cnt1 <= cnt2:
		print(cnt2 + (cnt2 - cnt1))


t = int(input())
list1 = []
list2 = []
for i in range(t):
	list1.append(int(input()))
	n = 0
	m = 0
	a = list1[i]
	while a % 3 == 0:
		a = a / 3
		n += 1
	while a % 2 == 0:
		a = a / 2
		m += 1
	if a > 1 or m > n:
		n = -1
	else:
		n = 2 * n - m
	list2.append(str(n))
print('\n'.join(list2))


def main():
	n = int(input())
	temp = n
	cnt3 = 0
	cnt2 = 0
	if n == 1:
		print(0)
		return
	while n > 1 and (temp % 2 == 0 or temp % 3 == 0):
		if temp % 3 == 0:
			temp = temp // 3
			cnt3 += 1
		if temp % 2 == 0:
			temp = temp // 2
			cnt2 += 1
	if temp != 1:
		print(-1)
		return
	if cnt3 == cnt2:
		print(cnt3)
		return
	if cnt3 > cnt2:
		print(cnt3 - cnt2 + cnt3)
		return
	print(-1)
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


t = int(input())
for i in range(t):
	n = int(input())
	(x2, x3) = (0, 0)
	while n % 2 == 0:
		n /= 2
		x2 += 1
	while n % 3 == 0:
		n /= 3
		x3 += 1
	if n != 1 or x2 > x3:
		print(-1)
		continue
	print(2 * x3 - x2)


t = int(input())
for tc in range(t):
	n = int(input())
	count = 0
	flag = 0
	while n != 1:
		if n > 10 ** 9:
			flag = 1
			break
		if n % 6 != 0:
			n *= 2
		else:
			n /= 6
		count += 1
		if n == 1:
			break
	if flag != 1:
		print(count)
		continue
	print('-1')


for _ in range(int(input())):
	n = int(input())
	dois = 0
	tres = 0
	invalid = False
	while True:
		if n == 1:
			break
		if n % 2 == 0:
			n /= 2
			dois += 1
		elif n % 3 == 0:
			n /= 3
			tres += 1
		else:
			invalid = True
			break
	if invalid or tres < dois:
		print('-1')
	else:
		print(tres - dois + tres)


t = int(input())
for _ in range(t):
	n = int(input())
	temp = n
	c1 = 0
	c2 = 0
	while (n % 2 == 0 or n % 3 == 0) and n > 0:
		if n % 2 == 0:
			c1 = c1 + 1
			n = n // 2
		elif n % 3 == 0:
			c2 = c2 + 1
			n = n // 3
	x = 2 ** c1 * 3 ** c2
	if x == temp and c1 <= c2:
		print(c2 - c1 + c2)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	flag = 0
	if n == 1:
		c = 0
	else:
		c = 0
		while n != 1:
			if n % 6 == 0:
				c += 1
				n = n // 6
			else:
				n *= 2
				if n % 6 == 0:
					c += 1
					continue
				else:
					flag = 1
					break
	if flag == 1:
		print(-1)
	else:
		print(c)


import atexit
import io
import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	ans = 0
	while True:
		if n < 9:
			if n == 1:
				pass
			elif n == 6:
				ans += 1
			elif n == 3:
				ans += 2
			else:
				ans = -1
			break
		elif n % 9 != 0:
			ans = -1
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		ans += 1
	print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	z = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			z = z + 1
		elif n % 2 != 0:
			n = n * 2
			z = z + 1
		else:
			n = 0
			z = -1
	print(z)


import math
T = int(input())
for _ in range(T):
	n = int(input())
	count = 0
	while n % 6 == 0:
		count += 1
		n = n // 6
	count1 = 0
	while n % 3 == 0:
		count += 2
		n = n // 3
	if n == 1:
		print(count + count1)
	else:
		print(-1)


import functools

def brute(N):

	@functools.lru_cache(maxsize=N)
	def dfs(n):
		if n == 1:
			return 0
		if n > 6 * N:
			return float('inf')
		cands = []
		if n % 6 == 0:
			cands.append(1 + dfs(n // 6))
		cands.append(1 + dfs(2 * n))
		return min(cands)
	result = dfs(N)
	if result == float('inf'):
		return -1
	return result

def fast1(n):
	result = float('inf')
	sixpow = 1
	for a in range(32):
		twopow = 1
		for b in range(19):
			if n == sixpow / twopow:
				print(a, b)
				result = min(result, a + b)
			twopow *= 2
		sixpow *= 6
	if result == float('inf'):
		return -1
	return result
from collections import Counter

def fast(n):
	ctr2 = ctr3 = 0
	while n % 2 == 0:
		n //= 2
		ctr2 += 1
	while n % 3 == 0:
		n //= 3
		ctr3 += 1
	if n != 1:
		return -1
	if ctr2 > ctr3:
		return -1
	return ctr3 - ctr2 + ctr3

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		result = fast(n)
		print(result)

def findPatterns():
	for n in range(1, 1000):
		result = brute(n)
		print(n, result)

def randomTest():
	import random
	t = 10 ** 4
	for i in range(t):
		n = random.randint(1, 10 ** 9)
		b = brute(n)
		f = fast(n)
		print(n, b, f)
		if b != f:
			print('Broken!')
			return
main()


def sum1(n):
	l = len(n)
	sum = 0
	for i in range(l):
		if n[i] != 'b':
			sum += int(n[i])
	return sum
n = int(input())
for i in range(n):
	num = int(input())
	bnum = bin(num)
	count3 = 0
	while 1:
		if num % 3 == 0:
			num /= 3
			count3 += 1
			continue
		else:
			break
	bbnum = bin(int(num))
	if sum1(str(bbnum)) == 1:
		count2 = len(str(bbnum)) - 3
		if count2 > count3:
			print(-1)
		else:
			print(2 * count3 - count2)
	else:
		print(-1)


t = int(input())
i = 0
while i < t:
	n = int(input())
	q = 0
	while n > 1:
		if n % 3 != 0:
			q = -1
			break
		elif n % 6 == 0:
			n //= 6
			q += 1
		else:
			n *= 2
			q += 1
	print(q)
	i += 1


t = int(input())
for _ in range(t):
	n = int(input())
	moves = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			moves += 1
		elif n % 3 == 0:
			n = n * 2
			moves += 1
		else:
			break
	if n == 1:
		print(moves)
	else:
		print(-1)


tt = int(input())
while tt != 0:
	n = int(input())
	t = 0
	th = 0
	while n % 2 == 0:
		t += 1
		n = n // 2
	while n % 3 == 0:
		th += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif t > th:
		print(-1)
	else:
		print(th - t + th)
	tt -= 1


for t in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	flag = False
	two = 0
	while not n & 1:
		two += 1
		n >>= 1
	three = 0
	while n:
		if n == 1:
			break
		if n % 3:
			flag = True
			break
		n //= 3
		three += 1
	if flag:
		print(-1)
		continue
	if three < two:
		print(-1)
	else:
		print(2 * three - two)


t = int(input())
for i in range(t):
	x = int(input())
	if x == 1:
		print(0)
	else:
		curr = x
		num_3 = 0
		while curr % 3 == 0:
			num_3 += 1
			curr = curr // 3
		curr = x
		num_2 = 0
		while curr % 2 == 0:
			num_2 += 1
			curr = curr // 2
		if num_3 < num_2:
			print(-1)
		else:
			curr = x
			curr = curr // 3 ** num_3
			curr = curr // 2 ** num_2
			if curr != 1:
				print(-1)
			else:
				print(num_3 - num_2 + num_3)


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	cnt = 0
	while n % 6 == 0:
		n = n // 6
		cnt += 1
	x = n
	while n % 3 == 0:
		n = n // 3
		x = x * 2
		cnt += 1
	n = x
	while n % 6 == 0:
		n = n // 6
		cnt += 1
	if n == 1:
		print(cnt)
	else:
		print(-1)


tl = input()
for t in range(int(tl)):
	n = int(input())
	c = -1
	while True:
		if n == 1:
			c += 1
			break
		elif n % 6 == 0:
			n //= 6
			c += 1
		elif n % 3 == 0:
			n <<= 1
			c += 1
		else:
			c = -1
			break
	print(c)


def next():
	return [int(x) for x in input().split()]
(ca,) = next()
for _ in range(ca):
	(x,) = next()
	(a, b) = (0, 0)
	while x % 3 == 0:
		x /= 3
		a += 1
	while x % 2 == 0:
		x /= 2
		b += 1
	if x != 1 or b > a:
		print(-1)
	else:
		print(a * 2 - b)


def r(x):
	i = 0
	j = 0
	while x % 3 == 0:
		x = x / 3
		i = i + 1
	while x % 2 == 0:
		x = x / 2
		j = j + 1
	if x == 1:
		return [j, i]
	else:
		return 0
for i in range(int(input())):
	n = int(input())
	if r(n) == 0:
		print(-1)
	else:
		l = r(n)
		if l[0] > l[1]:
			print(-1)
		else:
			print(2 * l[1] - l[0])


def compute(n):
	move = 0
	while n % 6 == 0 or n * 2 % 6 == 0 or n == 1:
		if n == 1:
			return move
		elif n % 6 == 0:
			n = n // 6
			move += 1
		elif n * 2 % 6 == 0:
			n = n * 2
			move += 1
		else:
			break
	return -1
t = int(input())
for _ in range(t):
	global n
	n = int(input())
	if n == 1:
		print(0)
	else:
		print(compute(n))


for i in range(int(input())):
	n = int(input())
	a2 = 0
	a3 = 0
	for j in range(n):
		flag = 0
		if n % 2 == 0:
			a2 += 1
			n = n / 2
			flag = 1
		if n % 3 == 0:
			a3 += 1
			n = n / 3
			flag = 1
		if flag == 0:
			break
	if n == 1:
		if a2 > a3:
			print(-1)
		else:
			print(a3 + a3 - a2)
	else:
		print(-1)


q = int(input())
for i in range(q):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		c3 = 0
		while n % 3 == 0:
			n = n / 3
			c3 += 1
		c2 = 0
		while n % 2 == 0:
			n = n / 2
			c2 += 1
		if n != 1 or c2 > c3:
			print(-1)
		else:
			print(2 * c3 - c2)


for _ in range(int(input())):
	x = int(input())
	if x == 1:
		print('0')
	elif x % 3 == 0:
		r = x
		tp = 0
		while r % 3 == 0:
			tp += 1
			r = r // 3
		ev = 0
		while r % 2 == 0:
			ev += 1
			r = r // 2
		if tp < ev or r != 1:
			print('-1')
		elif tp == ev:
			print(tp)
		else:
			ev = tp - ev
			print(ev + tp)
	else:
		print('-1')


t = int(input())
for i in range(t):
	answ = 0
	n = int(input())
	power_2 = 0
	power_3 = 0
	while n % 2 == 0:
		n //= 2
		power_2 += 1
	while n % 3 == 0:
		n //= 3
		power_3 += 1
	if n != 1 or power_2 > power_3:
		print(-1)
		continue
	answ += power_3 - power_2
	answ += power_3
	print(answ)


for _ in range(int(input())):
	t = int(input())
	cou = 0
	while t % 6 == 0:
		t = t / 6
		cou = cou + 1
	cou3 = 0
	while t % 3 == 0:
		t = t / 3
		cou3 += 1
	if t != 1:
		print(-1)
	else:
		print(cou + cou3 * 2)


import sys

def get_ints():
	return map(int, I().strip().split())

def get_list():
	return list(map(int, I().strip().split()))
I = sys.stdin.readline
for _ in range(int(I())):
	n = int(I())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		count = 0
		while n % 3 == 0:
			if n % 2 != 0:
				n *= 2
				count += 1
			while n % 6 == 0:
				n //= 6
				count += 1
		if n == 1:
			print(count)
		elif n == 3:
			print(count + 2)
		else:
			print(-1)


t = int(input())
count = 0
ans = []
while count < t:
	n = int(input())
	move = 0
	while n % 3 == 0:
		if n % 2 != 0:
			n *= 2
			n = n // 6
			move += 2
		else:
			n = n // 6
			move += 1
	if n == 1:
		ans.append(move)
	else:
		ans.append(-1)
	count += 1
for y in ans:
	print(y)


def solve(n):
	cnt = 0
	while True:
		while n % 6 == 0:
			n //= 6
			cnt += 1
		if n == 1:
			return cnt
		if n % 3 != 0:
			return -1
		n *= 2
		cnt += 1
for _ in range(int(input().strip())):
	n = int(input().strip())
	print(solve(n))


a = int(input())
for x in range(a):
	b = int(input())
	h = 0
	if b == 1:
		print(0)
	else:
		while b != 1 and b % 3 == 0:
			if b % 6 == 0:
				b = b // 6
				h += 1
			else:
				b = b * 2
				h += 1
		if b == 1:
			print(h)
		else:
			print(-1)


l = [6]
for i in range(1, 100):
	l.append(l[i - 1] * 6)
for i in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	tmp = 0
	i1 = 0
	i2 = 0
	while i1 < 30 and n != tmp:
		while n > l[i1]:
			i1 += 1
		tmp = l[i1]
		while n < tmp:
			i2 += 1
			n = n * 2
	if n != tmp:
		print(-1)
	else:
		print(i1 + i2 + 1)


t = int(input())
for _ in range(t):
	a = int(input())
	temp1 = a
	temp2 = a
	count1 = 0
	count2 = 0
	while temp1 % 2 == 0:
		temp1 = temp1 // 2
		count1 += 1
	while temp2 % 3 == 0:
		temp2 = temp2 // 3
		count2 += 1
	if count1 > count2:
		print(-1)
		continue
	stepcount = (count2 - count1) * 2
	a = a // 3 ** (count2 - count1)
	count1 = 0
	c = 0
	while a > 1:
		if a % 6 == 0:
			count1 += 1
			a = a // 6
		elif a != 1:
			c = 1
			break
	if c == 1:
		print(-1)
		continue
	stepcount = stepcount + count1
	print(stepcount)


for i in range(int(input())):
	two = 0
	thr = 0
	num = int(input())
	while num % 2 ** (two + 1) == 0:
		two += 1
	while num % 3 ** (thr + 1) == 0:
		thr += 1
	if num != 2 ** two * 3 ** thr or two > thr:
		print('-1')
	else:
		print(2 * thr - two)


def main():
	n = int(input())
	compteur = 0
	if n == 1:
		return 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		if n == 1:
			break
		if n % 3 != 0:
			return -1
			break
		compteur += 1
	return compteur + 1
t = int(input())
for loop in range(t):
	print(main())


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = 0
	b = 0
	while n & 1 == 0:
		n >>= 1
		a += 1
	while n % 3 == 0:
		n = n // 3
		b += 1
	if n != 1 or a > b:
		print(-1)
		continue
	print(b - a + b)


from collections import Counter
from collections import defaultdict
from collections import deque
import math
import heapq
import sys
import io, os
input = sys.stdin.readline
from bisect import *
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().strip().split()))
rls = lambda : list(map(str, input().split()))
t = int(input())
for _ in range(0, t):
	n = int(input())
	x = 2
	c1 = 0
	c2 = 0
	f = 1
	while n != 1:
		if n % 2 == 0:
			n = n // 2
			c1 = c1 + 1
		elif n % 3 == 0:
			n = n // 3
			c2 = c2 + 1
		else:
			f = 0
			break
	if c1 > c2 or f == 0:
		print(-1)
	else:
		print(c2 + (c2 - c1))


t = int(input())
for q in range(t):
	n = int(input())
	f = 'd'
	c = 0
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		while n != 1:
			if n % 3 != 0:
				f = 'u'
				break
			if n % 6 == 0:
				n //= 6
			else:
				n *= 2
			c += 1
		if f == 'u':
			print(-1)
		else:
			print(c)


t = int(input())
for i in range(t):
	n = int(input())
	p2 = 0
	p3 = 0
	while n % 2 == 0:
		p2 += 1
		n /= 2
	while n % 3 == 0:
		p3 += 1
		n /= 3
	if n != 1 or p2 > p3:
		print(-1)
		continue
	else:
		print(p2 + 2 * (p3 - p2))


n = int(input())
a = [int(input()) for i in range(n)]
for i in a:
	dem = 0
	while 2 * i % 6 == 0:
		if i % 6 == 0:
			i /= 6
		else:
			i *= 2
		dem += 1
	if i == 1:
		print(dem)
	elif i == 3:
		print(dem + 2)
	else:
		print(-1)


t = int(input())
for test in range(t):
	n = int(input())
	twos = 0
	while n % 2 == 0:
		n //= 2
		twos += 1
	tris = 0
	while n % 3 == 0:
		n //= 3
		tris += 1
	if n != 1 or twos > tris:
		print(-1)
	else:
		print(tris + tris - twos)


t = int(input())
import math
for i in range(t):
	n = int(input())
	x = 0
	for i in range(2 * (1 + int(math.log(n, 3)))):
		if n / 6 == int(n / 6):
			n = n / 6
			x = x + 1
		elif n / 3 == int(n / 3):
			n = n * 2
			x = x + 1
	if n == 1:
		print(x)
	else:
		print(-1)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	copy = n
	while n % 3 == 0:
		three += 1
		n //= 3
	while n % 2 == 0:
		two += 1
		n //= 2
	if n > 1 or two > three:
		print(-1)
	else:
		r = three - two + three
		print(r)


t = int(input())
for tt in range(t):
	n = int(input())
	dn = int(n)
	p2 = 0
	p3 = 0
	while dn % 2 == 0:
		dn /= 2
		p2 += 1
	while dn % 3 == 0:
		dn /= 3
		p3 += 1
	if dn != 1:
		print(-1)
	elif p2 > p3:
		print(-1)
	else:
		print(2 * p3 - p2)


t = int(input())
for test in range(t):
	n = int(input())
	(ans, f) = (0, 0)
	while n != 1:
		if n % 6 == 0:
			ans += 1
			n = n // 6
		elif n % 3 == 0:
			ans += 2
			n = n // 3
		else:
			f = 1
			break
	if f == 1:
		print(-1)
	else:
		print(ans)


t = int(input())
s = []
i = 2
while i < 10 ** 9:
	s.append(i)
	i = i * 2
for t1 in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		y = 0
		if n in s:
			print(-1)
			continue
		while True:
			while n % 6 == 0:
				n = n / 6
				y += 1
			if n == 1:
				break
			n *= 2
			y += 1
			if n > 10 ** 9:
				y = -1
				break
		print(y)


for _ in '0' * int(input()):
	n = int(input())
	o = True
	s = 0
	while n != 1:
		if n & 1:
			n = n * 2
			s += 1
		elif n >= 6 and n % 6 == 0:
			s += 1
			n = int(n / 6)
		else:
			o = False
			break
	if o:
		print(s)
	else:
		print('-1')


cases = []
for i in range(int(input())):
	cases.append(int(input()))

def solve(case):
	n = case
	steps = 0
	while case > 1 and case % 3 == 0:
		if case % 2 == 0:
			case /= 6
		else:
			case *= 2
		steps += 1
	if case == 1:
		print(steps)
	else:
		print('-1')
for case in cases:
	solve(case)


def div(n, c):
	f = 0
	if n == 1:
		return c
	if n % 6 != 0 and n % 3 != 0:
		return 1000000001
	if n % 6 == 0:
		f = 1
		x = div(n // 6, c + 1)
	else:
		x = 1000000001
	if n % 3 == 0 and f == 0:
		y = div(n // 3, c + 2)
	else:
		y = 1000000001
	c = min(x, y)
	return c
for _ in range(int(input())):
	n = int(input())
	ans = div(n, 0)
	if ans > 1000000000:
		print(-1)
	else:
		print(ans)


t = int(input())
import math
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
		continue
	p = 0
	while n % 3 == 0:
		n = n / 3
		p += 1
	n = int(n)
	p2 = 0
	while n % 2 == 0:
		n = n / 2
		p2 += 1
	if n != 1:
		print(-1)
	elif p2 > p:
		print(-1)
	else:
		print(p + (p - p2))


import math
t = int(input())
for _ in range(t):
	n = int(input())
	c2 = 0
	c3 = 0
	while n % 2 == 0:
		n = n // 2
		c2 += 1
	while n % 3 == 0:
		n = n // 3
		c3 += 1
	if n != 1:
		print(-1)
		continue
	if c2 <= c3:
		print(c3 + (c3 - c2))
	else:
		print(-1)


t = int(input())
i = 0
output = []
while i < t:
	n = int(input())
	threeCount = 0
	twoCount = 0
	if n == 1:
		output.append(0)
	else:
		while n % 3 == 0:
			threeCount += 1
			n = n / 3
		while n % 2 == 0:
			twoCount += 1
			n = n / 2
		if twoCount > threeCount or n > 1:
			output.append(-1)
		else:
			output.append(2 * threeCount - twoCount)
	i += 1
for x in output:
	print(x)


for _ in range(int(input())):
	n = int(input())
	x = n
	(count2, count3) = (0, 0)
	flag = 0
	while x:
		if x == 1:
			break
		if x % 2 == 0:
			x //= 2
			count2 += 1
		elif x % 3 == 0:
			x //= 3
			count3 += 1
		else:
			print(-1)
			flag = 1
			break
	if flag == 0:
		if count2 > count3:
			print(-1)
		else:
			print(count3 + (count3 - count2))


def answer(n):
	if n == 1:
		return 0
	count2 = 0
	while n % 2 == 0:
		count2 += 1
		n = n // 2
	count3 = 0
	while n % 3 == 0:
		count3 += 1
		n = n // 3
	if n == 1:
		if count2 > count3:
			return -1
		else:
			return count3 - count2 + count3
	else:
		return -1
t = int(input())
for i in range(t):
	n = int(input())
	print(answer(n))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def solve():
	for _ in range(int(input())):
		n = int(input())
		dic = {2: 0, 3: 0}
		while n % 2 == 0:
			dic[2] += 1
			n //= 2
		while n % 3 == 0:
			dic[3] += 1
			n //= 3
		if n > 1 or dic[2] > dic[3]:
			print(-1)
			continue
		print(dic[3] - dic[2] + dic[3])
solve()


T = int(input())
for i in range(T):
	n = int(input())
	c = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
		elif n % 6 == 3:
			n = n * 2
		else:
			c = -1
			break
		c = c + 1
	print(c)


n = int(input())
for i in range(n):
	m = int(input())
	a = 0
	b = 0
	for j in range(1000000000):
		if m % 3 == 0:
			m = m / 3
			a += 1
		elif m / 2 % 1 == 0:
			m = m / 2
			b += 1
		elif m == 1:
			break
		else:
			b = 1
			a = 0
			break
	if a >= b:
		print(2 * a - b)
	else:
		print(-1)


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def modefiedSieve():
	mx = 2750132
	sieve = [-1] * mx
	for i in range(2, mx):
		if sieve[i] == -1:
			sieve[i] = i
			for j in range(i * i, mx, i):
				if sieve[j] == -1:
					sieve[j] = i
	return sieve

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]))

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def maxSubArraySum(a, size):
	maxint = 10 ** 10
	max_so_far = -maxint - 1
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def solve():
	n = int(input())
	if n == 1:
		print(0)
		return
	op = 0
	while n % 3 == 0:
		if n % 6 == 0:
			op += 1
			n = n // 6
		else:
			op += 2
			n *= 2
			n = n // 6
	if n == 1:
		print(op)
		return
	print(-1)
t = 1
t = int(input())
for _ in range(t):
	solve()


def result(num, count):
	div_2 = True if num % 2 == 0 else False
	div_3 = True if num % 3 == 0 else False
	if div_2 and div_3:
		count += 1
		return result(num // 6, count)
	elif div_3:
		num *= 2
		count += 2
		return result(num // 6, count)
	elif num == 1:
		return count
	else:
		return -1
for t in range(int(input())):
	count = 0
	value = result(int(input()), count)
	print(value)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	n = int(ri())
	if n == 1:
		print(0)
	else:
		cnt2 = 0
		while n % 2 == 0:
			n = n // 2
			cnt2 += 1
		p = 3
		cnt3 = 0
		while n % p == 0:
			n = n // p
			cnt3 += 1
		if n != 1 or cnt2 > cnt3:
			print(-1)
		else:
			print(cnt3 - cnt2 + cnt3)


t = int(input())
for _ in range(t):
	n = int(input())
	p2 = 0
	while not n % 2:
		p2 += 1
		n /= 2
	p3 = 0
	while not n % 3:
		p3 += 1
		n /= 3
	if n == 1 and p2 <= p3:
		print(2 * p3 - p2)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	a1 = 0
	a2 = 0
	while n % 2 == 0:
		n = n / 2
		a1 += 1
	while n % 3 == 0:
		n = n / 3
		a2 += 1
	if n == 1 and a2 >= a1:
		print(2 * a2 - a1)
	else:
		print(-1)


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()
for _ in range(int(input())):
	k = 0
	q = int(input())
	while q % 6 == 0:
		k += 1
		q //= 6
	while q % 3 == 0:
		k += 2
		q //= 3
	if q == 1:
		print(k)
	else:
		print(-1)


t = int(input())
nums = []
for i in range(t):
	nums.append(int(input()))
output = []
for n in nums:
	if n == 1:
		output.append(0)
	elif n % 3 != 0:
		output.append(-1)
	else:
		counter2 = 0
		counter3 = 0
		while n % 2 == 0:
			n = n // 2
			counter2 += 1
		while n % 3 == 0:
			n = n // 3
			counter3 += 1
		if n != 1 or counter2 > counter3:
			output.append(-1)
		elif counter3 >= counter2:
			output.append(counter3 + (counter3 - counter2))
for x in output:
	print(x)


test = int(input())
for j in range(test):
	n = int(input())
	c = 0
	while n % 3 == 0:
		if n % 6 == 0:
			n //= 6
			c += 1
		else:
			n *= 2
			c += 1
	if n == 1:
		print(c)
	else:
		print(-1)


test = int(input())
for i in range(test):
	a = int(input())
	k = 0
	if a == 1:
		print('0')
	else:
		while a != 1:
			if a % 6 == 0:
				a /= 6
				k += 1
			elif a == 2 or a == 4 or a == 5:
				k = -1
				break
			elif a % 3 == 0:
				a = a * 2
				k += 1
			elif a % 6 != 0 and a % 3 != 0:
				k = -1
				break
		print(k)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print('0')
	else:
		twocnt = 0
		threecnt = 0
		flag = 1
		while flag == 1 and n > 1:
			if n % 2 == 0:
				flag = 1
				while n % 2 == 0:
					n = n // 2
					twocnt += 1
				if n == 1:
					break
			elif n % 3 == 0:
				flag = 1
				while n % 3 == 0:
					n = n // 3
					threecnt += 1
				if n == 1:
					break
			else:
				flag = 0
				break
		if twocnt > threecnt or flag == 0:
			print('-1')
		else:
			moves = 0
			moves += threecnt - twocnt
			moves += threecnt
			print(moves)


def find(x):
	exp2 = 0
	exp3 = 0
	while x % 2 == 0:
		x = x // 2
		exp2 += 1
	while x % 3 == 0:
		x = x // 3
		exp3 += 1
	if x != 1:
		return -1
	if exp3 < exp2:
		return -1
	return exp3 - exp2 + exp3
for _ in range(int(input())):
	x = int(input())
	print(find(x))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = b = 0
		while n % 2 == 0:
			n //= 2
			a += 1
		while n % 3 == 0:
			n //= 3
			b += 1
		if n > 1 or a > b:
			rr.append(-1)
		else:
			rr.append(b * 2 - a)
	return JA(rr, '\n')
print(main())


def check(n):
	if n == 1:
		return 0
	(two_count, three_count) = (0, 0)
	while n % 2 == 0:
		n /= 2
		two_count += 1
	while n % 3 == 0:
		n /= 3
		three_count += 1
	if two_count > three_count:
		return -1
	if n != 1:
		return -1
	times_2 = three_count - two_count
	div_6 = three_count
	return times_2 + div_6
t = int(input())
for i in range(t):
	n = int(input())
	print(check(n))


for i in range(int(input())):
	n = int(input())
	(k, p) = (0, 0)
	while n % 3 == 0:
		k = k + 1
		n = n / 3
	while n % 2 == 0:
		p = p + 1
		n = n / 2
	if n == 1:
		if k >= p:
			print(k + k - p)
		else:
			print('-1')
	else:
		print('-1')


import math
n = int(input())
C = []
for i in range(n):
	C.append(input())
for i in range(n):
	x = int(C[i])
	if x == 1:
		print(0)
	else:
		num = x
		num1 = 0
		num2 = 0
		while num % 2 == 0:
			num = num / 2
			num1 += 1
			if num % 2 != 0:
				break
		while num % 3 == 0:
			num = num / 3
			num2 += 1
		if num2 < num1 or num != 1:
			print(-1)
		elif num2 == num1:
			print(num2)
		else:
			print(num2 - num1 + num2)


for _ in range(int(input())):
	a = int(input())
	(b, c, j) = (0, 0, 1)
	while j:
		j = 0
		if a % 2 == 0:
			b += 1
			j = 1
			a = a / 2
		if a % 3 == 0:
			c += 1
			j = 1
			a = a / 3
	if a != 1 or b > c:
		print(-1)
	else:
		print(c + c - b)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	ans = 0
	if n == 1:
		print(ans)
		continue
	while n != 1:
		ans += 1
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		if ans > 100:
			ans = -1
			break
	print(ans)


amt = int(input(''))
for i in range(amt):
	n = int(input(''))
	listnum = []
	ans = []
	loop = True
	if n == 1:
		print(0)
	else:
		while loop:
			if n % 6 == 0:
				n = n / 6
				listnum.append(1)
			elif n % 3 == 0:
				n = n / 3
				listnum.append(1)
				listnum.append(1)
			else:
				loop = False
		if n == 1:
			print(len(listnum))
		else:
			print(-1)


for i in range(int(input())):
	s = input()
	l = list(map(int, s.split()))
	p = int(s)
	c = 0
	if sum(l) == 1:
		print(0)
	elif sum(l) % 3 == 0:
		while p > 1:
			if sum(l) % 3 == 0 and p % 2 == 0:
				p = int(p / 6)
				c = c + 1
				l = list(map(int, str(p).split()))
			elif sum(l) % 3 == 0 and p % 2 != 0:
				p = p * 2
				c = c + 1
				l = list(map(int, str(p).split()))
			else:
				print(-1)
				break
		if p == 1:
			print(c)
	else:
		print(-1)


tests = int(input())
for _ in range(tests):
	number = int(input())
	if number == 1:
		print(0)
	else:
		dop_1 = 0
		dop_2 = 0
		while number % 6 == 0:
			number //= 6
			dop_1 += 1
		while number % 3 == 0:
			number //= 3
			dop_2 += 1
		if number != 1 or (dop_1 == 0 and dop_2 == 0):
			print(-1)
		else:
			print(dop_1 + 2 * dop_2)


import sys
import math
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
for t in range(int(input())):
	n = int(input())
	step = 0
	notPoss = False
	while n > 1:
		if n % 6 == 0:
			n = n // 6
		else:
			if not n % 3 == 0:
				notPoss = True
				break
			n = n * 2
		step += 1
	if notPoss:
		print(-1)
	else:
		print(step)


t = int(input())
for _ in range(t):
	n = int(input())
	z = n
	if n == 1:
		print(0)
	else:
		x = 0
		y = 0
		if n % 2 == 0 or n % 3 == 0:
			while n % 2 == 0:
				n = n // 2
				x = x + 1
			while n % 3 == 0:
				n = n // 3
				y = y + 1
			if y > 0 and x < y and (2 ** x * 3 ** y == z):
				k = y - x
				n = z * 2 ** k
				c = k
				while n % 6 == 0:
					n = n // 6
					c = c + 1
				print(c)
			elif y > 0 and x == y and (2 ** x * 3 ** y == z):
				k = 0
				while z % 6 == 0:
					z = z // 6
					k = k + 1
				print(k)
			else:
				print(-1)
		else:
			print(-1)


o = []
for i in range(int(input())):
	n = int(input())
	r = 0
	while n % 3 == 0:
		r += n % 2 + 1
		n //= 6 - n % 2 * 3
	if n > 1:
		o.append('-1')
	else:
		o.append(r)
for i in o:
	print(i)


t = int(input())
while t:
	n = int(input())
	two = 0
	three = 0
	value = 0
	while n != 1:
		if n % 2 == 0:
			n = n // 2
			two = two + 1
		elif n % 3 == 0:
			n = n // 3
			three = three + 1
		else:
			value = -1
			n = 1
	if two > three:
		print(-1)
	elif value == -1:
		print(-1)
	else:
		value = three + (three - two)
		print(value)
	t = t - 1


t = int(input())
for i in range(t):
	n = int(input())
	pos = True
	count = 0
	while pos:
		if n == 1:
			print(count)
			pos = False
		elif n % 6 == 0:
			n = n // 6
			count += 1
		elif n % 3 == 0:
			n = n * 2
			count += 1
		else:
			pos = False
			print(-1)


t = int(input())
while t:
	n = int(input())
	if n == 1:
		print(0)
	else:
		cn = 0
		f = 0
		while n != 1:
			if n % 6 != 0:
				if f == 1:
					print(-1)
					break
				else:
					n *= 2
					f = 1
					cn += 1
			elif n % 6 == 0:
				n //= 6
				cn += 1
				f = 0
		else:
			print(cn)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	cnt = 0
	while n % 6 == 0 or n % 3 == 0:
		if n == 1:
			break
		if n % 6 == 0:
			cnt += 1
			n //= 6
		elif n % 3 == 0:
			n *= 2
			cnt += 1
	if n != 1:
		cnt = -1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	counter = 0
	operations = 0
	while n != 1 and operations != 100:
		if n == 1:
			break
		else:
			operations += 1
			counter += 1
			if n % 6 == 0:
				n /= 6
			else:
				n *= 2
	print(counter if operations != 100 else -1)


t = int(input())
for i in range(t):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		n /= 2
		two += 1
	while n % 3 == 0:
		n /= 3
		three += 1
	if n != 1 or two > three:
		ans = -1
	else:
		ans = three + (three - two)
	print(ans)


t = int(input())
for x in range(t):
	y = int(input())
	c = 0
	f = 0
	if y == 1:
		print('0')
		continue
	while y != 1:
		if y % 6 == 0:
			y = y // 6
			c += 1
		elif y % 3 == 0:
			y = y * 2
			c += 1
		else:
			print('-1')
			f = 1
			break
	if f != 1:
		print(c)


from sys import stdin, stdout
from bisect import bisect, bisect_left, bisect_right

def gt():
	return map(int, stdin.readline().split())

def gi():
	return int(stdin.readline())

def gl():
	return list(map(int, stdin.readline().split()))

def gs():
	return stdin.readline()
t = gi()
for _ in range(t):
	n = gi()
	count2 = 0
	count3 = 0
	while n % 2 == 0:
		n = n // 2
		count2 += 1
	while n % 3 == 0:
		n = n // 3
		count3 += 1
	if n != 1 or count3 < count2:
		print(-1)
		continue
	elif count2 == count3:
		print(count2)
	elif count2 > count3:
		print(-1)
	else:
		print(2 * count3 - count2)


def find_powers(num):
	two = 0
	three = 0
	while num > 0 and num % 2 == 0:
		two += 1
		num = num / 2
	while num > 0 and num % 3 == 0:
		three += 1
		num = num / 3
	if num != 1:
		return (-1, -1)
	return (two, three)
cases = int(input())
for _ in range(cases):
	n = int(input())
	(two, three) = find_powers(n)
	if two == -1:
		print(-1)
	elif two - three > 0:
		print(-1)
	else:
		print(2 * three - two)


def biggestPower(n, p):
	count = 0
	while n % p == 0:
		count += 1
		n /= p
	return count
for i in range(int(input())):
	n = int(input())
	twos = biggestPower(n, 2)
	threes = biggestPower(n, 3)
	if n / 2 ** twos / 3 ** threes != 1:
		print(-1)
	elif twos == threes:
		print(twos)
	elif twos > threes:
		print(-1)
	else:
		print(2 * threes - twos)


for _ in range(int(input())):
	n = int(input())
	k = 0
	p = 0
	if n == 2:
		print(-1)
	else:
		while n != 1:
			if n % 6 == 0:
				n = n // 6
			elif n % 3 == 0:
				n = n * 2
			if n % 3 != 0 and n > 3 or n == 2:
				p += 1
				break
			k += 1
		if p > 0:
			print(-1)
		else:
			print(k)


for _ in range(int(input())):
	c = int(input())
	t = c
	f = {2: 0, 3: 0}
	while c % 2 == 0:
		c //= 2
		f[2] += 1
	while c % 3 == 0:
		c //= 3
		f[3] += 1
	if 2 ** f[2] * 3 ** f[3] != t or f[2] > f[3]:
		print(-1)
	else:
		print(2 * f[3] - f[2])


import sys, math, cmath, time
start_time = time.time()

def solve():
	n = inp()
	q = 0
	w = 0
	while n % 3 == 0:
		q = q + 1
		n = n // 3
	while n % 2 == 0:
		w = w + 1
		n = n // 2
	if n == 1:
		if q > w:
			qq = 2 * q - w
		elif q == w:
			qq = q
		else:
			qq = -1
	else:
		qq = -1
	print(qq)

def main():
	global tt
	if not ONLINE_JUDGE:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	t = 1
	t = inp()
	for tt in range(t):
		solve()
	if not ONLINE_JUDGE:
		print('Time Elapsed :', time.time() - start_time, 'seconds')
		sys.stdout.close()

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	return input().strip()

def invr():
	return map(int, input().split())

def counter(a):
	q = [0] * max(a)
	for i in range(len(a)):
		q[a[i] - 1] = q[a[i] - 1] + 1
	return q

def string_counter(a):
	q = [0] * 26
	for i in range(len(a)):
		q[ord(a[i]) - 97] = (q[ord(a[i]) - 97], 1)
	return q

def factors(n):
	q = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			q.append(i)
			q.append(n // i)
	return list(sorted(list(set(q))))

def prime_factors(n):
	q = []
	while n % 2 == 0:
		q.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			q.append(i)
			n = n // i
	if n > 2:
		q.append(n)
	return list(sorted(q))
ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
	input = sys.stdin.readline
main()


def factors(x):
	factors = []
	while x % 2 == 0:
		x /= 2
		factors.append(2)
	while x % 3 == 0:
		x /= 3
		factors.append(3)
	return (int(x), factors)

def solution():
	from collections import Counter
	t = int(input())
	ans = []
	while t:
		n = int(input())
		(left, two_three) = factors(n)
		if left != 1:
			ans.append(-1)
		else:
			f = Counter(two_three)
			(two, three) = (f[2], f[3])
			remaining = three - two
			if remaining >= 0:
				ans.append(two + 2 * remaining)
			else:
				ans.append(-1)
		t -= 1
	for a in ans:
		print(a)
solution()


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		two = three = 0
		while n % 2 == 0:
			two += 1
			n //= 2
		while n % 3 == 0:
			three += 1
			n //= 3
		if two > three or n > 1:
			print('-1')
		else:
			print(2 * three - two)
main()


def check(n):
	if n % 2 != 0 and n % 3 != 0:
		return -1
	(mul2, mul3) = (0, 0)
	while n % 2 == 0:
		mul2 += 1
		if n % 2 == 0:
			n //= 2
		else:
			break
	while n % 3 == 0:
		mul3 += 1
		if n % 3 == 0:
			n //= 3
		else:
			break
	if mul2 > mul3 or n > 1:
		return -1
	return 2 * mul3 - mul2
t = int(input())
arr = []
for i in range(t):
	arr.append(int(input()))
for i in range(t):
	if arr[i] == 1:
		print(0)
	else:
		print(check(arr[i]))


from sys import stdin
a = int(stdin.readline())
for i in range(0, a):
	n = int(stdin.readline())
	times3 = 0
	times2 = 0
	while n / 3 == n // 3:
		times3 += 1
		n /= 3
	while n / 2 == n // 2:
		times2 += 1
		n /= 2
	if times2 > times3:
		print(-1)
	elif n != 1:
		print(-1)
	elif times2 <= times3:
		print(2 * times3 - times2)


t = int(input())
while t:
	n = int(input())
	res = 0
	while True:
		if n % 6 == 0:
			res += 1
			n = n / 6
		else:
			break
	while True:
		if n % 3 == 0:
			res += 2
			n = n / 3
		if n % 3 != 0:
			if n != 1:
				print('-1')
				break
			else:
				print(res)
				break
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	fac2 = 0
	fac3 = 0
	while n % 2 == 0:
		n = n // 2
		fac2 += 1
	while n % 3 == 0:
		n = n / 3
		fac3 += 1
	if n > 1 or fac3 < fac2:
		print(-1)
	else:
		fac3 -= fac2
		print(fac3 * 2 + fac2)


t = int(input())
for i in range(t):
	x = int(input())
	ans = 0
	yep = False
	while x != 1:
		if x % 3 != 0:
			print(-1)
			yep = True
			break
		elif x % 6 == 0:
			x = x / 6
			ans += 1
		else:
			x = x * 2
			ans += 1
	if not yep:
		print(ans)


def power_of_two(x):
	return x and (not x & x - 1)

def prime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for i in range(int(input())):
	n = int(input())
	ans = 0
	f = 1
	while True:
		while n % 6 == 0:
			n = n // 6
			ans += 1
		if n == 1:
			break
		if 2 * n % 6 != 0:
			f = 0
			break
		n = n * 2
		ans += 1
	if f == 0:
		ans = -1
	print(ans)


def mult(b, c):
	if b == 1:
		return c
	elif b % 3 != 0:
		return -1
	elif b % 6 == 0:
		return mult(b // 6, c + 1)
	else:
		return mult(b * 2, c + 1)
t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	print(mult(n, c))


t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	flag = True
	n = int(input())
	res = 0
	if n % 3 != 0 and n != 1:
		flag = False
	while n != 1 and n % 3 == 0:
		if n % 2 == 0:
			n //= 6
			res += 1
		else:
			n *= 2
			n //= 6
			res += 2
		if n % 3 != 0 and n != 1:
			flag = False
			break
	if not flag:
		print(-1)
	else:
		print(res)


t = int(input())
for i in range(t):
	n = int(input())
	s = set()
	s.add(n)
	flag = 0
	moves = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			moves += 1
		elif n % 3 == 0:
			moves += 1
			n = n * 2
		else:
			pass
		if n in s:
			flag = 1
			break
		s.add(n)
	if flag == 0:
		print(moves)
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	k = 0
	n1 = n
	while n > 1 and n < n1 ** 2:
		if n % 6 == 0:
			n = n // 6
			k += 1
		else:
			n = n * 2
			k += 1
	if n == 1:
		print(k)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a3 = 0
	a2 = 0
	while n % 3 == 0:
		n = n // 3
		a3 += 1
	while not n & 1:
		n = n // 2
		a2 += 1
	if a3 < a2:
		print(-1)
	elif n != 1:
		print(-1)
	else:
		print(a3 + (a3 - a2))


a = int(input())
dict_val = {1: 0, 2: -1, 3: 2, 4: -1, 5: -1, 6: 1, 7: -1, 8: -1, 9: 4}
for i in range(a):
	val = int(input())
	breakval = 'hi'
	if val < 10:
		print(dict_val[val])
	else:
		quo = val
		count_val = int(0)
		count_val2 = int(0)
		while quo != 1:
			rem = quo % 6
			if rem == 0:
				quo = quo // 6
				count_val = count_val + 1
			else:
				break
		while quo != 1:
			rem = quo % 3
			if rem == 0:
				quo = quo // 3
				count_val2 = count_val2 + 1
			else:
				break
		if quo == 1:
			print(count_val + 2 * count_val2)
		else:
			print('-1')


def make_one(n):
	if n == 1:
		return 0
	pow_of_two = 0
	temp = n
	while not temp % 2:
		temp //= 2
		pow_of_two += 1
	pow_of_three = 0
	while not temp % 3:
		temp //= 3
		pow_of_three += 1
	if temp != 1:
		return -1
	if pow_of_two > pow_of_three:
		return -1
	else:
		return 2 * pow_of_three - pow_of_two
test_cases = int(input())
while test_cases:
	print(make_one(int(input())))
	test_cases -= 1


t = int(input())
for i in range(t):
	x = int(input())
	num3 = 0
	num2 = 0
	while x % 3 == 0:
		x = x / 3
		num3 = num3 + 1
	while x % 2 == 0:
		x = x / 2
		num2 = num2 + 1
	if x != 1 or num3 < num2:
		print(-1)
	else:
		print(2 * num3 - num2)


def shef(n):
	if n == 1:
		return 0
	count = 0
	while True:
		while n % 6 == 0:
			n = n // 6
			count += 1
		if n % 3 == 0:
			n = n * 2
			count += 1
		else:
			break
	if n == 1:
		return count
	return -1
for t in range(int(input())):
	n = int(input())
	print(shef(n))


n = int(input())
t = []
for j in range(n):
	a = int(input())
	b = a
	c = b
	zwei = 0
	drei = 0
	while b % 2 == 0:
		zwei += 1
		b = int(b / 2)
	while b % 3 == 0:
		drei += 1
		b = int(b / 3)
	if 2 ** zwei * 3 ** drei == c:
		if drei != 0 and zwei <= drei:
			t.append(2 * drei - zwei)
		elif c == 1:
			t.append(0)
		else:
			t.append(-1)
	else:
		t.append(-1)
for h in t:
	print(h)


def counting(n):
	minimum = 0
	while True:
		if n == 1:
			return minimum
		if n % 6 == 0 or n % 3 == 0:
			if n % 6 == 0:
				n = n // 6
				minimum = minimum + 1
			else:
				n = n * 2
				minimum = minimum + 1
		else:
			return -1
howManyNumber = int(input())
for i in range(0, howManyNumber):
	N = int(input())
	if N == 1:
		print('0')
	else:
		print(counting(N))


from sys import stdin
input = stdin.readline
INF = 10 ** 9 + 7
MAX = 10 ** 7 + 7
MOD = 10 ** 9 + 7
for Ti in range(int(input().strip())):
	n = int(input().strip())
	tc = 0
	thc = 0
	while n % 2 == 0:
		n //= 2
		tc += 1
	while n % 3 == 0:
		n //= 3
		thc += 1
	if tc <= thc and n == 1:
		ans = 0
		ans += thc - tc
		ans += thc
		print(ans)
	else:
		print('-1')


import sys
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
t = int(input())
for _ in range(t):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		n = n // 2
		two += 1
	while n % 3 == 0:
		n = n // 3
		three += 1
	if n != 1:
		print(-1)
		continue
	if two > three:
		print(-1)
		continue
	print(three - two + three)


from sys import stdin
a = int(stdin.readline().rstrip())
for i in range(a):
	n = int(stdin.readline().rstrip())
	c3 = 0
	while n % 3 == 0:
		n = n // 3
		c3 += 1
	c2 = 0
	while n % 2 == 0:
		n = n // 2
		c2 += 1
	if n != 1:
		print(-1)
	elif c2 > c3:
		print(-1)
	else:
		print(c2 + 2 * (c3 - c2))


from collections import deque
import sys

def inp():
	return sys.stdin.readline().strip()
for _ in range(int(inp())):
	n = int(inp())
	p2 = 0
	p3 = 0
	while n > 1 and n % 3 == 0:
		n //= 3
		p3 += 1
	while n > 1 and n % 2 == 0:
		n //= 2
		p2 += 1
	if n > 1 or p2 > p3:
		print(-1)
		continue
	print(p3 - p2 + p3)


t = int(input())
while t != 0:
	t -= 1
	x = int(input())
	cnt = 0
	while x != 1:
		if x % 6 == 0:
			x = x / 6
			cnt += 1
		elif x % 3 == 0:
			x = x * 2
			cnt += 1
		else:
			break
	if x == 1:
		print(cnt)
	else:
		print(-1)


def Solve(n):
	if n == 1:
		return 0
	steps = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
		else:
			n = n * 2
			if n % 6 != 0:
				return -1
		steps += 1
	return steps
T = int(input())
for _ in range(T):
	n = int(input())
	print(Solve(n))


from sys import stdin, stdout
n = int(stdin.readline())
for i in range(n):
	two = three = 0
	a = int(stdin.readline())
	while a % 2 == 0:
		a //= 2
		two += 1
	while a % 3 == 0:
		a //= 3
		three += 1
	if a != 1:
		stdout.write('-1' + '\n')
		continue
	if three < two:
		stdout.write('-1' + '\n')
		continue
	k = three * 2 - two
	stdout.write(str(k) + '\n')


tests = int(input())
for _ in range(tests):
	n = int(input())
	d = dict()
	d[n] = 1
	count = 0
	if n == 1:
		print(0)
		continue
	while n != 1:
		if n % 3 != 0:
			print(-1)
			break
		if n % 6 == 0:
			n //= 6
			if n in d:
				print(-1)
				break
			d[n] = 1
		else:
			n *= 2
			if n in d:
				print(-1)
				break
			d[n] = 1
		count += 1
	if n == 1:
		print(count)


from functools import lru_cache
t = int(input())

@lru_cache(maxsize=None)
def recurse(m, n):
	if m == n:
		return 0
	if m > n:
		return -0.5
	if n % 2 == 1:
		return 1 + recurse(m, n * 2)
	else:
		return 1 + recurse(m, n / 6)
while t != 0:
	t -= 1
	n = int(input())
	seen = []
	i = 1
	fa = recurse(1, n)
	if int(fa) != fa:
		print(-1)
	else:
		print(fa)


def div_mult(n):
	moves = 0
	if n == 1:
		return 0
	if n == 2:
		return -1
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			if n == 2:
				return -1
		else:
			n *= 2
			if n % 3 != 0:
				return -1
		moves += 1
	return moves
t = int(input())
for i in range(t):
	n = int(input())
	print(div_mult(n))


T = int(input().strip())
for t in range(T):
	n = int(input().strip())
	p2 = 0
	while n % 2 == 0:
		n //= 2
		p2 += 1
	p3 = 0
	while n % 3 == 0:
		n //= 3
		p3 += 1
	if n != 1 or p2 > p3:
		print(-1)
	else:
		print(p3 + (p3 - p2))


t = int(input())
for u in range(t):
	n = int(input())
	count = 0
	while n % 6 == 0:
		n = n // 6
		count += 1
	while n % 3 == 0:
		n = n // 3
		count += 2
	if n == 1:
		print(count)
	else:
		print(-1)


for _ in range(int(input())):
	t = int(input())
	ans = 0
	c = 0
	if t == 1:
		print('0')
	elif t == 2 or t == 4 or t == 5:
		print('-1')
	elif t == 3:
		print('2')
	elif t == 6:
		print('1')
	else:
		while t > 1:
			if t % 6 != 0 and t * 2 % 6 != 0:
				ans = 1
				break
			elif t % 6 == 0:
				t = t // 6
				c += 1
			else:
				t = t * 2
				c += 1
		if ans == 0:
			print(c)
		else:
			print('-1')


def twosix(n):
	k = 0
	if n == 1:
		return 0
	else:
		while n > 1:
			if n % 6 == 0:
				n = n / 6
				k += 1
			elif n % 3 == 0:
				n = n * 2 / 6
				k += 2
			else:
				n = 0
				k = 0
		if n == 1:
			return k
		elif 0 <= n < 1:
			return -1
t = int(input())
for i in range(t):
	n = int(input())
	print(twosix(n))


t = int(input())
for test in range(t):
	n = int(input())
	cur = n
	steps = 0
	while cur > 1:
		cur_mod = cur % 6
		if cur_mod in [1, 2, 4, 5]:
			break
		elif cur_mod == 3:
			cur = cur / 3
			steps += 2
		elif cur_mod == 0:
			cur = cur / 6
			steps += 1
		else:
			print('ah shit')
			exit(-1)
	if cur == 1:
		print(steps)
	else:
		print(-1)


n = int(input())
for _ in range(n):
	a = int(input())
	c = 0
	x = 0
	while a != 1:
		if a % 6 == 0:
			a //= 6
		else:
			a *= 2
		c += 1
		if c > 100:
			x = 1
			break
	print(c if x == 0 else -1)


import sys, math
input = sys.stdin.readline

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input().strip()

def listStr():
	return list(input().strip())
import collections as col
import math

def solve():
	N = getInt()
	ans = 0
	while N > 1:
		if N % 3 != 0:
			return -1
		if N % 2 != 0:
			ans += 1
			N *= 2
		ans += 1
		N //= 6
	return ans
for _ in range(getInt()):
	print(solve())


import math
from collections import Counter
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		moves = 0
		while n != 1:
			if n % 6 == 0:
				n //= 6
				moves += 1
			elif n % 3 == 0:
				n *= 2
				moves += 1
			else:
				break
		if n == 1:
			print(moves)
		else:
			print(-1)


t = int(input())
numbers = [int(input()) for i in range(t)]
for n in numbers:
	ops = 0
	while n != 1:
		if n % 6 == 2 or n % 6 == 4 or n % 6 == 5 or (n % 6 == 1):
			ops = -1
			break
		elif n % 6 == 0:
			n = n / 6
			ops += 1
		elif n % 6 == 3:
			n = n * 2
			ops += 1
	print(ops)


import sys
t = int(sys.stdin.readline().strip())
while t:
	n = int(sys.stdin.readline().strip())
	flag = 0
	count = 0
	while flag == 0 and n > 1:
		flag = 1
		if n % 6 == 0:
			flag = 0
			n = n // 6
			count += 1
		elif n % 3 == 0:
			flag = 0
			n *= 2
			count += 1
	if flag == 1:
		print(-1)
	else:
		print(count)
	t -= 1


import sys
import math
input = lambda : sys.stdin.readline().rstrip()

def solve(n):
	temp = n
	num2 = 0
	num3 = 0
	while n % 2 == 0:
		num2 += 1
		n = n // 2
	while n % 3 == 0:
		num3 += 1
		n = n // 3
	if n != 1 or num2 > num3:
		return -1
	else:
		return num3 - num2 + num3

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		print(solve(n))
main()


t = int(input())
while t:
	t = t - 1
	n = int(input())
	if n == 1:
		print(0)
	else:
		flag = False
		count = 0
		while True:
			if n % 6 == 0:
				n = n // 6
			elif n * 2 % 6 == 0:
				n = n * 2
			elif n == 1:
				break
			else:
				flag = True
				break
			count += 1
		if flag:
			print(-1)
		else:
			print(count)


def log(n, k):
	ans = 0
	while n % k == 0:
		ans += 1
		n //= k
	return [n, ans]
for _ in range(int(input())):
	n = int(input())
	a = log(n, 3)
	n = a[0]
	t = a[1]
	a = log(n, 2)
	n = a[0]
	tw = a[1]
	if tw > t or n != 1:
		print(-1)
	else:
		print(max(t, tw) + abs(t - tw))


def answer(num):
	log2 = 0
	log3 = 0
	while num % 2 == 0:
		num /= 2
		log2 += 1
	while num % 3 == 0:
		num /= 3
		log3 += 1
	if num != 1:
		return -1
	if log2 > log3:
		return -1
	return log2 + 2 * (log3 - log2)
t = int(input())
while t > 0:
	num = int(input())
	print(answer(num))
	t -= 1


import math
for _ in range(int(input())):
	n = int(input())
	d = {2: 0, 3: 0}
	while n % 2 == 0:
		d[2] += 1
		n = n // 2
	while n % 3 == 0:
		d[3] += 1
		n = n // 3
	if n > 1:
		print(-1)
	elif d[3] < d[2]:
		print(-1)
	else:
		x = d[2]
		y = d[3]
		a = max(x, y)
		b = min(x, y)
		print(2 * a - b)


import math
for _ in range(int(input())):
	x = int(input())
	if x == 1:
		print(0)
	else:
		i = 0
		temp = x
		while temp % 3 == 0:
			temp /= 3
			i += 1
		j = 0
		temp = x
		while temp % 2 == 0:
			temp /= 2
			j += 1
		if i >= j and pow(3, i) * pow(2, j) == x:
			print(i - j + i)
		else:
			print(-1)


t = int(input())
cases = []
for i in range(t):
	n = int(input())
	cases.append(n)

def solve(n):
	count = 0
	while n > 1:
		if n % 6 == 0:
			n /= 6
		elif n * 2 % 6 == 0:
			n *= 2
		else:
			break
		count += 1
	if n == 1:
		return count
	else:
		return -1
for j in cases:
	print(solve(j))


for _ in range(int(input())):
	n = int(input())
	(twos, thrs, othr) = (0, 0, 0)
	while n % 2 == 0 and n != 0:
		twos += 1
		n = n // 2
	while n % 3 == 0 and n != 0:
		thrs += 1
		n = n // 3
	if n > 1:
		othr = 100
	if othr > 0 or thrs < twos:
		print(-1)
		continue
	sixs = min(thrs, twos)
	thrs -= sixs
	twos -= sixs
	print(sixs + thrs + thrs)


t = int(input())
for T in range(t):
	n = int(input())
	twos = 0
	threes = 0
	while n % 2 == 0:
		n //= 2
		twos += 1
	while n % 3 == 0:
		n //= 3
		threes += 1
	if n != 1 or twos > threes:
		print(-1)
	else:
		print(threes * 2 - twos)


t = int(input())
while t:
	t -= 1
	n = int(input())
	ans = -1
	cnt2 = 0
	cnt3 = 0
	while n and n % 2 == 0:
		cnt2 += 1
		n //= 2
	while n and n % 3 == 0:
		cnt3 += 1
		n //= 3
	if n == 1 and cnt3 >= cnt2:
		if cnt2 == cnt3:
			ans = cnt2
		else:
			ans = 2 * cnt3 - cnt2
	print(ans)


import math
t = int(input())
for i in range(t):
	n = int(input())
	(ans, ans1) = (0, 0)
	while True:
		if n % 2 == 0:
			ans += 1
			n = n / 2
		else:
			break
	while True:
		if n % 3 == 0:
			ans1 += 1
			n = n / 3
		else:
			break
	if n == 1 and ans1 >= ans:
		print(2 * ans1 - ans)
	else:
		print(-1)


from sys import stdin
nii = lambda : map(int, stdin.readline().split())
lnii = lambda : list(map(int, stdin.readline().split()))
t = int(input())
for i in range(t):
	n = int(input())
	cnt = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			cnt += 1
		elif n % 3 == 0:
			n //= 3
			cnt += 2
		else:
			cnt = -1
			break
	print(cnt)


def solve():
	n = int(input())
	c = 0
	rep = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			rep = 0
		else:
			n = n * 2
			rep += 1
		if rep > 2:
			c = -1
			break
		c += 1
	print(c)
t = 1
t = int(input())
for _ in range(t):
	solve()


def ans(x):
	temp = x
	p2 = 0
	p3 = 0
	for i in range(x):
		if temp % 2 == 0:
			temp //= 2
			p2 += 1
		else:
			break
	for i in range(x):
		if temp % 3 == 0:
			temp //= 3
			p3 += 1
		else:
			break
	if temp != 1:
		print(-1)
	elif p2 > p3:
		print(-1)
	else:
		print(p3 * 2 - p2)
(t, *n) = map(int, open(0).read().split())
for x in n:
	ans(x)


test_case = int(input())
for i in range(test_case):
	n = int(input())
	x = 0
	y = 0
	while not n & 1:
		n >>= 1
		x += 1
	while n % 3 == 0:
		n //= 3
		y += 1
	if n != 1 or x > y:
		print(-1)
		continue
	print(y + y - x)


l = []
for _ in range(int(input())):
	steps = 0
	number = int(input())
	if number == 1:
		l.append(str(0))
		continue
	if number % 3 != 0:
		l.append(str(-1))
		continue
	while number % 3 == 0:
		while number % 6 != 0:
			number = number * 2
			steps = steps + 1
		while number % 6 == 0:
			number = number // 6
			steps = steps + 1
	if number != 1:
		l.append(str(-1))
	else:
		l.append(str(steps))
print('\n'.join(l))


for _ in range(int(input())):
	a = int(input())
	b = a
	c = 0
	d = 0
	while a % 3 == 0:
		a //= 3
		c += 1
	while a % 2 == 0:
		a //= 2
		d += 1
	if a != 1:
		print(-1)
	elif d > c:
		print(-1)
	else:
		print(c + (c - d))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def LAR(n):
	num_3s = 0
	num_6s = 0
	while n % 6 == 0:
		n = n / 6
		num_6s += 1
	while n % 3 == 0:
		n = n / 3
		num_3s += 1
	if n == 1:
		operations = num_3s + num_6s + num_3s
		print(operations)
		return 0
	else:
		print('-1')
		return 0
	print('-1')
test = inp()
for i in range(0, int(test)):
	n = inp()
	num = LAR(n)


from time import time
from random import randint

def algorithm(n):
	operations = 0
	while n > 1:
		if n % 3 != 0:
			return -1
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		operations += 1
	return operations

def algorithm2(n):
	cnt2 = cnt3 = 0
	while n % 2 == 0:
		n //= 2
		cnt2 += 1
	while n % 3 == 0:
		n //= 3
		cnt3 += 1
	if n == 1 and cnt2 <= cnt3:
		return 2 * cnt3 - cnt2
	else:
		return -1

def submit():
	for _ in range(int(input())):
		n = int(input())
		print(algorithm2(n))

def tryOut():
	print(algorithm2(a))
a = 6
submit()


a = int(input())
for i in range(a):
	x = int(input())
	if x == 1:
		print(0)
		continue
	else:
		count = 0
		ans = 0
		while True:
			if x % 3 == 0:
				x //= 3
				count += 1
			else:
				break
		if x & x - 1 != 0:
			print(-1)
		else:
			while x < 2 ** count:
				x *= 2
				ans += 1
			if x > 2 ** count:
				print(-1)
			else:
				print(count + ans)


import sys
import math
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	cnt_2 = 0
	cnt_3 = 0
	if n == 1:
		print(0)
		continue
	if n >= 2:
		while n % 2 == 0:
			cnt_2 += 1
			n = n // 2
		while n % 3 == 0:
			cnt_3 += 1
			n = n // 3
		if n == 1:
			if cnt_2 == cnt_3:
				print(cnt_3)
			elif cnt_2 < cnt_3:
				print(cnt_3 + cnt_3 - cnt_2)
			else:
				print(-1)
		else:
			print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	temp = n
	ans = 0
	count = 0
	check = False
	while temp != 1:
		a = temp % 6 == 0
		if a == True:
			temp = temp / 6
			count = 0
			ans += 1
		else:
			temp = temp * 2
			count += 1
			if count > 20:
				print('-1')
				check = True
				break
			ans += 1
	if check == False:
		print(ans)


t = int(input())
for i in range(t):
	N = int(input())
	k = 0
	while N % 2 == 0:
		N //= 2
		k += 1
	n = 0
	while N % 3 == 0:
		N //= 3
		n += 1
	print(2 * n - k if k <= n and N == 1 else -1)


t = int(input())
while t:
	t -= 1
	n = int(input())
	k = n
	r_3 = 0
	r_2 = 0
	while n % 3 == 0:
		n = n // 3
		r_3 += 1
	while n % 2 == 0:
		n = n // 2
		r_2 += 1
	if k == 1:
		print(0)
	elif n != 1 or r_3 == 0:
		print(-1)
	elif r_2 > r_3:
		print(-1)
	else:
		print(r_3 - r_2 + r_3)


t = int(input())
for i in range(t):
	n = int(input())
	x = n
	y = 0
	z = 0
	while x % 3 == 0:
		y += 1
		x //= 3
	while x % 2 == 0:
		z += 1
		x //= 2
	if x != 1 or z > y:
		print(-1)
	else:
		print(y + (y - z))


for _ in [0] * int(input()):
	n = int(input())
	d = 0
	t = 0
	while n % 2 == 0:
		n = n // 2
		d += 1
	while n % 3 == 0:
		n = n // 3
		t += 1
	if n == 1 and d <= t:
		print(2 * t - d)
	else:
		print(-1)


from math import *
from itertools import *
from collections import *

def ii():
	return int(input())

def mas():
	return [int(i) for i in input().split()]

def mapis():
	return map(int, input().split())
INF = 1000000000.0 + 1

def solve():
	n = ii()
	d = 0
	for i in range(0, 10 ** 2):
		if n == 1:
			print(i)
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
	if n != 1:
		print(-1)

def main():
	t = ii()
	while t:
		solve()
		t -= 1
main()


import math
n = int(input())
for i in range(n):
	z = int(input())
	c = 0
	if math.log(z, 6) % 1 == 0:
		c = math.log(z, 6)
	elif math.log(z, 3) % 1 == 0:
		c = math.log(z, 3) * 2
	else:
		c = math.floor(math.log(z, 6))
		z /= 6 ** c
		while z != 1:
			if z % 6 == 0:
				z /= 6
				c += 1
			elif z * 2 ** c % 3 == 0:
				z *= 2
				c += 1
			else:
				c = -1
				break
	print(int(c))


for test in range(int(input())):
	val = int(input())
	two = three = 0
	while val % 2 == 0:
		two += 1
		val /= 2
	while val % 3 == 0:
		three += 1
		val /= 3
	if val != 1:
		print('-1')
	elif three < two:
		print('-1')
	else:
		print(three + three - two)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
seti = set()
for i in range(40):
	seti.add(2 ** i)
t = int(input())
for _ in range(t):
	n = int(input())
	flag = 0
	count = 0
	while True:
		if n == 1:
			flag = 1
			break
		if n in seti:
			break
		if n % 3 == 0:
			if n % 6 == 0:
				count += 1
				n //= 6
			else:
				count += 1
				n *= 2
		else:
			break
	if flag:
		print(count)
	else:
		print(-1)


test = int(input())
for i in range(test):
	val = int(input())
	if val == 1:
		print(0)
	else:
		flag1 = 0
		while val % 3 == 0:
			flag1 += 1
			val = val // 3
		flag2 = 0
		while val % 2 == 0:
			flag2 += 1
			val = val // 2
		if val == 1 and flag1 >= flag2:
			res = flag1 + (flag1 - flag2)
			print(res)
		else:
			print('-1')


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	a = 0
	b = 0
	ss = set()
	bb = 0
	while n % 3 == 0:
		n /= 3
		a += 1
	while n % 2 == 0:
		n /= 2
		b += 1
	res = 2 * a - b
	if n != 1:
		print(-1)
	elif b > a:
		print('-1')
	else:
		print(res)


for i in range(int(input())):
	n = int(input())
	count = 0
	while True:
		if n == 1:
			print(count)
			break
		if n % 6 != 0 and n % 3 != 0:
			print('-1')
			break
		elif n % 6 == 0:
			count += 1
			n //= 6
		elif n % 6 != 0 and n % 2 == 0 or n % 3 == 0:
			count += 1
			n *= 2


p = int(input())
for i in range(p):
	n = int(input())
	s = 0
	summ = 0
	f = True
	while f:
		if n % 2 == 0:
			s -= 1
			n //= 2
		elif n % 3 == 0:
			s += 1
			n //= 3
			summ += 1
		else:
			if n != 1:
				s = -1
			f = False
	if s < 0:
		print(-1)
	else:
		print(s + summ)


from collections import defaultdict as dfd
import math
import sys
import heapq

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def main():
	for _ in range(int(input())):
		N = II()
		co = 0
		while N != 1:
			if N % 6 == 0:
				N /= 6
				co += 1
			elif N % 3 == 0:
				N *= 2
				co += 1
			else:
				break
		if N == 1:
			print(co)
		else:
			print(-1)
main()


a = {}
a[0] = [1]
a[1] = [6]
c = 1
while c <= 35:
	h = a.get(c)
	k = set()
	for i in range(len(h)):
		p = h[i] * 6
		if p <= 1000000000:
			k.add(p)
		if h[i] % 2 == 0:
			k.add(h[i] // 2)
	c = c + 1
	a[c] = list(k)
t = int(input())
while t > 0:
	n = int(input())
	flag = 1
	for i in range(37):
		b = a.get(i)
		if n in b:
			print(i)
			flag = 0
			break
	if flag == 1:
		print(-1)
	t = t - 1


import sys

def swap(x, y, a):
	temp = a[x]
	a[x] = a[y]
	a[y] = temp
	return a

def solve():
	return None

def main():
	q = []
	for line in sys.stdin.readlines():
		q.append(line)
	for i in range(len(q)):
		q[i] = int(q[i].rstrip().split(' ')[0])
	for i in range(1, len(q)):
		num = q[i]
		if num == 1:
			print(0)
		else:
			total = 0
			while num != 1:
				if num % 6 == 0:
					total += 1
					num /= 6
					continue
				if num % 3 == 0:
					total += 2
					num /= 3
					continue
				total = -1
				break
			print(total)
main()


for _ in range(int(input())):
	n = int(input())
	c = 0
	j = 0
	while True:
		if n == 1:
			break
		if n % 6 == 0:
			n = n // 6
			j = 0
		else:
			n = n * 2
			j += 1
		c += 1
		if j == 2:
			break
	if j != 2:
		print(c)
	else:
		print('-1')


from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys
for i in range(int(input())):
	n = int(input())
	cnt = 0
	f = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
		elif n % 3 == 0:
			n = n * 2
		else:
			f = 1
			print(-1)
			break
		cnt += 1
	if f == 0 and n == 1:
		print(cnt)
	elif f == 0:
		print(-1)


def div2(n):
	k = 0
	while n % 2 == 0:
		n //= 2
		k += 1
	return k

def div3(n):
	k = 0
	while n % 3 == 0:
		n //= 3
		k += 1
	return k
k = int(input())
while k >= 1:
	k -= 1
	n = int(input())
	x2 = div2(n)
	x3 = div3(n)
	if n // 2 ** x2 // 3 ** x3 != 1 or x2 > x3:
		print(-1)
	else:
		print(x3 - x2 + max(x2, x3))


def ans(a):
	count = 0
	k = 0
	while a != 1:
		if a % 6 == 0:
			a = a // 6
			count += 1
		else:
			a = a * 2
			count += 1
			if a % 6 != 0 or a == 1:
				break
		if a == 1:
			k = 1
			break
	if k == 1:
		print(count)
	else:
		print(-1)
t = int(input())
while t > 0:
	a = int(input())
	if a == 1:
		print(0)
	elif a % 10 == 5 or (a % 6 != 0 and a * 2 % 6 != 0):
		print(-1)
	else:
		ans(a)
	t -= 1


import sys
test_cases = int(input())
while test_cases != 0:
	test_cases -= 1
	n = int(input())
	m = n
	factor_two = 0
	factor_three = 0
	while n % 2 == 0:
		factor_two += 1
		n = int(n / 2)
	while n % 3 == 0:
		factor_three += 1
		n = int(n / 3)
	if m == 1:
		print(0)
	elif n > 1:
		print(-1)
	elif factor_two > factor_three:
		print(-1)
	else:
		print(factor_three + factor_three - factor_two)


for i in range(int(input())):
	n = int(input())
	k = m = 0
	while n % 2 == 0:
		n = n // 2
		k += 1
	while n % 3 == 0:
		n = n // 3
		m += 1
	if n != 1 or k > m:
		print(-1)
	else:
		print(2 * m - k)


def main():
	t = int(input())
	cnt = 0
	found = 1
	while t > 0:
		n = int(input())
		if n == 1:
			print('0')
		else:
			while n != 1:
				if n % 6 == 0:
					n = n // 6
					cnt = cnt + 1
				elif n % 3 == 0:
					n = n * 2
					cnt = cnt + 1
				elif n == 2:
					found = 0
					break
				elif n % 6 != 0 and n % 3 != 0:
					found = 0
					break
			if found == 0:
				print('-1')
			else:
				print(cnt)
		cnt = 0
		found = 1
		t -= 1
main()


from math import *

def isPrime(n):
	if n == 0 or n == 1:
		return False
	if n == 2 or n == 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(sqrt(n)) + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def primefactors(n):
	(cnt2, cnt3) = (0, 0)
	while n % 2 == 0:
		n = n // 2
		cnt2 += 1
	while n % 3 == 0:
		n = n // 3
		cnt3 += 1
	return (cnt2, cnt3, n)
t = int(input())
for i in range(t):
	n = int(input())
	(cnt2, cnt3, n) = primefactors(n)
	if n == 1 and cnt2 <= cnt3:
		print(2 * cnt3 - cnt2)
	else:
		print(-1)


t = int(input())
p = 1
while p <= t:
	x = int(input())
	i = 1
	cnt = 0
	while x >= 1:
		if x % 6 == 0:
			x /= 6
			cnt += 1
		elif x * 2 % 6 == 0:
			x *= 2
			cnt += 1
			x /= 6
			cnt += 1
		else:
			break
		i += 1
	if x == 1.0:
		print(cnt)
	else:
		print(-1)
	p += 1


def main():
	t = int(input())
	for case in range(t):
		n = int(input())
		res = n
		count = 0
		flag = True
		if res == 1:
			print(0)
		else:
			while flag:
				while res % 6 == 0:
					res /= 6
					count += 1
				if res % 3 != 0 and int(res) != 1:
					count = -1
					flag = False
					break
				if res == 1:
					flag = False
					break
				res *= 2
				count += 1
				if res % 6 != 0:
					if count == 1:
						count = -1
					flag = False
			print(count)
main()


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		ost2 = 0
		ost3 = 0
		while n % 2 == 0:
			n //= 2
			ost2 += 1
		while n % 3 == 0:
			n //= 3
			ost3 += 1
		if ost3 < ost2 or n != 1:
			print(-1)
		elif ost3 == ost2:
			print(ost3)
		else:
			ans = ost2
			ost3 -= ost2
			ans += ost3
			ans += ost3
			print(ans)


for _ in range(int(input())):
	N = int(input())
	count2 = 0
	count3 = 0
	while N % 2 == 0:
		N = N // 2
		count2 += 1
	while N % 3 == 0:
		N = N // 3
		count3 += 1
	if N > 1:
		print(-1)
	elif count2 > count3:
		print(-1)
	else:
		t = count3 - count2
		print(t + count3)


def powe(n, x):
	power = 0
	while n % x == 0:
		n = n // x
		power += 1
	return power
t = int(input(''))
for i in range(t):
	n = int(input(''))
	m = powe(n, 3)
	k = powe(n, 2)
	if m < k:
		print(-1)
	elif m == k:
		if n == 6 ** k:
			print(k)
		else:
			print(-1)
	elif n == 2 ** k * 3 ** m:
		print(2 * m - k)
	else:
		print(-1)


for x in range(int(input())):
	a = int(input())
	list1 = []
	while a % 2 == 0:
		a = a / 2
		list1.append(2)
	while a % 3 == 0:
		a = a / 3
		list1.append(3)
	if a != 1 or list1.count(2) > list1.count(3):
		print(-1)
	else:
		print(list1.count(3) * 2 - list1.count(2))


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')
tests = lambda : range(1, int_in() + 1)
case_out = lambda i, o: out('Case #{}: {}'.format(i, o))

def solve(n):
	result = 0
	while n != 1:
		if n % 3 != 0:
			return -1
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		result += 1
	return result
for i in tests():
	n = int_in()
	out(solve(n))


def findDiv(n, count):
	if n < 0:
		print(-1)
		return
	if n == 1:
		print(count)
		return
	if n % 6 == 0:
		findDiv(n / 6, count + 1)
	elif n % 3 == 0:
		findDiv(n * 2 / 6, count + 2)
	else:
		findDiv(-1, count)
number = int(input())
for i in range(number):
	count = 0
	n = int(input())
	if n == 1:
		print(0)
		continue
	findDiv(n, count)


def solve():
	n = int(input())
	count = 0
	while n > 1:
		if n % 6 == 0:
			count += 1
			n = n // 6
		elif n * 2 % 6 == 0:
			count += 2
			n *= 2
			n = n // 6
		else:
			return -1
	return count
for _ in range(int(input())):
	print(solve())


import math
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	count = 0
	while 1:
		while n % 6 == 0:
			n //= 6
			count += 1
		if n % 3 == 0:
			n *= 2
			count += 1
		else:
			break
	if n == 1:
		print(count)
	else:
		print(-1)


t = int(input())

def steps(n):
	num_steps = 0
	while n > 1:
		if n % 3 != 0:
			return -1
		if n % 6 == 0:
			n //= 6
			num_steps += 1
		else:
			n //= 3
			num_steps += 2
	return num_steps
for i in range(t):
	n = int(input())
	print(steps(n))


t = int(input())
for _ in range(t):
	n = int(input())
	counter2 = 0
	while n % 2 == 0:
		n = n // 2
		counter2 += 1
	counter3 = 0
	while n % 3 == 0:
		n = n // 3
		counter3 += 1
	if n != 1:
		print(-1)
		continue
	if counter2 > counter3:
		print(-1)
		continue
	print(counter3 + counter3 - counter2)


t = int(input())
for i in range(t):
	n = int(input())
	cnt = 0
	if n != 1 and n % 3 != 0:
		print(-1)
		continue
	while n > 2:
		if n % 6 == 0:
			n //= 6
			cnt += 1
			if n % 3 != 0:
				break
		else:
			n *= 2
			cnt += 1
	if n == 1:
		print(cnt)
	else:
		print(-1)


def f(val):
	(powers_of_two, powers_of_three) = (0, 0)
	while val % 2 == 0:
		val = val // 2
		powers_of_two += 1
	while val % 3 == 0:
		val = val // 3
		powers_of_three += 1
	if val > 1 or powers_of_two > powers_of_three:
		return -1
	return powers_of_three + (powers_of_three - powers_of_two)
n = int(input())
for _ in range(n):
	val = int(input())
	print(f(val))


t = int(input())
c = []
d = []
for i in range(t):
	n = int(input())
	c.append(n)
for i in range(t):
	a = 0
	b = 0
	while c[i] % 2 == 0:
		c[i] /= 2
		a += 1
	while c[i] % 3 == 0:
		c[i] /= 3
		b += 1
	if c[i] == 1 and a <= b:
		d.append(2 * b - a)
	else:
		d.append(-1)
for i in range(t):
	print(d[i], end='\n')


for _ in range(int(input())):
	n = int(input())
	ans = True
	cnt = 0
	while True:
		if n == 1:
			break
		if n % 6 != 0:
			n *= 2
			cnt += 1
			if n % 6 != 0:
				print(-1)
				ans = False
				break
		else:
			n //= 6
			cnt += 1
	if ans:
		print(cnt)


def foo(n, a):
	res = 0
	while n % a == 0:
		n = n // a
		res += 1
	return res

def loo(n, b):
	while n % b == 0:
		n = n // b
	return n
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	d3 = foo(n, 3)
	d2 = foo(n, 2)
	res = d3 - d2
	if d3 < d2 or loo(n * 2 ** res, 6) != 1:
		print(-1)
	else:
		res += foo(n * 2 ** res, 6)
		print(res)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	a = 0
	b = 0
	f = 1
	while n % 2 == 0:
		a = a + 1
		n = n // 2
	while n % 3 == 0:
		b = b + 1
		n = n // 3
	if n <= 1:
		if a > b:
			print(-1)
		else:
			print(b - a + b)
	else:
		print(-1)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	n = int(input())
	count = 0
	while n % 3 == 0:
		n = n // 3
		count += 1
	count2 = 0
	while n % 2 == 0:
		n = n // 2
		count2 += 1
	if n != 1:
		print(-1)
		continue
	if count >= count2:
		print(2 * count - count2)
	else:
		print(-1)


n = int(input())
for i in range(n):
	number = int(input())
	result = 0
	flag = 0
	while number != 1:
		if number % 6 == 0:
			number /= 6
			result += 1
			continue
		elif number % 3 == 0 and number % 6 != 0:
			number *= 2
			result += 1
			continue
		else:
			flag = 1
			print('-1')
			break
	if flag != 1:
		print(result)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math
from functools import reduce

def gift():
	for _ in range(t):
		n = int(input())
		counter = 0
		while True:
			if n == 1:
				break
			elif n % 6 == 0:
				counter += 1
				n //= 6
			elif n % 3 == 0:
				counter += 2
				n //= 3
			else:
				counter = -1
				break
		yield counter
t = int(input())
ans = gift()
print(*ans, sep='\n')


from collections import Counter
import sys
input = sys.stdin.readline

def add_target(n):
	primes = Counter()
	for i in [2, 3]:
		while n % i == 0:
			primes[i] += 1
			n //= i
		if not n:
			break
	if n > 1:
		return False
	return primes

def solve():
	t = int(input())
	for _ in range(t):
		n = int(input())
		d = add_target(n)
		if d == False:
			print(-1)
			continue
		used = set(d.keys())
		if n == 1:
			print(0)
		elif used == {2, 3} or used == {3}:
			y = d[3]
			x = y - d[2]
			if x >= 0 and y >= 0:
				print(x + y)
			else:
				print(-1)
		else:
			print(-1)
solve()


for _ in range(int(input())):
	n = int(input())
	result = 0
	if n == 1:
		print(0)
		continue
	elif n % 6 != 0:
		n = 2 * n
		result += 1
		if n % 6 != 0:
			print(-1)
			continue
	while n > 1:
		if n % 6 == 0:
			n = n / 6
			result += 1
		else:
			n = 2 * n
			result += 1
			if n % 6 == 0:
				n = n / 6
				result += 1
			else:
				print(-1)
				break
	if n == 1:
		print(result)


tc = int(input())
for s in range(tc):
	n = int(input())
	c = 0
	temp = n
	f = 0
	while temp > 1:
		if temp % 6 == 0:
			temp /= 6
			c += 1
			f = c
		else:
			temp *= 2
			temp /= 6
			c += 2
			f = c
	if temp != 1:
		f = '-1'
	print(f)


T = int(input())
for _ in range(T):
	n = int(input())
	moves = 0
	mul = 0
	ans = 'yes'
	while n != 1:
		if n % 6 == 0:
			n //= 6
			mul = 0
		else:
			n *= 2
			mul += 1
		if mul > 1:
			ans = 'no'
			break
		moves += 1
	if ans == 'no':
		print(-1)
	else:
		print(moves)


for _ in range(int(input())):
	n = int(input())
	dp = {}
	dp[1] = 0
	dp[3] = 2
	dp[6] = 1
	dp[9] = 4
	dp[27] = 6
	dp[36] = 2
	dl = [6, 3, 9, 36, 27]
	c = 0
	tmp = n
	if n == 1 or n == 0:
		print(0)
		continue
	if n in dl:
		print(dp[n])
	else:
		(i, ans, h) = (0, 0, 0)
		while i < 3 and tmp >= 1:
			if tmp / dl[i] == tmp // dl[i] and tmp not in dp.keys():
				c += 1
				tmp = tmp // dl[i]
			elif tmp in dl or tmp == 1:
				print(c * dp[dl[i]] + dp[tmp] + h)
				dp[n] = c * dp[dl[i]] + dp[tmp] + h
				dl.append(n)
				break
			else:
				if tmp % dl[i + 1] == 0:
					h += c * dp[dl[i]]
				else:
					tmp = n
				c = 0
				i += 1
		if i >= 3:
			print(-1)


def f(x):
	if x == 1:
		return 0
	if x == 6:
		return 1
	if x < 6:
		if x == 3:
			return 2
	num = str(x)
	s = 0
	for i in range(len(num)):
		s += int(num[i])
	if s % 3 == 0:
		if x % 6 == 0:
			temp = f(x // 6)
			if temp == -1:
				return -1
			return temp + 1
		else:
			temp = f(x * 2)
			if temp == -1:
				return -1
			return temp + 1
	else:
		return -1
for _ in range(int(input())):
	n = int(input())
	print(f(n))


import math
test = int(input())
for i in range(test):
	a = int(input())
	ans = 0
	while True:
		if a == 1:
			break
		if a % 6 == 0:
			a = math.floor(a / 6)
			ans += 1
		elif a % 3 == 0 and a % 2 == 1:
			a = a * 2
			ans += 1
		elif a % 3 != 0 and a % 2 == 1:
			ans = -1
			break
		elif a % 3 != 0 and a % 2 == 0:
			ans = -1
			break
	print(ans)


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
for ii in range(int(input())):
	n = int(input())
	ans = 0
	if n == 1:
		print(0)
		continue
	curr = 0
	f = 1
	two = 0
	three = 0
	while n % 2 == 0:
		n = n // 2
		two += 1
	while n % 3 == 0:
		n = n // 3
		three += 1
	if n != 1:
		print(-1)
		continue
	if three < two:
		print(-1)
	else:
		print(three + (three - two))


t = int(input())
for _ in range(t):
	n = int(input())
	count = 0
	while True:
		if n == 1:
			print(count)
			break
		elif n % 6 == 2 or n % 6 == 4:
			print(-1)
			break
		elif n % 6 == 0:
			n /= 6
			count += 1
		else:
			n *= 2
			count += 1


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def sieve_of_eratosthenes(n):
	ans = []
	arr = [1] * (n + 1)
	(arr[0], arr[1], i) = (0, 0, 2)
	while i * i <= n:
		if arr[i] == 1:
			j = i + i
			while j <= n:
				arr[j] = 0
				j += i
		i += 1
	for k in range(n):
		if arr[k] == 1:
			ans.append(k)
	return ans

def nc2(x):
	if x == 1:
		return 0
	else:
		return x * (x - 1) // 2

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far
from math import *
for _ in range(int(input())):
	n = int(input())
	(p2, p3) = (0, 0)
	while True:
		if n % 2 == 0:
			p2 += 1
			n //= 2
		elif n % 3 == 0:
			p3 += 1
			n //= 3
		else:
			break
	if n != 1:
		print(-1)
	elif p2 > p3:
		print(-1)
	else:
		print(p3 - p2 + p3)


from collections import deque
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from bisect import bisect_left as bl
from math import log2, ceil
from itertools import permutations

def main():
	for t in range(int(input())):
		n = int(input())
		a = 0
		b = 0
		while n % 3 == 0:
			n = n // 3
			a += 1
		while n % 2 == 0:
			n = n // 2
			b += 1
		ans = -1
		if a >= b and n == 1:
			ans = a - b + a
		print(ans)
main()


import sys
TEST = 0
if TEST == 1:
	input_file = open('inputB.txt', 'r')
	input_from_file = input_file.readlines()

def read_int():
	if TEST:
		return int(input_from_file.pop(0))
	else:
		return int(sys.stdin.readline())

def read_ints():
	if TEST:
		return list(map(int, input_from_file.pop(0).rstrip().split()))
	else:
		return list(map(int, sys.stdin.readline().split()))

def read_str():
	if TEST:
		in1 = input_from_file.pop(0).rstrip()
	else:
		in1 = sys.stdin.readline().rstrip()
	return in1

def read_str_to_list():
	if TEST:
		in1 = input_from_file.pop(0).rstrip()
	else:
		in1 = sys.stdin.readline().rstrip()
	return [x for x in in1]
tests = read_int()
for test in range(tests):
	n = read_int()
	steps = 0
	while n > 1:
		if n % 6 == 0:
			n /= 6
			steps += 1
		elif n % 3 == 0:
			n /= 3
			steps += 2
		else:
			steps = -1
			break
	print(steps)


def helper(n):
	steps = 0
	step1 = 0
	if n == 1:
		print(0)
		return
	if n < 6 and n != 3:
		print(-1)
		return
	else:
		while n % 6 == 0:
			n = n // 6
			steps += 1
		if n == 1:
			print(steps)
			return
		else:
			while n % 3 == 0:
				n = n // 3
				step1 += 1
			if n == 1:
				print(steps + 2 * step1)
				return
	print(-1)
for _ in range(int(input())):
	n = int(input())
	helper(n)


t = int(input())
for i in range(t):
	n = int(input())
	time_2 = 0
	time_3 = 0
	while n % 2 == 0:
		n = n / 2
		time_2 += 1
	while n % 3 == 0:
		n = n / 3
		time_3 += 1
	if n != 1 or time_2 > time_3:
		print(-1)
	else:
		print(time_3 * 2 - time_2)


t = int(input())
answer = []
for i in range(t):
	n = int(input())
	mod2 = 0
	mod3 = 0
	while n % 2 == 0:
		n /= 2
		mod2 += 1
	while n % 3 == 0:
		n /= 3
		mod3 += 1
	if n == 1 and mod3 >= mod2:
		answer.append(2 * mod3 - mod2)
	else:
		answer.append(-1)
for i in answer:
	print(i)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	count3 = 0
	count2 = 0
	while n % 2 == 0:
		n = n // 2
		count2 += 1
	while n % 3 == 0:
		n = n // 3
		count3 += 1
	if n != 1 or count2 > count3:
		print(-1)
	else:
		print(count3 - count2 + count3)


for ty in range(int(input())):
	n = int(input())
	t = 0
	tr = 0
	while n % 2 == 0:
		n = n / 2
		t += 1
	while n % 3 == 0:
		n = n / 3
		tr += 1
	if n != 1:
		print(-1)
	elif t > tr:
		print(-1)
	else:
		print(tr + tr - t)


t = int(input())
for _ in range(t):
	n = int(input())
	val = 1
	count = 0
	flag = 0
	while val != n:
		if val < n:
			val = val * 6
			count += 1
		elif val > n:
			if val % 2 == 0 and val > 0:
				val = val // 2
				count += 1
			else:
				flag = 1
				print(-1)
				break
		if val == n:
			break
	if flag == 0:
		print(count)


def sol(n):
	n2 = 0
	while n % 2 == 0:
		n //= 2
		n2 += 1
	n3 = 0
	while n % 3 == 0:
		n //= 3
		n3 += 1
	if n == 1 and n2 <= n3:
		return n3 - n2 + n3
	return -1
n = int(input())
for i in range(n):
	x = int(input())
	print(sol(x))


n = int(input())
for _ in range(0, n):
	a = int(input())
	if a == 1:
		print(0)
	elif a % 3 == 0:
		cnt = 0
		run = True
		while run:
			if a % 2 == 0:
				a /= 6
				cnt += 1
				if a.is_integer():
					if a == 1:
						run = False
						break
				else:
					cnt = -1
					run = False
					break
			else:
				a *= 2
				cnt += 1
		if a == -1:
			print(-1)
		else:
			print(cnt)
	else:
		print(-1)


import math
import sys
for _ in range(int(input())):
	n = int(input())
	(c, i) = (0, 0)
	k = True
	if n == 2 or n % 5 == 0 or n % 7 == 0:
		print(-1)
	elif n == 1:
		print(0)
	else:
		while n > 1:
			if n % 6 == 0:
				n = n // 6
				c += 1
			elif n % 3 == 0:
				n = n // 3
				c += 2
			else:
				print(-1)
				k = False
				break
		if k:
			print(c)


cases = int(input())
for v in range(cases):
	n = int(input())
	if n == 1:
		print(0)
	else:
		f = 0
		cnt = 0
		while 1:
			if n % 6 == 0:
				n = n // 6
			elif n % 3 == 0:
				n = n * 2
			else:
				f = 1
				break
			cnt = cnt + 1
			if n == 1:
				break
		if f == 1:
			print(-1)
		else:
			print(cnt)


for _ in range(int(input())):
	i = int(input())
	d = 0
	if i < 2:
		print(0)
	else:
		for _ in range(50):
			d += 1
			if i % 6 == 0:
				i = i / 6
			else:
				i = i * 2
			if i == 1:
				break
		if d == 50:
			print(-1)
		else:
			print(d)


t = int(input())
for j in range(t):
	a = int(input())
	ans = 0
	while a % 6 == 0:
		ans += 1
		a /= 6
	while a % 3 == 0:
		ans += 2
		a /= 3
	if a == 1:
		print(ans)
	else:
		print(-1)


for ad in range(int(input())):
	n = int(input())
	div_two = 0
	div_three = 0
	while n % 2 == 0:
		n //= 2
		div_two += 1
	while n % 3 == 0:
		n //= 3
		div_three += 1
	if n == 1:
		if div_two > div_three:
			print(-1)
		else:
			print(2 * div_three - div_two)
	else:
		print(-1)


import sys
input = sys.stdin.buffer.readline
import os
from math import *
t = int(input())
while t:
	t -= 1
	n = int(input())
	ans = 0
	count = 0
	z = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			ans += 1
			count = 0
		else:
			n *= 2
			count += 1
			if count > 2:
				z = 1
				break
			ans += 1
	if z == 1:
		print('-1')
	else:
		print(ans)


for _ in range(int(input())):
	num = int(input())
	q = 0
	while num >= 1:
		if num % 2 == 0 and num % 3 == 0:
			q += 1
			num = num / 6
		elif num % 3 == 0 and num % 2 != 0:
			q += 2
			num = num / 3
		else:
			break
	if num == 1:
		print(q)
	else:
		print(-1)


def solve(n):
	count = 0
	if n == 1:
		return 0
	tmp = n
	threes = 0
	twos = 0
	while tmp % 3 == 0:
		tmp = tmp // 3
		threes += 1
	while tmp % 2 == 0:
		tmp = tmp // 2
		twos += 1
	if tmp != 1:
		return -1
	if twos > threes:
		return -1
	count += twos
	threes = threes - twos
	if threes > 0:
		count += 2 * threes
	return count
for _ in range(int(input())):
	n = int(input())
	print(solve(n))


import math
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		x = n
		r = 0
		while n % math.pow(3, r) == 0:
			r += 1
		r -= 1
		x = n // math.pow(3, r)
		s = 0
		while n % math.pow(2, s) == 0:
			s += 1
		s -= 1
		three = r
		two = s
		if int(math.pow(2, two) * math.pow(3, three)) == n:
			x = three
			y = three - two
			if x >= 0 and y >= 0:
				print(x + y)
			else:
				print(-1)
		else:
			print(-1)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, input().split()))

def In():
	return map(int, input().split())

def I():
	return int(input())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		lis = [0, 0]
		if n == 1:
			print(0)
		else:
			while n % 2 == 0:
				lis[0] += 1
				n //= 2
			while n % 3 == 0:
				lis[1] += 1
				n //= 3
			if n > 1 or lis[0] > lis[1]:
				print(-1)
			else:
				print(lis[1] + lis[1] - lis[0])
main()


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	n = ri()
	moves = 0
	while 1:
		if n == 1:
			break
		elif n % 3 != 0:
			print(-1)
			return
		elif n % 6 == 0:
			n //= 6
			moves += 1
		else:
			n //= 3
			moves += 2
	print(moves)
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


def count_factors(n):
	(f2, f3) = (0, 0)
	while n % 2 == 0:
		n //= 2
		f2 += 1
	while n % 3 == 0:
		n //= 3
		f3 += 1
	if n != 1 or f3 < f2:
		return -1
	return f3 - f2 + f3
t = int(input())
for i in range(0, t):
	n = int(input())
	print(count_factors(n))


def main():
	for _ in range(int(input())):
		n = int(input())
		x = n
		count2 = 0
		count3 = 0
		while x % 2 == 0:
			count2 += 1
			x /= 2
		while x % 3 == 0:
			count3 += 1
			x /= 3
		result = 0
		if x != 1:
			result = -1
		elif n == 1:
			result = 0
		elif count2 <= count3:
			result = count3 - count2
			result += count3
		elif count2 > count3:
			result = -1
		elif count3 == 0 and x != 1:
			result = -1
		else:
			result = -1
		print(result)
main()


try:
	t = int(input())
	for j in range(t):
		p = list(map(int, input().rstrip().split()))
		r = n = p[0]
		k = False
		count = 0
		if n == 1:
			print(0)
			continue
		while n != 1:
			if n % 6 == 0:
				n = n / 6
				k = True
				count += 1
			elif n % 3 == 0:
				n *= 2
				count += 1
			else:
				k = False
				break
		if k:
			print(count)
		else:
			print(-1)
except:
	pass


from math import ceil, floor

def ans(n):
	count = 0
	while n > 1 and ceil(n) == floor(n):
		if n % 6 == 0:
			n = n / 6
			count += 1
		elif n % 3 == 0:
			n *= 2
			count += 1
		else:
			return -1
	if n == 1:
		return count
	else:
		return -1
t = int(input())
L = []
for i in range(t):
	n = int(input())
	L.append(ans(n))
for l in L:
	print(l)


t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	if n == 1:
		print(0)
		continue
	if n == 2 or n % 3 != 0 or n == 0:
		print(-1)
		continue
	while n != 1:
		if n % 3 == 0 and n % 6 != 0:
			n = n * 2
			count += 1
		elif n % 6 == 0:
			n = n / 6
			count += 1
		else:
			count = -1
			break
	print(count)


for _ in range(int(input())):
	x = int(input())
	ans = 0
	while x > 1:
		if x % 6 == 0:
			x = x // 6
			ans += 1
		elif x % 3 == 0:
			x = x * 2
			ans += 1
		else:
			break
	print(ans if x == 1 else -1)


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
for _ in range(int(input())):
	N = int(input())
	a = 0
	b = 0
	while N % 3 == 0:
		a += 1
		N //= 3
	while N % 2 == 0:
		b += 1
		N //= 2
	if N != 1 or b > a:
		print(-1)
	else:
		print(2 * a - b)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
		continue
	count = 0
	while True:
		count += 1
		if n % 6 == 0:
			n //= 6
			continue
		if n == 1:
			print(count - 1)
			break
		if n % 3 != 0:
			print(-1)
			break
		n *= 2


def solve(n):
	two = 0
	three = 0
	while True:
		if n % 2 == 0:
			two += 1
			n = n // 2
		else:
			break
	while True:
		if n % 3 == 0:
			three += 1
			n = n // 3
		else:
			break
	if n > 1 or two > three:
		return -1
	return three + three - two
for _ in range(int(input())):
	n = int(input())
	print(solve(n))


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	temp = int(n)
	(twos, threes) = (0, 0)
	while temp != 0:
		if temp % 2 == 0:
			twos += 1
			temp //= 2
		else:
			break
	temp = int(n)
	while temp != 0:
		if temp % 3 == 0:
			threes += 1
			temp //= 3
		else:
			break
	flag = False
	if twos == 0 and threes == 0:
		flag = True
	elif n // (2 ** twos * 3 ** threes) != 1:
		flag = True
	if twos > threes or flag:
		print(-1)
	elif twos < threes:
		print(2 * threes - twos)
	else:
		print(threes)


n = int(input())
for _ in range(n):
	counter = 0
	t = int(input())
	if t == 1:
		print(0)
		continue
	while counter < 50:
		if t % 6 == 0:
			t /= 6
		else:
			t *= 2
		counter += 1
		if t == 1:
			print(counter)
			break
	else:
		print(-1)


t = int(input())
for e in range(t):
	num = int(input())
	cont = 0
	while num != 1:
		if num % 3 == 0:
			if num % 6 == 0:
				num /= 6
			else:
				num *= 2
			cont += 1
		else:
			cont = -1
			break
	print(cont)


def conver(m, n):
	if m == n:
		return 0
	if m > n:
		return m - n
	if m < 1 and n > 0:
		return -1
	if n % 2 == 1:
		return 1 + conver(m, n * 2)
	else:
		return 1 + conver(m, n / 6)
t = int(input())
for i in range(t):
	n = int(input())
	x = conver(1, n)
	if type(x) != int:
		print(-1)
	else:
		print(x)


def solution1():
	for tangocharlie in range(int(input())):
		(xx, yy, nn) = map(int, input().split())
		p = nn // xx
		k = p * xx + yy
		while k > nn:
			p -= 1
			k = p * xx + yy
		print(k)

def solution2():
	for tangocharlie in range(int(input())):
		nnn = int(input())
		apple = 5
		banana = 10
		orange = apple + banana
		answer = 0
		while nnn > 1:
			if nnn % 6 == 0:
				answer += 1
				nnn = nnn // 6
			elif nnn % 3 == 0 and nnn % 2 != 0:
				answer += 1
				nnn *= 2
			else:
				answer = -1
				break
		print(answer)

def solution3():
	for tangocharlie in range(int(input())):
		number = int(input())
		sunlight = list(input())
		opnn = wrong = 0
		for i in sunlight:
			if i == '(':
				opnn += 1
			else:
				opnn -= 1
			if opnn < 0:
				wrong += 1
				opnn = 0
		print(wrong)

def solution4():
	for tangocharlie in range(int(input())):
		(n, k) = map(int, input().split())
		abra_ka_dabra = sorted(list(map(int, input().split())))
		abra_ka_dabra1 = []
		for i in abra_ka_dabra:
			if i % k != 0:
				abra_ka_dabra1.append(k - i % k)
		if len(abra_ka_dabra1) == 0:
			print(0)
			continue
		abra_ka_dabra1.sort()
		ans = abra_ka_dabra1[0]
		curr = abra_ka_dabra1[0]
		for i in range(1, len(abra_ka_dabra1)):
			if abra_ka_dabra1[i] != abra_ka_dabra1[i - 1]:
				curr = abra_ka_dabra1[i]
			else:
				curr += k
			ans = max(ans, curr)
		print(ans + 1)
solution2()


t = int(input())
for zz in range(t):
	n = int(input())
	s = 0
	p = 0
	ans = -1
	if n == 1:
		ans = 0
	else:
		while n % 3 == 0:
			n /= 3
			s += 1
		while n % 2 == 0:
			n /= 2
			p += 1
		if s >= p and n == 1:
			ans = s + s - p
	print(ans)


for i in range(int(input())):
	n = int(input())
	cnt = 0
	if n == 1:
		print(cnt)
	elif n < 6 and n % 3 != 0:
		print(-1)
	elif n >= 6 or n % 3 == 0:
		while n != 1:
			if n >= 6 and n % 6 == 0:
				n = n // 6
				cnt += 1
			elif n > 6 and n % 3 == 0:
				n = n * 2
				cnt += 1
			elif n < 6 and n % 3 == 0:
				n = n * 2
				cnt += 1
			elif n < 6 and n % 3 == 0:
				n = n * 2
				cnt += 1
			else:
				print(-1)
				break
		else:
			print(cnt)


import math, collections

def primeFactors(n):
	f = []
	while n % 2 == 0:
		f.append(2)
		n = n / 2
	for i in range(3, 4, 2):
		while n % i == 0:
			f.append(i)
			n = n / i
	return f
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		a = primeFactors(n)
		d = collections.Counter(a)
		if 2 in d and 3 not in d:
			print(-1)
		elif 2 not in d and 3 in d:
			if 3 ** d[3] != n:
				print(-1)
			else:
				print(2 * d[3])
		elif 2 in d and 3 in d:
			if d[2] > d[3]:
				print(-1)
			elif 2 ** d[2] * 3 ** d[3] != n:
				print(-1)
			else:
				print(-d[2] + 2 * d[3])
		else:
			print(-1)


t = int(input())
answer = ''
for i in range(t):
	t_num = int(input())
	three_count = 0
	two_count = 0
	six_count = 0
	while True:
		if t_num % 3 == 0:
			three_count += 1
			t_num //= 3
		else:
			break
	while True:
		if t_num % 2 == 0:
			two_count += 1
			t_num //= 2
		else:
			break
	if t_num == 1:
		if three_count < two_count:
			answer += '-1\n'
		elif three_count == two_count and three_count != 0:
			answer += '%d\n' % three_count
		elif three_count == 0:
			answer += '%d\n' % three_count
		elif three_count > two_count:
			answer += '%d\n' % (2 * three_count - two_count)
	else:
		answer += '-1\n'
print(answer.strip())


def HowManyMoves(n):
	CountDevisionBy6 = 0
	CountDevisionBy3 = 0
	while n % 6 == 0:
		CountDevisionBy6 += 1
		n //= 6
	if n == 1:
		return CountDevisionBy6
	while n % 3 == 0:
		CountDevisionBy3 += 1
		n //= 3
	if n == 1:
		return CountDevisionBy3 * 2 + CountDevisionBy6
	return -1
n = int(input())
for _ in range(n):
	print(HowManyMoves(int(input())))


t = int(input())
for i in range(t):
	x = int(input())
	cnt_2 = 0
	cnt_3 = 0
	while x % 2 == 0:
		x //= 2
		cnt_2 += 1
	while x % 3 == 0:
		cnt_3 += 1
		x //= 3
	if x != 1 or cnt_2 > cnt_3:
		print(-1)
	else:
		print(2 * cnt_3 - cnt_2)


import sys
input = sys.stdin.readline
num_tests = int(input().strip())
numbers = [0] * num_tests
for i in range(num_tests):
	numbers[i] = int(input().strip())
for n in numbers:
	aux = n
	num_steps = 0
	while aux != 1 and aux % 3 == 0:
		if aux % 2 != 0:
			aux *= 2
			num_steps += 1
		aux = aux // 6
		num_steps += 1
	if aux == 1:
		print(num_steps)
	else:
		print(-1)


def sixtwo(x):
	cnt2 = 0
	cnt3 = 0
	while x % 2 == 0:
		x /= 2
		cnt2 += 1
	while x % 3 == 0:
		x /= 3
		cnt3 += 1
	if x == 1 and cnt2 <= cnt3:
		print(cnt3 - cnt2 + cnt3)
	else:
		print('-1')
testnum = int(input())
k = 0
testlist = []
while k < testnum:
	testlist.append(int(input()))
	k += 1
p = 0
while p < testnum:
	sixtwo(testlist[p])
	p += 1


for _ in range(int(input())):
	n = int(input())
	k = 0
	while n % 3 == 0:
		if n % 2:
			n *= 2
			k += 1
		n //= 6
		k += 1
	if n != 1:
		k = -1
	print(k)


def func(n):
	m = n
	two = 0
	three = 0
	while m % 2 == 0:
		two = two + 1
		m = m // 2
	m = n
	while m % 3 == 0:
		three = three + 1
		m = m // 3
	element = n * 2 ** (three - two)
	if two > three:
		ans = -1
	elif element == 6 ** three:
		ans = 2 * three - two
	else:
		ans = -1
	return ans
test_case = int(input())
for _ in range(test_case):
	n = int(input())
	print(func(n))


import sys
input = sys.stdin.readline
out = sys.stdout

def main():
	q = int(input())
	for i in range(q):
		n = int(input())
		(count2, count3) = (0, 0)
		while n % 2 == 0:
			n //= 2
			count2 += 1
		while n % 3 == 0:
			n //= 3
			count3 += 1
		if n != 1 or count2 > count3:
			print(-1)
		else:
			print(2 * count3 - count2)
main()


t = int(input())
for i in range(0, t):
	step = 0
	n = int(input())
	flag = False
	if n == 1:
		print('0')
	elif n < 1:
		print('-1')
	else:
		while n > 1:
			if n % 6 == 0:
				n = n / 6
				flag = False
				step += 1
			elif flag == False:
				n = n * 2
				flag = True
				step += 1
			else:
				break
		if n == 1:
			print(step)
		else:
			print('-1')


def readintlst():
	return list(map(int, input().split(' ')))

def readn():
	return int(input())
import math
for _ in range(readn()):
	n = readn()
	i = j = 0
	while n / 3 == int(n / 3):
		j += 1
		n = n // 3
	while n / 2 == int(n / 2):
		i += 1
		n = n // 2
	if n != 1:
		print(-1)
	else:
		steps = 0
		if i <= j:
			print(2 * j - i)
		else:
			print(-1)


t = int(input())
l = []
for e in range(0, t):
	n = int(input())
	i = 0
	while n > 1:
		if n % 6 == 0:
			n = n / 6
			i = i + 1
		elif n % 3 == 0:
			n = n * 2
			i = i + 1
		else:
			l.append(-1)
			break
	else:
		l.append(i)
for h in l:
	print(h)


t = int(input())
for _ in range(t):
	n = int(input())
	f = 0
	cnt = 0
	if n == 1:
		print(0)
		continue
	while n != 1:
		if n % 6 == 0:
			n //= 6
		elif n * 2 % 6 == 0:
			n *= 2
		else:
			print(-1)
			f = 1
			break
		cnt += 1
	if not f:
		print(cnt)


t = int(input())
while t:
	n = int(input())
	cnt = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
		else:
			n = n * 2
			rem = n % 6
			if rem == 2 or rem == 4:
				cnt = -1
				break
		cnt += 1
	print(cnt)
	t -= 1


import sys
input = lambda : sys.stdin.readline().rstrip()
inp = sys.stdin.buffer.readline

def I():
	return list(map(int, inp().split()))
for _ in range(int(input())):
	n = int(input())
	n1 = n
	cnt2 = 0
	cnt3 = 0
	while n1 % 2 == 0 or n1 % 3 == 0:
		if n1 % 2 == 0:
			n1 //= 2
			cnt2 += 1
		if n1 % 3 == 0:
			n1 //= 3
			cnt3 += 1
	if n1 != 1:
		print(-1)
	elif cnt2 > cnt3:
		print(-1)
	elif cnt2 == cnt3:
		print(cnt2)
	else:
		print(2 * cnt3 - cnt2)


t = int(input())
for i in range(t):
	number = int(input())
	cnttwo = 0
	cntthree = 0
	while number % 3 == 0:
		number /= 3
		cntthree += 1
	while number % 2 == 0:
		number /= 2
		cnttwo += 1
	if number != 1 or cnttwo > cntthree:
		print(-1)
	else:
		print(cntthree - cnttwo + cntthree)


from math import *
for j in range(int(input())):
	n = int(input())
	am2 = 0
	am3 = 0
	n1 = n
	n2 = n
	am = 0
	while n1 % 2 == 0 or n2 % 3 == 0:
		if n1 % 2 == 0:
			am2 += 1
			n1 = n1 // 2
		if n2 % 3 == 0:
			am3 += 1
			n2 = n2 // 3
	k = 2 ** (am3 - am2) * n
	while k % 6 == 0:
		am += 1
		k = k // 6
	if am2 - am3 > 0 or k > 1:
		print(-1)
	else:
		print(int(am3 - am2 + am))


t = int(input())
while t:
	n = int(input())
	count = 0
	i = 0
	if n == 1:
		print('0')
	else:
		while n != 1 and i < 50:
			if n % 6 == 0:
				n = n // 6
				count = count + 1
			else:
				n = n * 2
				count = count + 1
			i = i + 1
		if n == 1:
			print(count)
		else:
			print('-1')
	t = t - 1


import math
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		x = n
		c = 0
		while x % 6 == 0:
			x /= 6
			c += 1
		if x == 1:
			print(c)
		else:
			while x % 3 == 0:
				x /= 3
				c += 2
			if x == 1:
				print(c)
			else:
				print(-1)


a = int(input())
d = [int(input()) for i in range(a)]
for i in d:
	count = 0
	c2 = 0
	f = 0
	e = i
	while True:
		if i % 3 == 0:
			i = i / 3
			count += 1
		elif i % 2 == 0:
			i = i / 2
			c2 += 1
		else:
			break
	if i == 1 and c2 <= count:
		print(count * 2 - c2)
	else:
		print('-1')


import math

def facts(n):
	ans = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			ans.append(i)
			ans.append(n // i)
	ans = sorted(ans)
	return ans
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		(three, two) = (0, 0)
		while n % 3 == 0:
			n //= 3
			three += 1
		while n % 2 == 0:
			n //= 2
			two += 1
		if n == 1 and two <= three:
			print(three - two + three)
		else:
			print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	cp = n
	cnt = 0
	while cp % 6 == 0:
		cp = cp // 6
		cnt += 1
	while cp % 3 == 0:
		cp = cp // 3
		cnt += 2
	if cp == 1:
		print(cnt)
	else:
		print(-1)


import math
for _ in range(int(input())):
	n = int(input())
	(step, flag) = (0, True)
	while n > 1:
		if n % 3 == 0:
			if n % 6 == 0:
				n = n // 6
				step += 1
			else:
				n = n * 2
				step += 1
		else:
			flag = False
			break
	if flag:
		print(step)
	else:
		print(-1)


def step(n):
	if n == 1:
		return 0
	two_fac = 0
	while n:
		if n % 2 != 0:
			break
		n = n // 2
		two_fac += 1
	three_fac = 0
	while n:
		if n % 3 != 0:
			break
		n = n // 3
		three_fac += 1
	if n != 1:
		return -1
	if two_fac > three_fac:
		return -1
	if two_fac == three_fac:
		return three_fac
	if two_fac == 0 and three_fac > 0:
		return 2 * three_fac
	if two_fac < three_fac:
		return three_fac + (three_fac - two_fac)
T = int(input())
for t in range(T):
	num = int(input())
	ans = step(num)
	print(ans)


n = int(input())
for _ in range(n):
	a = int(input())
	k = a
	c = 0
	while k != 1 and (k % 6 == 0 or k * 2 % 6 == 0) and (k != 0):
		if k % 6 == 0:
			k = k // 6
			c += 1
		else:
			k = k * 2
			c += 1
	if k == 1:
		print(c)
	else:
		print(-1)


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	for _ in range(II()):
		n = II()
		c2 = c3 = 0
		while n % 3 == 0:
			n //= 3
			c3 += 1
		while n % 2 == 0:
			n //= 2
			c2 += 1
		if n == 1 and c3 >= c2:
			print(2 * c3 - c2)
		else:
			print(-1)
main()


import math

def min_pow(n):
	x = 30
	while 6 ** x % n == 0:
		x -= 1
	return x + 1
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	if n == 1:
		print(0)
		continue
	if 6 ** 30 % n == 0:
		m = min_pow(n)
	else:
		print(-1)
		continue
	rem = 6 ** m / n
	mul_steps = math.log(rem, 2)
	div_steps = m
	print(int(mul_steps + div_steps) if math.floor(mul_steps) == mul_steps else -1)


i = int(input())
for t in range(i):
	ans = int(input())
	k = 0
	while ans != 1:
		if ans % 6 == 0:
			ans = ans // 6
			k += 1
		elif ans % 6 == 3:
			ans *= 2
			k += 1
		else:
			k = -1
			break
	print(k)


a = int(input())
z = 0
n = 0
b = []
for i in range(a):
	c = int(input())
	b.append(c)
for j in b:
	x = j
	while x % 6 == 0:
		z = z + 1
		x = x / 6
	while x % 3 == 0:
		n = n + 1
		x = x / 3
	if x != 1:
		print(-1)
	else:
		print(z + 2 * n)
	z = 0
	n = 0


def mul2div6(n):
	moves = 0
	while n != 1:
		if n % 6 == 0:
			n /= 6
			moves += 1
		elif n * 2 % 6 == 0:
			n *= 2
			moves += 1
		else:
			print(-1)
			return
	print(moves)
t = int(input())
for i in range(t):
	n = int(input())
	mul2div6(n)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		var = n
		ans = 0
		while var > 0:
			if var == 1:
				break
			elif var % 6 == 0:
				var = var / 6
			elif var * 2 <= 1000000000:
				var = var * 2
			else:
				ans = -1
				break
			ans += 1
		print(ans)


testcases = int(input())
for testcase in range(testcases):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n == 2 or n == 4 or n == 5:
		print(-1)
		continue
	if n == 3:
		print(2)
		continue
	count = 0
	flag = 0
	while True:
		if n == 1:
			flag = 1
			print(count)
			break
		if n == 2 or n == 4 or n == 5:
			print(-1)
			flag = 1
			break
		if n == 3:
			flag = 1
			print(count + 2)
			break
		if n & 1 == 0 and n % 6 != 0:
			break
		if n & 1 == 0 and n % 6 == 0:
			n = n // 6
			count += 1
			continue
		count += 1
		n = n << 1
	if flag == 0:
		print(-1)


from sys import stdin

def iinput():
	return int(stdin.readline())

def sinput():
	return input()

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
t = iinput()
while t:
	t -= 1
	n = iinput()
	(a, b) = (0, 0)
	while n % 6 == 0:
		n //= 6
		a += 1
	while n % 3 == 0:
		n //= 3
		b += 1
	print(a + 2 * b if n == 1 else -1)


for t in [*open(0)][1:]:
	n = int(t)
	r = 0
	while n % 3 < 1:
		r += n % 2 + 1
		n //= 6 - n % 6
	print((r, -1)[n > 1])


def get(n, p):
	count = 0
	while not n % p:
		n //= p
		count += 1
	return count
for _ in range(int(input())):
	n = int(input())
	p = get(n, 2)
	q = get(n, 3)
	if n != 2 ** p * 3 ** q:
		print(-1)
		continue
	count = 0
	while p or q:
		if p and q:
			p -= 1
			q -= 1
		elif p:
			print(-1)
			break
		else:
			p += 1
		count += 1
	else:
		print(count)


a = int(input())
for i in range(a):
	x = int(input())
	temp = 0
	count = 0
	while True:
		if temp == 2:
			print(-1)
			break
		if x == 1:
			print(count)
			break
		if x // 6 == x / 6:
			x //= 6
			temp = 0
			count += 1
		else:
			x *= 2
			temp += 1
			count += 1


testcases = int(input())
while testcases != 0:
	x = int(input())
	(two_count, three_count) = (0, 0)
	while x % 2 == 0:
		x = x / 2
		two_count = two_count + 1
	while x % 3 == 0:
		x = x / 3
		three_count = three_count + 1
	if two_count <= three_count and x == 1:
		divide_by_sixx_moves = min(two_count, three_count)
		two_count = two_count - divide_by_sixx_moves
		three_count = three_count - divide_by_sixx_moves
		if two_count == three_count:
			print(divide_by_sixx_moves)
		else:
			print(divide_by_sixx_moves + (three_count - two_count) * 2)
	else:
		print(-1)
	testcases = testcases - 1


def solve(p):
	two = 0
	three = 0
	while p > 1:
		if p / 2 != p // 2:
			break
		p //= 2
		two += 1
	while p > 1:
		if p / 3 != p // 3:
			break
		p //= 3
		three += 1
	if p != 1:
		return -1
	if two > three:
		return -1
	return three - two + three
t = int(input())
for i in range(t):
	n = int(input())
	ans = solve(n)
	print(ans)


from sys import stdin
input = stdin.readline
t = int(input())
for ts in range(t):
	n = int(input())
	(c2, c3) = (0, 0)
	while n % 2 == 0:
		c2 += 1
		n //= 2
	while n % 3 == 0:
		c3 += 1
		n //= 3
	if n == 1 and c2 <= c3:
		print(c3 - c2 + c3)
	else:
		print(-1)


t = int(input())
for T in range(t):
	n = int(input())
	N = n
	k2 = 0
	k3 = 0
	while N % 2 == 0:
		k2 += 1
		N /= 2
	while N % 3 == 0:
		k3 += 1
		N /= 3
	if N != 1 or k3 < k2:
		print(-1)
		continue
	print(2 * k3 - k2)


def isPossible(num):
	(count_2, count_3, flag) = (0, 0, False)
	while num % 2 == 0:
		num /= 2
		count_2 += 1
	while num % 3 == 0:
		num /= 3
		count_3 += 1
	if num == 1:
		flag = True
	return (count_2, count_3, flag)
t = int(input())
for i in range(t):
	n = int(input())
	(p2, p3, f) = isPossible(n)
	if f and p3 >= p2:
		print(2 * p3 - p2)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		b = 0
		c = 0
		x = 0
		y = 0
		while b == 0:
			if n % 2 == 0:
				n = n // 2
				x += 1
			else:
				b += 1
		while c == 0:
			if n % 3 == 0:
				n = n // 3
				y += 1
			else:
				c += 1
		if n != 1:
			print(-1)
		elif x > y:
			print(-1)
		else:
			print(y + (y - x))


for _ in range(int(input())):
	x = int(input())
	(tw, th) = (0, 0)
	while x % 2 == 0:
		x = x // 2
		tw = tw + 1
	while x % 3 == 0:
		x = x // 3
		th = th + 1
	if tw > th or x != 1:
		print(-1)
	else:
		print(th - tw + th)


import sys
input = sys.stdin.readline
for T in range(int(input())):
	n = int(input().strip())
	cnt2 = 0
	cnt3 = 0
	while n % 2 == 0:
		cnt2 += 1
		n //= 2
	while n % 3 == 0:
		cnt3 += 1
		n //= 3
	if n > 1:
		print(-1)
	elif cnt2 > cnt3:
		print(-1)
	else:
		d = cnt3 - cnt2
		print(d + cnt3)


t = int(input())
for _ in range(t):
	n = int(input())
	c3 = 0
	c2 = 0
	m = n
	if n == 1:
		print(0)
		continue
	while m % 3 == 0:
		m = m // 3
		c3 += 1
	m = n
	while m % 2 == 0:
		m = m // 2
		c2 += 1
	reff = 1
	if c2 > c3:
		print(-1)
	else:
		for i in range(c3):
			reff = reff * 3
			reff = reff * 2
		if reff % n == 0:
			print(c3 - c2 + c3)
		else:
			print(-1)


import math
t = int(input())
for k in range(t):
	n = int(input())
	count = 0
	while n > 1:
		if n % 6 == 0:
			n = n / 6
			count = count + 1
		elif n % 6 != 0:
			rem = n % 6
			if rem != 3:
				break
			elif rem == 3:
				n = n * 2
				count = count + 1
	if n == 1:
		print(count)
	else:
		print(-1)


powers = [2 ** x for x in range(1, 31)]
t = int(input())
for x in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	elif n % 3 != 0:
		print(-1)
		continue
	else:
		temp = n
		steps = 0
		flag = 0
		while temp > 1:
			if temp % 2 != 0:
				temp *= 2
				steps += 1
			if temp % 6 == 0:
				temp /= 6
				steps += 1
			if temp == 1:
				flag = 1
				break
			elif temp % 3 != 0:
				break
		if flag == 0:
			print(-1)
		else:
			print(steps)


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	n = I()
	ans = []
	for _ in range(n):
		a = I()
		b = c = 0
		while True:
			if a % 2 != 0:
				break
			b += 1
			a //= 2
		while True:
			if a % 3 != 0:
				break
			c += 1
			a //= 3
		if a != 1:
			ans.append(-1)
		elif b > c:
			ans.append(-1)
		else:
			ans.append(c * 2 - b)
	for x in ans:
		print(x)
main()


def f(x):
	m = x
	cnt2 = 0
	cnt3 = 0
	while True:
		if m % 2 == 0:
			cnt2 = cnt2 + 1
			m = m // 2
		elif m % 3 == 0:
			cnt3 = cnt3 + 1
			m = m // 3
		elif m > 1:
			return -1
		elif cnt3 >= cnt2:
			return 2 * cnt3 - cnt2
		else:
			return -1
t = int(input())
for i in range(0, t):
	x = int(input())
	print(f(x))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	cnt = defaultdict(int)
	while n % 2 == 0:
		n /= 2
		cnt[2] += 1
	while n % 3 == 0:
		n /= 3
		cnt[3] += 1
	if n > 1:
		print(-1)
	else:
		(a, b) = (cnt[2], cnt[3])
		if a > b:
			print(-1)
		else:
			print(2 * b - a)


for _ in range(int(input())):
	n = int(input())
	tr = 0
	to = 0
	broke = False
	while n != 1:
		if n % 3 == 0:
			n = n // 3
			tr += 1
		elif n % 2 == 0:
			n = n // 2
			to += 1
		else:
			print(-1)
			broke = True
			break
	if not broke:
		if to > tr:
			print(-1)
		elif to == tr:
			print(to)
		else:
			print(2 * tr - to)


import math
for _ in range(int(input())):
	n = int(input())
	if n % 3 != 0 and n != 1:
		print('-1')
	elif n == 1:
		print('0')
	else:
		two = 0
		three = 0
		flag = True
		while n > 1:
			if n % 6 == 0:
				n = n // 6
				three += 1
				two += 1
			elif n % 3 == 0:
				n = n // 3
				three += 1
			elif n % 2 == 0:
				n = n // 2
				two += 1
			else:
				print('-1')
				flag = False
				break
		if two > three and flag:
			print('-1')
		elif flag:
			res = abs(two - three) + max(two, three)
			print(res)


t = int(input())
answer = []
while t > 0:
	n = int(input())
	count2 = 0
	count3 = 0
	while n % 2 == 0:
		n //= 2
		count2 += 1
	while n % 3 == 0:
		n //= 3
		count3 += 1
	if n == 1 and count2 <= count3:
		answer.append(str(2 * count3 - count2))
	else:
		answer.append(str(-1))
	t -= 1
print('\n'.join(answer))


def perform_bubble_sort(blist):
	(cmpcount, swapcount) = (0, 0)
	blist = list(map(int, input().split()))
	for j in range(len(blist)):
		for i in range(1, len(blist) - j):
			cmpcount += 1
			if blist[i - 1] > blist[i]:
				swapcount += 1
				(blist[i - 1], blist[i]) = (blist[i], blist[i - 1])
	print(swapcount)

def fun(l, c, d):
	if c == n:
		return d
	if c % 2 == 0:
		d = max(fun(l[:-1], c + 1, d + l[-1]), fun(l[1:], c + 1, d + l[0]))
	else:
		d = min(fun(l[1:], c + 1, d - l[0]), fun(l[:-1], c + 1, d - l[-1]))
	return d
t = int(input())
while t:
	t -= 1
	n = int(input())
	a = 0
	b = 0
	while n % 2 == 0:
		n //= 2
		a += 1
	while n % 3 == 0:
		n //= 3
		b += 1
	if n > 1:
		print(-1)
	elif a > b:
		print(-1)
	else:
		print(b - a + b)


for z in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		c1 = 0
		c2 = 0
		while n % 2 == 0:
			n = n // 2
			c1 += 1
		while n % 3 == 0:
			n = n // 3
			c2 += 1
		if n == 1 and c1 <= c2:
			print(2 * c2 - c1)
		else:
			print(-1)


def factor(n):
	Ans = []
	d = 2
	while d * d <= n:
		if n % d == 0:
			Ans.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		Ans.append(n)
	return Ans

def f(n):
	if n == 1:
		return 0
	count_2 = 0
	while n % 2 == 0:
		count_2 += 1
		n //= 2
	count_3 = 0
	while n % 3 == 0:
		count_3 += 1
		n //= 3
	if n != 1:
		return -1
	if count_2 > count_3:
		return -1
	else:
		return count_3 + (count_3 - count_2)
t = int(input())
for _ in range(t):
	print(f(int(input())))


from collections import deque
D = {1: 0}
Q = deque([1])
MAX = 10 ** 9

def add(j, i):
	if 1 <= j <= MAX and j not in D:
		D[j] = D[i] + 1
		Q.append(j)
while Q:
	i = Q.popleft()
	if i % 2 == 0:
		add(i // 2, i)
	add(i * 6, i)
for _ in range(int(input())):
	print(D.get(int(input()), -1))


for _ in range(int(input())):
	n = int(input())
	pow2 = 0
	pow3 = 0
	while n % 2 == 0:
		n = n / 2
		pow2 += 1
	while n % 3 == 0:
		n = n / 3
		pow3 += 1
	output = -1
	if n == 1:
		if pow3 >= pow2:
			output = 2 * pow3 - pow2
	print(output)


t = int(input())
ans = []
for k in range(t):
	n = int(input())
	div6 = 0
	div3 = 0
	while n % 6 == 0:
		n /= 6
		div6 += 1
	while n % 3 == 0:
		n /= 3
		div3 += 1
	if n != 1:
		ans.append(-1)
	else:
		ans.append(div6 + div3 * 2)
for i in range(t):
	print(ans[i])


def solver(n):
	c = 0
	while n != 1 and n % 3 == 0:
		if n % 6 != 0:
			n *= 2
		else:
			n //= 6
		c += 1
	print(c if n == 1 else -1)
t = int(input())
while t > 0:
	solver(int(input()))
	t -= 1


import sys, math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	(c, flag) = (0, 0)
	while True:
		if n % 6 == 0:
			n = n // 6
			c = c + 1
		else:
			break
	while True:
		if n == 1:
			break
		elif n % 3 != 0:
			flag = 1
			break
		else:
			n = n // 3
			c = c + 2
	if flag == 0:
		print(c)
	else:
		print(-1)


def solve(n):
	if n == 1:
		return 0
	if n == 2:
		return -1
	if n == 3:
		return 2
	k = 1
	k = n
	two = 0
	three = 0
	while k % 2 == 0:
		k //= 2
		two += 1
	k = n
	while k % 3 == 0:
		k //= 3
		three += 1
	if two > three:
		return -1
	n = n / pow(2, two)
	n = n / pow(3, three)
	if n != 1:
		return -1
	return three * 2 - two
t = int(input())
for we in range(t):
	n = int(input())
	print(solve(n))


for _ in range(int(input())):
	n = int(input())
	if n % 3 == 0:
		cnt = 0
		flg = False
		while True:
			if n % 3 != 0 and n != 1:
				flg = True
				break
			elif n == 1:
				flg = False
				break
			elif n % 6 == 0:
				n //= 6
			else:
				n *= 2
			cnt += 1
		if flg == False:
			print(cnt)
		else:
			print(-1)
	elif n == 1:
		print(0)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	flag = True
	c = 0
	while n != 1 and flag:
		if n and (not n & n - 1) or n % 3 != 0:
			flag = False
			break
		elif n % 6 == 0:
			c = c + 1
			n = n // 6
		else:
			n = n * 2
			c = c + 1
	if flag:
		print(c)
	else:
		print(-1)


def driver():
	t = int(input())
	for i in range(t):
		print(solution())

def solution():
	n = int(input())
	c = 0
	while n != 1:
		if n % 6 == 0:
			c += 1
			n //= 6
		elif n % 3 == 0:
			c += 1
			n *= 2
		else:
			return -1
	else:
		return c
driver()


def check(n):
	flag = 0
	count = 0
	while n != 1:
		if n % 6 == 0:
			n = n / 6
			flag = 0
			count += 1
		else:
			if flag == 1:
				return -1
			n = n * 2
			count += 1
			flag = 1
	return count
for _ in range(int(input())):
	n = int(input())
	print(check(n))


from sys import stdin
input = stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	cnt = 0
	while n % 6 == 0:
		n //= 6
		cnt += 1
	while n % 3 == 0:
		n //= 3
		cnt += 2
	if n == 1:
		print(cnt)
	else:
		print('-1')


def get_steps(n):
	if n == 1:
		return 0
	if n % 3 != 0:
		return -1
	elif n % 2 == 0:
		steps = get_steps(n / 6)
		if steps == -1:
			return -1
		else:
			return 1 + steps
	else:
		steps = get_steps(n / 3)
		if steps == -1:
			return -1
		else:
			return 2 + steps
for t in range(int(input())):
	print(get_steps(int(input())))


def solution(n):
	cnt = 0
	lmt = 0
	while n > 1:
		if n % 6 == 0:
			cnt = cnt + 1
			n = n / 6
		else:
			if n * 2 % 6 == 0:
				n = n * 2 / 6
				cnt = cnt + 2
			else:
				n = n * 2
				cnt = cnt + 1
				lmt = lmt + 1
			if lmt > 50:
				break
	if n == 1:
		return cnt
	else:
		return '-1'
for _ in range(int(input())):
	n = int(input())
	print(solution(n))


import sys
import random
from math import *

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def finput():
	return float(input())

def tinput():
	return input().split()

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

def fiinput():
	return map(float, tinput())

def rlinput():
	return list(map(int, input().split()))

def trinput():
	return tuple(rinput())

def srlinput():
	return sorted(list(map(int, input().split())))

def NOYES(fl):
	if fl:
		print('NO')
	else:
		print('YES')

def YESNO(fl):
	if fl:
		print('YES')
	else:
		print('NO')

def main():
	n = iinput()
	(t, g) = (0, 0)
	while n % 3 == 0:
		g += 1
		n //= 3
	while n % 2 == 0:
		n //= 2
		t += 1
	if n != 1 or t > g:
		print(-1)
	else:
		print(g * 2 - t)
for inytd in range(iinput()):
	main()


t = int(input())
for i in range(t):
	n = int(input())
	j = 0
	temp = j
	temp2 = n
	while temp2 != 1:
		if temp2 == 0 or (temp2 % 2 == 0 and temp2 % 3 != 0):
			j = 0
			break
		if temp2 % 6 == 0:
			temp2 = temp2 // 6
			if temp2 == 1:
				j = 1
		else:
			temp2 = temp2 * 2
		temp = temp + 1
	if n == 1:
		print(0)
	elif j == 1:
		print(temp)
	else:
		print('-1')


def two_six_func(num):
	i = 0
	n = num
	while n != 1:
		if n % 3 != 0:
			return -1
		if n % 2 != 0:
			n *= 2
			i += 1
		n /= 6
		i += 1
	return i
tests = []
l = int(input())
i = 0
while i < l:
	tests.append(int(input()))
	i += 1
print()
for n in tests:
	print(two_six_func(n))


import math
t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	flag = 0
	while n > 1:
		t = n % 6
		if t == 1 or t == 2 or t == 4 or (t == 5):
			print(-1)
			flag = 1
			break
		if t == 0:
			n = n // 6
			count = count + 1
		elif t == 3:
			n = n * 2
			count = count + 1
	if flag == 0:
		print(count)


a = int(input())
for i in range(a):
	b = int(input())
	c = 0
	while b != 1:
		if b % 3 == 0:
			if b % 2 == 0:
				b = b / 6
				c += 1
			else:
				b = b * 2
				c += 1
		else:
			c = -1
			break
	print(c)


from math import log2
for _ in range(int(input())):
	n = int(input())
	k = 0
	if n == 1:
		a = 0
		s = 0
	else:
		b = n
		a = 0
		while b % 3 == 0:
			a += 1
			b //= 3
		s = log2(b)
		if s == int(s):
			s = int(s)
			if s <= a:
				pass
			else:
				k = 1
		else:
			k = 1
	if k:
		print(-1)
	else:
		print(2 * a - s)


def check(x):
	n = 0
	while x > 5:
		if x % 6 == 0:
			x = x / 6
			n += 1
		elif x % 3 == 0:
			x = x / 3
			n += 2
		else:
			n = -1
			break
	if x == 1:
		n = n
	elif x == 3:
		n = n + 2
	elif x in [2, 4, 5]:
		n = -1
	else:
		pass
	return n
for i in range(int(input())):
	num = int(input())
	print(check(num))


n = int(input())
for _ in range(n):
	i = int(input())
	c = 0
	check = True
	while i != 1:
		if i % 6 != 0 and i % 6 != 3:
			print(-1)
			check = False
			break
		if i % 6 == 0:
			c += 1
			i = i // 6
		else:
			c += 1
			i = i * 2
	if check:
		print(c)


import sys
sys.setrecursionlimit(100000)

def read():
	return int(input())

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
cas = read()
while cas:
	cas -= 1
	x = read()
	(two, six) = (0, 0)
	while x % 2 == 0:
		x /= 2
		two += 1
	while x % 3 == 0:
		x /= 3
		six += 1
	if x != 1 or two > six:
		print(-1)
	else:
		print(six * 2 - two)


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n > 1 and n < 3:
		print(-1)
	else:
		c = 0
		flag = 1
		while n != 1:
			if n % 6 == 0:
				n /= 6
			elif n % 3 == 0:
				n *= 2
			else:
				print(-1)
				flag = 0
				break
			c += 1
		if flag:
			print(c)


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	ns = 0
	while 1:
		if n % 6 == 0:
			n = n // 6
			ns += 1
		elif n % 3 == 0:
			n = n * 2
			ns += 1
		elif n == 1:
			break
		else:
			ns = -1
			break
	ans.append(ns)
for el in ans:
	print(el)


t = int(input().strip())
for i in range(t):
	num = int(input().strip())
	count = 0
	while True:
		if num == 1:
			print(count)
			break
		if num % 6 == 0:
			count += 1
			num /= 6
		elif num % 6 == 3:
			count += 1
			num *= 2
		elif num % 6 != 3:
			print(-1)
			break


def f(n):
	a = 0
	b = 0
	while n != 1:
		if n % 2 != 0 and n % 3 != 0:
			return (False, 0, 0)
		else:
			if n % 2 == 0:
				n //= 2
				a += 1
			if n % 3 == 0:
				n //= 3
				b += 1
	if n == 1:
		return (True, a, b)
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		t = f(n)
		if t[0] == False:
			print(-1)
		else:
			a = t[1]
			b = t[2]
			if a == b:
				print(a)
			if a > b:
				print(-1)
			if a < b:
				print(2 * b - a)


def counter(no):
	count = 0
	while no != 1:
		if no % 6 == 0:
			no = no // 6
			count = count + 1
		elif no * 2 % 6 == 0:
			no = no * 2
			count = count + 1
			no = no // 6
			count = count + 1
		else:
			return -1
	return count
t = int(input())
for i in range(t):
	x = int(input())
	print(counter(x))


n = input()
arr = []
for i in range(int(n)):
	arr.append(list(map(int, input().split())))
for j in range(int(n)):
	x = 0
	if int(arr[j][0]) == 1:
		print('0')
	elif int(arr[j][0]) % 6 == 0:
		while int(arr[j][0]) % 6 == 0:
			arr[j][0] = int(arr[j][0]) / 6
			x = x + 1
		if int(arr[j][0]) % 3 == 0:
			while int(arr[j][0]) % 3 == 0:
				arr[j][0] = int(arr[j][0]) / 3
				x = x + 2
			if int(arr[j][0]) == 1:
				print(x)
			else:
				print('-1')
		elif int(arr[j][0]) == 1:
			print(x)
		elif int(arr[j][0]) % 3 != 0:
			print('-1')
	elif int(arr[j][0]) % 3 == 0:
		while int(arr[j][0]) % 3 == 0:
			arr[j][0] = int(arr[j][0]) / 3
			x = x + 2
		if int(arr[j][0]) == 1:
			print(x)
		else:
			print('-1')
	else:
		print('-1')


for i in range(int(input())):
	a = int(input())
	(p, q) = (0, 0)
	while a % 2 == 0:
		p += 1
		a = a // 2
	while a % 3 == 0:
		q += 1
		a = a // 3
	if a > 1:
		print(-1)
	elif q >= p:
		print(min(p, q) + 2 * (q - p))
	else:
		print(-1)


import sys, collections, math

def in_out():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(int(input())):
	n = int(input())
	three = 0
	two = 0
	while n % 3 == 0:
		n = n // 3
		three += 1
	while n % 2 == 0:
		n = n // 2
		two += 1
	if n > 1:
		print(-1)
		continue
	if two > three:
		print(-1)
		continue
	req = three - two
	print(req * 2 + two)


t = int(input())
for i in range(t):
	active = True
	n = int(input())
	counter = 0
	if n == 1:
		active = False
		print(0)
	elif n == 2:
		active = False
		print(-1)
	else:
		while n != 1:
			if n % 6 == 0:
				n /= 6
				counter += 1
			else:
				n *= 2
				counter += 1
			if n > 1000000000:
				print(-1)
				active = False
				break
	if active == True:
		print(counter)


T = int(input())
for t in range(1, T + 1):
	N = int(input())
	answer = 0
	div_2 = 0
	div_3 = 0
	x = N
	while x % 3 == 0:
		x = x // 3
		div_3 += 1
	while x % 2 == 0:
		x = x // 2
	if x != 1:
		print(-1)
		continue
	x = N
	while x % 2 == 0:
		x = x // 2
		div_2 += 1
	if div_3 < div_2:
		print(-1)
		continue
	answer = div_2 + 2 * div_3
	N = N * 2 ** (div_3 - div_2)
	answer = 0
	while N > 1:
		N = N // 6
		answer += 1
	print(answer + div_3 - div_2)


import collections
t = int(input())
for _ in range(0, t):
	realn = int(input())
	n = realn
	len3 = 0
	len2 = 0
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		while n >= 3:
			if n % 3 == 0:
				len3 += 1
				n = n // 3
			else:
				break
		while n >= 2:
			if n % 2 == 0:
				len2 += 1
				n = n // 2
			else:
				break
		if len2 > len3:
			print(-1)
		else:
			multi = len3 - len2
			mul = len3
			for _ in range(0, multi):
				realn *= 2
			for _ in range(0, mul):
				realn //= 6
			if realn == 1:
				print(multi + mul)
			else:
				print(-1)


def B(n):
	if n == 1:
		return 0
	count = 0
	while n % 6 == 0:
		n /= 6
		count += 1
	while n % 3 == 0:
		n /= 3
		count += 2
	if n == 1:
		return count
	return -1
a = int(input())
for i in range(a):
	n = int(input())
	print(B(n))


from sys import stdin, stdout
from sys import maxsize

def solve():
	pass
test = 1
test = int(input())
for t in range(0, test):
	n = int(input())
	(p2, p3) = (0, 0)
	while n % 2 == 0:
		p2 += 1
		n = n // 2
	while n % 3 == 0:
		p3 += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif p2 > p3:
		print(-1)
	else:
		print(2 * p3 - p2)
	ans = solve()


from sys import stdin as lector
input = lector.readlines()
for i in range(1, int(input[0]) + 1):
	max = 0
	n = int(input[i])
	found = False
	while found == False:
		if n == 1:
			found = True
		elif n % 6 == 0:
			max = max + 1
			n = n / 6
		elif 2 * n % 6 == 0:
			n = 2 * n
			max = max + 1
		else:
			found = True
			max = -1
	print(max)


n = int(input())
for i in range(n):
	num = int(input())
	step = 0
	while num != 1:
		if num % 6 == 0:
			num //= 6
			step += 1
		elif num % 3 == 0:
			num *= 2
			step += 1
		else:
			step = -1
			break
	print(step)


x = int(input())
for _ in range(x):
	n = int(input())
	broken = False
	count = 0
	while n != 1:
		if n == 2:
			broken = True
			break
		elif n % 2 == 0 and n % 6 != 0:
			broken = True
			break
		elif n % 6 == 0:
			n = n // 6
			count += 1
		else:
			n *= 2
			count += 1
	if broken == True:
		print('-1')
	else:
		print(count)


t = int(input())
for tt in range(t):
	n = int(input())
	pow2 = 0
	pow3 = 0
	while n > 0 and n % 2 == 0:
		pow2 += 1
		n //= 2
	while n > 0 and n % 3 == 0:
		pow3 += 1
		n //= 3
	if n != 1:
		print(-1)
		continue
	if pow2 > pow3:
		print(-1)
		continue
	ans = pow3 - pow2
	ans += pow3
	print(ans)


def minStep(n, count):
	if n == 1:
		return count
	if n % 3 != 0:
		return -1
	elif n % 6 == 0:
		count += 1
		n1 = n // 6
		return minStep(n1, count)
	else:
		n2 = n * 2
		count += 1
		return minStep(n2, count)
t = int(input())
arr = []
count = 0
for i in range(t):
	arr.append(int(input()))
for i in range(t):
	print(minStep(arr[i], count))


t = int(input())
for _ in range(t):
	n = int(input())
	flag = True
	(c2, c3) = (0, 0)
	while n % 2 == 0:
		n /= 2
		c2 += 1
	while n % 3 == 0:
		n /= 3
		c3 += 1
	if n == 1 and c2 <= c3:
		print(2 * c3 - c2)
	else:
		print('-1')


import math
for _ in range(int(input())):
	n = int(input())
	x = 0
	if n % 6 != 0:
		if n % 3 == 0:
			n = n * 2
			x += 1
	flag = 0
	while n != 1:
		p = math.log(n, 2)
		if p == int(p) or (n % 6 != 0 and n % 3 != 0):
			flag = 1
			break
		if n % 6 == 0:
			n = n // 6
		else:
			n = n * 2
		x += 1
	if flag == 1:
		print(-1)
	else:
		print(x)


for i in range(int(input())):
	num = int(input())
	c = 0
	c1 = 1
	if num == 1:
		print(0)
		continue
	while num != 1:
		if num % 6 == 0:
			num = num // 6
			c = c + 1
		elif num % 3 == 0 and num % 2 != 0:
			num = num * 2
			c = c + 1
		elif num % 3 != 0:
			print(-1)
			c1 = 0
			break
	if c1 == 1:
		print(c)


t = int(input())
for x in range(t):
	n = int(input())
	a = 0
	b = 0
	d = n
	while d % 2 == 0 or d % 3 == 0:
		if d % 2 == 0:
			a += 1
			d = d // 2
		if d % 3 == 0:
			b += 1
			d = d // 3
	if d != 1:
		print(-1)
	elif a > b:
		print(-1)
	else:
		print(b - a + b)


t = int(input())
compt_tot = []
for kk in range(t):
	n = int(input())
	num = n
	compt = 0
	compt2 = 0
	while num != 1 and compt2 < 2:
		if num % 6 == 0:
			num = num / 6
			compt = compt + 1
			compt2 = 0
		else:
			num = num * 2
			compt = compt + 1
			compt2 = compt2 + 1
	if compt2 == 2:
		compt = -1
	compt_tot.append(compt)
for i in range(t):
	print(compt_tot[i])


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		ans = 0
		val = 0
		while n % 2 == 0:
			n //= 2
			val += 1
		while n % 3 == 0:
			n //= 3
			ans += 1
		if n > 1 or val > ans:
			print(-1)
		else:
			print(2 * ans - val)


def f(n):
	ans = 0
	while n % 6 == 0:
		ans += 1
		n /= 6
	while n % 3 == 0:
		ans += 2
		n /= 3
	return (-1, ans)[n == 1]

def main():
	t = int(input())
	while t > 0:
		print(f(int(input())))
		t -= 1
main()


sixPowers = [1, 6, 36, 216, 1296, 7776, 46656, 279936, 1679616, 10077696, 60466176, 362797056]
t = int(input())
for testcase in range(t):
	A = int(input())
	i = 0
	while A != 1:
		if A % 6 != 0:
			A = A * 2
			i += 1
			if A % 6 != 0:
				break
		if A * 2 in sixPowers:
			A = A * 2
		else:
			A = A / 6
		i += 1
	if A != 1:
		print(-1)
	else:
		print(i)


for _ in range(int(input())):
	n = int(input())
	p = 0
	ans = 0
	while p < 100 and n > 1:
		if n % 6 == 0:
			n /= 6
			ans += 1
		else:
			n *= 2
			p += 1
			ans += 1
	if p == 100:
		print(-1)
	else:
		print(ans)


subtests = int(input())
for subtest in range(subtests):
	n = int(input())
	ans = 0
	ok = True
	while n != 1:
		if n % 6 and 2 * n % 6:
			print(-1)
			ok = False
			break
		if not n % 6:
			n //= 6
			ans += 1
		else:
			n *= 2
			ans += 1
	if ok:
		print(ans)


def identity(*args):
	if len(args) == 1:
		return args[0]
	return args

def parsin(*, l=1, vpl=1, cf=identity, s=' '):
	if l == 1:
		if vpl == 1:
			return cf(input())
		else:
			return list(map(cf, input().split(s)))
	elif vpl == 1:
		return [cf(input()) for _ in range(l)]
	else:
		return [list(map(cf, input().split(s))) for _ in range(l)]

def condition_satisfy(a: int):
	steps = 0
	while a != 1:
		if a % 6 == 0:
			a /= 6
		elif a % 3 != 0:
			return -1
		else:
			a *= 2
		steps += 1
	return steps

def main():
	t = int(input())
	if t == 1:
		print(condition_satisfy(int(input())))
		return 0
	for each in parsin(l=t, vpl=1, cf=int, s='\n'):
		print(condition_satisfy(each))
main()


t = int(input())
for i in range(t):
	n = int(input())
	x = 0
	while True:
		if n == 1:
			break
		elif n == 2 or n == 4 or n == 5:
			x = -1
			break
		if n % 6 == 0:
			n = n // 6
			x += 1
		elif n % 3 == 0:
			n = n * 2
			x += 1
		elif n % 3 != 0:
			x = -1
			break
	print(x)


t = int(input())
for case in range(t):
	n = int(input())
	i = 0
	j = 0
	while n % 2 == 0:
		n = n // 2
		i += 1
	while n % 3 == 0:
		n = n // 3
		j += 1
	if n > 1 or i > j:
		ans = -1
	else:
		ans = 2 * j - i
	print(ans)


for i in range(int(input())):
	n = int(input())
	count = 0
	a = 1
	x = n
	d = {}
	if n == 1:
		print('0')
	elif n % 3 != 0:
		print('-1')
	else:
		while n != 1:
			if (n / 6).is_integer():
				n = n // 6
			elif n not in d:
				n = n * 2
				d[n] = 1
			else:
				a = 0
				break
			count += 1
		if a == 0:
			print('-1')
		else:
			print(count)


n = int(input())
result = []
for i in range(n):
	count = 0
	num = int(input())
	flag = 0
	if num == 1:
		result.append(0)
	while num > 0 and num != 1:
		if num % 6 == 0:
			num = num / 6
			count += 1
		elif num % 3 == 0:
			num *= 2
			count += 1
		else:
			break
		if num == 1:
			result.append(count)
			flag = 1
			break
	if flag == 0 and num != 1:
		result.append(-1)
for j in result:
	print(j)


for _ in range(int(input())):
	a = int(input())
	c3 = 0
	c2 = 0
	while a % 3 == 0:
		c3 += 1
		a //= 3
	while a % 2 == 0:
		c2 += 1
		a //= 2
	if a != 1 or c2 > c3:
		print(-1)
	else:
		print(c3 + (c3 - c2))


def find2(n):
	s = 0
	while n % 2 == 0:
		n = n // 2
		s += 1
	return s

def find3(n):
	s = 0
	while n % 3 == 0:
		n = n // 3
		s += 1
	return s
for i in range(int(input())):
	n = int(input())
	n2 = find2(n)
	n3 = find3(n)
	if n == 1:
		print(0)
	elif 2 ** n2 * 3 ** n3 != n:
		print(-1)
	elif n3 < n2:
		print(-1)
	else:
		print(n3 - n2 + n3)


t = int(input())
for _ in range(t):
	n = int(input())
	two = 0
	thr = 0
	while n % 2 == 0:
		n //= 2
		two += 1
	while n % 3 == 0:
		n //= 3
		thr += 1
	if n != 1 or two > thr:
		print(-1)
	else:
		print(2 * thr - two)


t = int(input())
for _ in range(t):
	a = int(input())
	count = 0
	if a == 1:
		count = 0
	while a != 1:
		if count > 50:
			count = -1
			break
		if a % 6 == 0:
			a = int(a / 6)
			count = count + 1
		else:
			a = int(a * 2)
			count = count + 1
	print(count)
	count = 0


for i in range(int(input())):
	x = int(input())
	cnt2 = 0
	cnt3 = 0
	while x % 2 == 0:
		cnt2 += 1
		x /= 2
	while x % 3 == 0:
		cnt3 += 1
		x /= 3
	if x > 1 or cnt2 > cnt3:
		print(-1)
	else:
		print(cnt3 + cnt3 - cnt2)


from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
t = sInt()
for _ in range(t):
	n = sInt()
	ans = 0
	while n % 6 == 0:
		n = n / 6
		ans += 1
	while n % 3 == 0:
		n = n / 3
		ans += 2
	if n == 1:
		print(ans)
	else:
		print(-1)


for i in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		two += 1
		n /= 2
	while n % 3 == 0:
		three += 1
		n /= 3
	if n == 1:
		if two <= three:
			res = min(two, three)
			two -= res
			three -= res
			res += three * 2
			print(res)
		else:
			print(-1)
	else:
		print(-1)


def f(x):
	if x == 0:
		return -1
	t = 0
	while x % 3 == 0:
		if x % 2 != 0:
			t += 2
			x /= 3
		else:
			x /= 6
			t += 1
	if x != 1:
		return -1
	else:
		return t
n = int(input())
a = []
for i in range(n):
	inpu = int(input())
	a.append(f(inpu))
for i in a:
	print(i)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	ctwo = 0
	cthree = 0
	while n % 2 == 0:
		n = n // 2
		ctwo += 1
	while n % 3 == 0:
		n = n // 3
		cthree += 1
	if cthree < ctwo or n > 1:
		print(-1)
	else:
		print(2 * cthree - ctwo)


def solve():
	n = int(input())
	ans = 0
	while True:
		if n == 1:
			return ans
		if n % 6 == 0:
			n = n // 6
			ans += 1
		elif n * 2 % 6 != 0:
			return -1
		else:
			n = n * 2
			ans += 1
t = int(input())
for i in range(t):
	ans = solve()
	print(ans)


for n in range(int(input())):
	x = int(input())
	if x == 1:
		print(0)
		continue
	if x % 3 != 0:
		print(-1)
		continue
	tw = 0
	while x % 2 == 0:
		tw += 1
		x //= 2
	v = 0
	while x % 3 == 0:
		v += 1
		x //= 3
	if x != 1:
		print(-1)
		continue
	if tw > v:
		print(-1)
		continue
	res = v - tw
	res += v
	print(res)


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def f3(n):
	f = 0
	while n % 3 == 0:
		n //= 3
		f += 1
	return [f, n]

def f2(n):
	f = 0
	while n % 2 == 0:
		n //= 2
		f += 1
	return [f, n]
for xyz in range(0, int(input())):
	n = int(input())
	l = f3(n)
	(fa3, n) = (l[0], l[1])
	l = f2(n)
	(fa2, n) = (l[0], l[1])
	if n != 1:
		print(-1)
	elif fa2 > fa3:
		print(-1)
	else:
		print(fa3 - fa2 + fa3)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	n2 = 0
	while n % 2 == 0:
		n //= 2
		n2 += 1
	n3 = 0
	while n % 3 == 0:
		n //= 3
		n3 += 1
	if n > 1 or n2 > n3:
		print(-1)
	else:
		print(2 * n3 - n2)


def find(n):
	if n == 1:
		return 0
	c = 0
	while n != 1:
		if n % 2 == 0 and n % 3 == 0:
			n = n // 6
			c += 1
		elif n % 2 != 0 and n % 3 == 0:
			n = n * 2
			n = n // 6
			c += 2
		else:
			return -1
	return c
t = int(input())
for i in range(t):
	n = int(input())
	print(find(n))


for _ in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	while n > 1:
		if n % 2 == 0:
			n //= 2
			two += 1
		else:
			break
	while n > 1:
		if n % 3 == 0:
			n //= 3
			three += 1
		else:
			break
	if n != 1 or two > three:
		print(-1)
	else:
		print(max(two, three) + max(two, three) - min(two, three))


t = int(input())
p = []
for i in range(t):
	u = int(input())
	p.append(u)

def api(n):
	if n == 1:
		return 0
	elif n % 3 != 0:
		return -1
	else:
		t = n
		twos = 0
		thr = 0
		count = 0
		while t % 3 == 0:
			t = t // 3
			thr += 1
		while t % 2 == 0:
			t = t // 2
			twos += 1
		if t != 1:
			return -1
		elif thr < twos:
			return -1
		else:
			return twos + 2 * (thr - twos)
for i in range(t):
	r = api(p[i])
	print(r)


t = int(input())
for i in range(t):
	n = int(input())
	m = n
	two = 0
	three = 0
	diff = 0
	while n % 2 == 0:
		n = n / 2
		two += 1
	while n % 3 == 0:
		n = n / 3
		three += 1
	if three >= two and n == 1:
		diff = three - two
		print(diff + three)
	else:
		print(-1)


num = int(input())
while num > 0:
	num = num - 1
	n = int(input())
	count = 0
	flag = 1
	while n != 1 and flag == 1:
		if n % 6 == 0:
			n = n / 6
			count = count + 1
		else:
			n = n * 2
			count = count + 1
			if n % 6 != 0:
				flag = 0
				break
			n = n / 6
			count = count + 1
	if flag == 1:
		print(count)
	else:
		print('-1')


d = {}
for _ in range(int(input())):
	n = int(input())
	p = n
	if n in d.keys():
		print(d[n])
		continue
	c = 0
	while True:
		if n == 1:
			break
		if n % 6 == 0:
			n = n // 6
		else:
			s = sum(list(map(int, list(str(n)))))
			if s % 3 != 0:
				c = -1
				break
			n *= 2
		c += 1
	print(c)
	d[p] = c


def task_B():
	t = int(input())
	for _ in range(t):
		n = int(input())
		if n == 1:
			print(0)
		elif n % 3 != 0:
			print(-1)
		else:
			curr_n = n
			div_2 = 0
			div_3 = 0
			while curr_n % 2 == 0:
				div_2 += 1
				curr_n /= 2
			while curr_n % 3 == 0:
				div_3 += 1
				curr_n /= 3
			if curr_n != 1:
				print(-1)
			elif div_3 == div_2:
				print(div_3)
			elif div_3 > div_2:
				print(div_3 + div_3 - div_2)
			else:
				print(-1)

def main():
	task_B()
main()


t = int(input())
for i in range(t):
	n = int(input())
	n2 = 0
	while not n % 2:
		n2 += 1
		n //= 2
	n3 = 0
	while not n % 3:
		n3 += 1
		n //= 3
	if n != 1 or n3 < n2:
		print('-1')
	else:
		print(2 * n3 - n2)


for _ in range(int(input())):
	each = int(input())
	i = 0
	while each > 1:
		if each % 6 == 0:
			each = each / 6
			i += 1
		elif each % 2 != 0:
			each = each * 2
			i += 1
		else:
			each = 0
			i = -1
	print(i)


from math import log, floor
t = int(input())
for case in range(t):
	n = int(input())
	count = 0
	if n == 1:
		count = 0
	elif n % 3 != 0:
		count = -1
	else:
		while n % 3 == 0:
			count += 1
			n = n // 3
		count_2 = 0
		while n % 2 == 0:
			count_2 += 1
			n = n // 2
		if count_2 > count:
			count = -1
		elif n == 1:
			count += count - count_2
		else:
			count = -1
	print(count)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	x = n
	c3 = 0
	while x % 3 == 0:
		x //= 3
		c3 += 1
	x = n
	c2 = 0
	while x % 2 == 0:
		x //= 2
		c2 += 1
	x = 2 ** c2 * 3 ** c3
	if c2 > c3 or n != x:
		print(-1)
	else:
		print(c3 - c2 + c3)


for _ in range(int(input())):
	n = int(input())
	c = 0
	fl = False
	while True:
		if n % 2 == 0 and n % 3 == 0:
			c += 1
			n //= 6
			continue
		if n % 3 == 0:
			n *= 2
			c += 1
			continue
		if n == 1:
			break
		fl = True
		break
	if fl:
		print(-1)
	else:
		print(c)


def min_moves(n):
	count = 0
	if n == 1:
		return 0
	while n > 0:
		if n == 1:
			break
		elif n % 6 == 0:
			n //= 6
			count += 1
		elif n % 3 == 0:
			n *= 2
			count += 1
		else:
			return -1
	return count
t = int(input())
for _ in range(t):
	n = int(input())
	print(min_moves(n))


for u in range(int(input())):
	n = int(input())
	out = 0
	p = 1
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			out += 1
		elif n % 3 != 0:
			p = 0
			break
		else:
			n = n * 2 // 6
			out += 2
	if p == 0:
		print(-1)
	else:
		print(out)


def solve():
	n = int(input())
	n2 = 0
	n3 = 0
	while n % 2 == 0:
		n /= 2
		n2 += 1
	while n % 3 == 0:
		n /= 3
		n3 += 1
	if n > 1 or n2 > n3:
		return -1
	return n3 - n2 + n3
T = int(input())
for case in range(T):
	print(solve())


t = int(input())
while t:
	t += -1
	n = int(input())
	cnt1 = 0
	while n % 6 == 0:
		cnt1 -= -1
		n //= 6
	cnt = 0
	while n % 3 == 0:
		cnt -= -1
		n //= 3
	if n == 1:
		print(cnt * 2 + cnt1)
	else:
		print(-1)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
for _ in range(ii()):
	n = ii()
	(two, three) = (0, 0)
	while n % 3 == 0:
		n //= 3
		three += 1
	while n % 2 == 0:
		n //= 2
		two += 1
	if n != 1 or two > three:
		print(-1)
	else:
		print(three * 2 - two)


def qwe(n):
	ans = 0
	while 1:
		while n % 6 == 0:
			n //= 6
			ans += 1
		if n == 1:
			return ans
		if n % 3 != 0:
			return -1
		n *= 2
		ans += 1
for _ in range(int(input())):
	print(qwe(int(input())))


t = int(input())
for i in range(t):
	k = s = 0
	x = int(input())
	while x % 3 == 0:
		x /= 3
		k += 1
	while x % 2 == 0:
		x /= 2
		s += 1
	if (x != 1) | (s > k):
		print(-1)
	else:
		print(k + k - s)


from sys import stdin, stdout

class SOLVE:

	def solve(self):
		R = stdin.readline
		W = stdout.write
		ans = []
		for i in range(int(R())):
			n = int(R())
			two = 0
			while not n % 2:
				two += 1
				n //= 2
			three = 0
			while not n % 3:
				three += 1
				n //= 3
			if n != 1 or three < two:
				ans.append('-1')
				continue
			ans.append(str(max(two, three) + three - two))
		W('\n'.join(ans))

def main():
	s = SOLVE()
	s.solve()
main()


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	ans = 0
	while n % 6 == 0 and n > 1:
		n //= 6
		ans += 1
	while n > 1 and n % 3 == 0:
		ans += 2
		n //= 3
	if n > 1:
		ans = -1
	print(ans)


for t in range(int(input())):
	n = int(input())
	dic = {}
	dic[2] = 0
	dic[3] = 0
	ans = 0
	if n == 1:
		print(0)
		continue
	while n > 1:
		if n % 2 == 0:
			n = n // 2
			dic[2] += 1
			continue
		elif n % 3 == 0:
			n = n // 3
			dic[3] += 1
			continue
		else:
			ans = -1
			break
	if ans == -1:
		print(-1)
		continue
	elif dic[2] > dic[3]:
		print(-1)
		continue
	else:
		ans = dic[2] + (dic[3] - dic[2]) * 2
		print(ans)


for _ in range(int(input())):
	n = int(input())
	k2 = 0
	k3 = 0
	while n != 1:
		if n % 2 == 0:
			n //= 2
			k2 += 1
		else:
			break
	while n != 1:
		if n % 3 == 0:
			n //= 3
			k3 += 1
		else:
			break
	if n != 1:
		print(-1)
		continue
	if k2 > k3:
		print(-1)
	else:
		print(2 * k3 - k2)


testcase = int(input())
while testcase:
	testcase -= 1
	n = int(input())
	m = n
	if n == 1:
		print('0')
		continue
	if n % 3 != 0:
		print('-1')
		continue
	(three, two) = (0, 0)
	while n % 2 == 0:
		n = n // 2
		two += 1
	while n % 3 == 0:
		n = n // 3
		three += 1
	flag = False
	if n != 1:
		print('-1')
		continue
	elif three < two:
		print('-1')
		continue
	elif three == two:
		print(three)
		continue
	else:
		print(three + three - two)
		continue


TC = int(input())
for tc in range(TC):
	N = int(input())
	result = 0
	if N == 1:
		result = 0
	elif N % 3 != 0:
		result = -1
	else:
		while N % 3 == 0:
			N = N // 3
			result += 1
		if N > 2 ** result or 2 ** result % N != 0:
			result = -1
		else:
			N = 2 ** result // N
			while N % 2 == 0:
				N = N // 2
				result += 1
			if N > 1:
				result = -1
	print(result)


for i in range(int(input())):
	c = int(input())
	c2 = 0
	c3 = 0
	k = 0
	while k == 0:
		if c % 2 == 0:
			c = c // 2
			c2 += 1
		elif c % 3 == 0:
			c = c // 3
			c3 += 1
		else:
			k = 1
	if c == 1 and c2 <= c3:
		mini = min(c2, c3)
		maxi = max(c2, c3)
		ans = maxi - mini
		ans = maxi + ans
		print(ans)
	else:
		print(-1)


t = int(input())
while t:
	x = y = 0
	n = int(input())
	while n % 2 == 0:
		n = n // 2
		x = x + 1
	while n % 3 == 0:
		n = n // 3
		y = y + 1
	if n == 1 and x <= y:
		print(y + y - x)
	else:
		print(-1)
	t = t - 1


def deli(n):
	n3 = 0
	n2 = 0
	while n % 3 == 0:
		n //= 3
		n3 += 1
	while n % 2 == 0:
		n //= 2
		n2 += 1
	if n != 1:
		return (-1, -1)
	else:
		return (n2, n3)

def solve():
	n = int(input())
	(a, b) = deli(n)
	if a == -1:
		print(-1)
	elif b < a:
		print(-1)
	else:
		print(b + b - a)
for i in range(int(input())):
	solve()


i = 0
t = int(input())
while i < t:
	n = int(input())
	temp = n
	if n == 0:
		print(-1)
		i += 1
		continue
	elif n == 1:
		print(0)
		i += 1
		continue
	(c2, c3) = (0, 0)
	while temp % 2 == 0:
		c2 += 1
		temp /= 2
	while temp % 3 == 0:
		c3 += 1
		temp /= 3
	if temp != 1:
		print(-1)
		i += 1
		continue
	tem = n
	pas = 0
	if c3 > c2:
		pas += c3 - c2
		tem = n * 2 ** (c3 - c2)
		pas += c3
		tem = n / 6 ** c3
	elif c2 > c3:
		print(-1)
		i += 1
		continue
	else:
		pas += c2
		tem = n / 6 ** c2
	print(pas)
	i += 1


import sys
import bisect as b
import math
from collections import defaultdict as dd
input = sys.stdin.readline

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
for _ in range(inin()):
	n = inin()
	p2 = 0
	p3 = 0
	while n % 2 == 0:
		p2 += 1
		n = n // 2
	while n % 3 == 0:
		p3 += 1
		n //= 3
	if n == 1:
		if p3 >= p2:
			if p3 == p2:
				print(p3)
			elif p2 == 0:
				print(p3 * 2)
			else:
				print(p3 + p3 - p2)
		else:
			print(-1)
	else:
		print(-1)


def solve(n):
	global c
	if n == 1:
		return True
	if n % 6 == 2:
		return False
	if n % 6 == 0:
		n = n // 6
		c = c + 1
		return solve(n)
	c = c + 1
	return solve(n * 2)
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	c = 0
	if solve(n):
		print(c)
	else:
		print('-1')


for i in range(int(input())):
	n = int(input())
	sct = 0
	tct = 0
	s = n
	t = n
	while True:
		if s % 3 == 0:
			s /= 3
			sct += 1
		else:
			break
	while True:
		if t % 2 == 0:
			t /= 2
			tct += 1
		else:
			break
	if tct > sct:
		print(-1)
	elif n - 2 ** tct * 3 ** sct != 0:
		print(-1)
	elif tct == sct:
		print(tct)
	elif tct < sct:
		print(2 * sct - tct)


import sys
import math as mt
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	(three, two) = (0, 0)
	n1 = n
	while n % 3 == 0:
		n = n // 3
		three += 1
	while n % 2 == 0:
		n //= 2
		two += 1
	if three < two or 2 ** two * 3 ** three != n1:
		print(-1)
	else:
		print(max(three, two) + three - two)


for j in range(int(input())):
	k = 0
	s = int(input())
	while s != 1:
		if s % 6 == 0:
			s = s // 6
			k += 1
		elif s % 3 == 0:
			s = s * 2
			k += 1
		else:
			break
	if s == 1:
		print(k)
	else:
		print(-1)


def solve(n):
	moves = 0
	while True:
		if n == 1:
			return moves
			break
		elif n % 6 == 0:
			n = n // 6
			moves += 1
		elif n % 3 == 0:
			n = n // 3
			moves += 2
		else:
			return '-1'
moveslist = list()
for _ in range(0, int(input())):
	n = int(input())
	print(solve(n))


for _ in range(int(input())):
	n = int(input())
	if n == 3:
		print(2)
		continue
	if n == 1:
		print(0)
		continue
	if n < 6:
		print(-1)
		continue
	if n == 6:
		print(1)
		continue
	dic = {2: 0, 3: 0}
	while n % 8 == 0:
		dic[2] += 3
		n //= 8
	while n % 2 == 0:
		dic[2] += 1
		n //= 2
	while n % 27 == 0:
		dic[3] += 3
		n //= 27
	while n % 3 == 0:
		dic[3] += 1
		n //= 3
	if n > 1 or dic[2] > dic[3]:
		print(-1)
		continue
	print(2 * dic[3] - dic[2])


t = int(input())
for _ in range(0, t):
	n = int(input())
	a = []
	a.append(n)
	p = 0
	count = 0
	while n != 1:
		if n == 2 or n == 4 or n == 5:
			p = 1
			break
		elif n % 6 == 0:
			n = n // 6
			count += 1
		elif n % 3 == 0:
			n = n * 2
			count += 1
		else:
			p = 1
			break
		z = n
		if z in a:
			p = 1
			break
		else:
			a.append(z)
	if p == 0:
		print(count)
	else:
		print(-1)


import sys
input = lambda : sys.stdin.readline().strip()
li = lambda : list(map(int, input().split()))
I = lambda : int(input())
for _ in range(I()):
	n = I()
	tc = 0
	while n & 1 == 0:
		n >>= 1
		tc += 1
	c = 0
	while n % 3 == 0:
		n //= 3
		c += 1
	if n == 1 and tc <= c:
		print(c - tc + c)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	k = n
	i = 0
	k = n
	while k % 2 == 0:
		k = k // 2
		i += 1
	j = 0
	l = n
	while l % 3 == 0:
		j += 1
		l = l // 3
	if 2 ** i * 3 ** j == n:
		if j - i >= 0:
			print(j - i + j)
		else:
			print(-1)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	res = 0
	while 1:
		while n % 6 == 0:
			f = 1
			res += 1
			n //= 6
		if n == 1:
			break
		else:
			n *= 2
			res += 1
			if n % 6 == 0:
				continue
			else:
				res = -1
				break
	print(res)


from sys import stdin, stdout
import bisect

def st():
	return list(stdin.readline())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def soe(limit):
	l = [1] * (limit + 1)
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
			n = n - 1
		else:
			a = a * a
			n = n >> 1
	return r

def solve():
	n = inp()
	c = 0
	flag = 0
	while n != 1:
		if n % 6 == 0:
			c += 1
			n //= 6
		elif n * 2 % 6 == 0:
			n = n * 2
			c += 1
		else:
			flag = 1
			break
	if flag:
		pr(-1)
	else:
		pr(c)
for _ in range(inp()):
	solve()


def solve(n, ans):
	moves = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
		elif n % 3 == 0:
			n *= 2
		else:
			ans.append(-1)
			return
		moves += 1
	ans.append(moves)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		solve(n, ans)
	for i in ans:
		print(i)
main()


t = int(input())
for x in range(t):
	n = int(input())
	fail = False
	if n == 1:
		print(0)
	else:
		things = [0, 0, 0, 0]
		c = n
		for i in range(2, 4):
			if i * i > n:
				break
			count = 0
			if c % i == 0:
				count = 0
				while c % i == 0:
					c //= i
					count += 1
				things[i] = count
		if c > 1:
			if c > 3:
				fail = True
			else:
				things[c] = 1
		if things[3] == 0 or things[2] > things[3] or fail:
			print(-1)
		else:
			print(things[3] - things[2] + things[3])


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		if n == 1:
			print(0)
			continue
		cnt = 0
		while n % 3 == 0:
			cnt += 1
			n //= 3
		if cnt == 0:
			print(-1)
		else:
			cnt2 = 0
			while n % 2 == 0:
				cnt2 += 1
				n //= 2
			if cnt2 > cnt:
				print(-1)
			else:
				print(cnt2 + 2 * (cnt - cnt2) if n == 1 else -1)
	return
main()


from sys import stdin, stdout
import sys
from math import *
for __ in range(int(input())):
	n = int(input())
	a = [0, 0]
	while n % 2 == 0:
		a[0] += 1
		n = n // 2
	while n % 3 == 0:
		a[1] += 1
		n = n // 3
	if n > 1 or a[0] > a[1]:
		print(-1)
	elif a[0] == a[1]:
		print(a[0])
	else:
		print(a[1] + a[1] - a[0])


import math
import sys
from collections import defaultdict as df

def ini():
	return int(sys.stdin.readline())

def inp():
	return map(int, sys.stdin.readline().strip().split())

def li():
	return list(map(int, sys.stdin.readline().strip().split()))
for i in range(ini()):
	n = ini()
	c1 = 0
	while n % 2 == 0:
		c1 += 1
		n = n / 2
	c2 = 0
	while n % 3 == 0:
		c2 += 1
		n = n / 3
	if n != 1 or c2 < c1:
		print(-1)
	else:
		print(2 * c2 - c1)


from functools import lru_cache
T = int(input())

@lru_cache(maxsize=None)
def ans(N):
	if N == 1:
		return 0
	if N % 6 == 0:
		return 1 + ans(N // 6)
	elif N * 2 % 6 == 0:
		return 2 + ans(N * 2 // 6)
	elif N * 4 % 6 == 0:
		return 3 + ans(N * 4 // 6)
	else:
		raise RuntimeError()
for t in range(T):
	N = int(input())
	try:
		print(ans(N))
	except Exception:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3:
		print(-1)
	else:
		(c, c1) = (0, 0)
		while n % 2 == 0:
			n = n // 2
			c += 1
		while not n % 3:
			n = n // 3
			c1 += 1
		if n != 1 or c > c1:
			print(-1)
		else:
			print(2 * c1 - c)


for _ in range(int(input())):
	n = int(input())
	qu_2 = 0
	qu_3 = 0
	while n % 2 == 0:
		n //= 2
		qu_2 += 1
	while n % 3 == 0:
		n //= 3
		qu_3 += 1
	if n != 1 or qu_3 < qu_2:
		print(-1)
	else:
		print(qu_3 + (qu_3 - qu_2))


def exp_find(num, p):
	if num % p == 0:
		return exp_find(num // p, p) + 1
	else:
		return 0
for t in range(int(input())):
	n = int(input())
	exp_2 = exp_find(n, 2)
	exp_3 = exp_find(n, 3)
	n_remain = n / (2 ** exp_2 * 3 ** exp_3)
	if n_remain == 1 and exp_2 <= exp_3:
		print(2 * exp_3 - exp_2)
	else:
		print(-1)


from math import *
for _ in range(int(input())):
	n = int(input())
	c = 0
	while True:
		if n % 6 == 0:
			c += 1
			n //= 6
		elif n % 3 == 0:
			c += 2
			n //= 3
		else:
			break
	if n != 1:
		print(-1)
	else:
		print(c)


def f(n):
	if n == 1:
		return 0
	m = 0
	while n != 1:
		if n % 3 != 0:
			return -1
		elif n % 6 == 0:
			m += 1
			n = n / 6
		else:
			m += 1
			n = n * 2
	return m
t = int(input())
liste = []
for i in range(t):
	n = int(input())
	liste += [n]
for i in range(t):
	print(f(liste[i]))


t = int(input())
while t:
	n = int(input())
	count = 0
	if n % 3 == 0:
		while n > 1 and n != 2:
			if n % 6 == 0:
				n = n // 6
				count += 1
				if n % 3 != 0 and n != 1:
					print(-1)
					break
			else:
				n = n * 2
				count += 1
			if n == 1:
				print(count)
	elif n == 1:
		print(count)
	else:
		print(-1)
	t -= 1


t = int(input(''))
a = []
for i in range(t):
	n = int(input())
	tmp = n
	k2 = 0
	k3 = 0
	ans = 0
	while tmp != 1:
		f = 0
		if tmp % 2 == 0:
			k2 += 1
			tmp /= 2
			f = 1
		if tmp % 3 == 0:
			k3 += 1
			tmp /= 3
			f = 1
		if f == 0:
			ans = -1
			break
	if k2 > k3:
		ans = -1
	elif ans == 0:
		ans = k3 - k2 + k3
	a.append(ans)
for i in range(t):
	print(a[i])


t = int(input())
for ex in range(t):
	n = int(input())
	ans = int(0)
	n1 = int(n)
	while n1 > 1 and n1 <= 1000000000:
		if n1 % 6 == 0:
			n1 = n1 // 6
			ans += 1
		else:
			n1 *= 2
			ans += 1
	if n1 == 1:
		print(ans)
	else:
		print('-1')


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	limit = 10 ** 18
	ans = 0
	for test in range(tests):
		n = inp()
		three = 0
		two = 0
		while n % 3 == 0:
			three = three + 1
			n = n // 3
		while n % 2 == 0:
			two = two + 1
			n = n // 2
		if n != 1 or two > three:
			print(-1)
			continue
		else:
			print(three + (three - two))
main()


def b(n):
	step = 0
	while n != 1:
		if n % 3 != 0:
			print(-1)
			return
		if n % 6 == 0:
			n /= 6
		else:
			n *= 2
		step += 1
	print(step)
n = int(input())
for i in range(n):
	temp = int(input())
	b(temp)


for t in range(int(input())):
	n = int(input())
	p = n
	cnt = {2: 0, 3: 0}
	for e in [2, 3]:
		while not p % e:
			p //= e
			cnt[e] += 1
	if p != 1 or cnt[2] > cnt[3]:
		print(-1)
	else:
		print(cnt[2] + (cnt[3] - cnt[2]) * 2)


def get_answer(n):
	n_moves = 0
	while n % 3 == 0:
		if n % 2 == 0:
			n //= 6
		else:
			n *= 2
		n_moves += 1
	if n == 1:
		return n_moves
	else:
		return -1
t = int(input())
for i in range(t):
	n = int(input())
	print(get_answer(n))


import math
T = int(input())
for _ in range(T):
	n = int(input())
	m = n
	if n == 1:
		print(0)
	else:
		two = 0
		three = 0
		flag = 0
		while flag != 1:
			if n % 2 == 0:
				n = n // 2
				two += 1
			else:
				flag = 1
		while flag != 0:
			if n % 3 == 0:
				n = n // 3
				three += 1
			else:
				flag = 0
		if two == 0 and three == 0:
			print(-1)
		elif two > three:
			print(-1)
		elif math.pow(2, two) * math.pow(3, three) != m:
			print(-1)
		else:
			p = three - two + three
			print(p)


def nb3(n):
	y = 0
	while n % 3 == 0:
		y = y + 1
		n = n // 3
	return y

def nb2(n):
	y = 0
	while n % 2 == 0:
		y = y + 1
		n = n // 2
	return y
test = int(input())
for i in range(test):
	n = int(input())
	if n == 3 ** nb3(n) * 2 ** nb2(n) and nb3(n) - nb2(n) >= 0:
		print(2 * nb3(n) - nb2(n))
	else:
		print(-1)


a = 0
ans = 0
for i in range(int(input())):
	a = int(input())
	ans = 0
	while a % 6 == 0:
		a //= 6
		ans += 1
	while a % 3 == 0:
		a //= 3
		ans += 2
	if a == 1:
		print(ans)
	else:
		print(-1)


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
from bisect import bisect_left as bl
from bisect import bisect_right as br
import itertools
from collections import Counter
import collections
import math
import heapq
import re

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def most_frequent(list):
	return max(set(list), key=list.count)

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def Convert(string):
	li = list(string.split(''))
	return li

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
prime = []
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j

def main():
	for _ in range(inin()):
		n = inin()
		ans = 0
		flag = 0
		if n == 1:
			print(0)
			continue
		while n != 1:
			if n % 6 == 0:
				n //= 6
				ans += 1
				flag = 0
			elif flag == 0:
				n *= 2
				ans += 1
				flag = 1
			else:
				break
		if flag == 0:
			print(ans)
		else:
			print(-1)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def r(n):
	c = 0
	if n == 1:
		return c
	if n == 2:
		return -1
	while n != 1:
		if n <= 0:
			break
		elif n % 6 == 0:
			if n // 6 == 1:
				return c + 1
			else:
				n //= 6
				c += 1
		elif n * 2 % 6 != 0:
			return -1
		else:
			n = n * 2
			c += 1
	return c
for i in range(int(input())):
	n = int(input())
	print(r(n))


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	count2 = 0
	count3 = 0
	while n % 2 == 0:
		n = n // 2
		count2 += 1
	while n % 3 == 0:
		n = n // 3
		count3 += 1
	if n != 1 or count2 > count3:
		print(-1)
	else:
		ans = count3 - count2
		print(count3 + ans)


t = int(input())
while t > 0:
	n = int(input())
	ftwo = 0
	fthree = 0
	while n > 0:
		if n % 2 == 0:
			n = n // 2
			ftwo += 1
		elif n % 3 == 0:
			n = n // 3
			fthree += 1
		else:
			break
	if n != 1:
		print('-1')
	elif fthree < ftwo:
		print('-1')
	else:
		ans = 2 * fthree - ftwo
		print(ans)
	t = t - 1


from sys import stdin, stdout, setrecursionlimit
from math import ceil
for i in range(int(input())):
	n = int(input())
	ans = 0
	while n % 6 == 0:
		n /= 6
		ans += 1
	while n % 3 == 0:
		ans += 2
		n /= 3
	if n == 1:
		print(ans)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	num = 0
	ans = 0
	while n % 3 == 0:
		ans += 2
		num += 1
		n //= 3
	while n % 2 == 0:
		num -= 1
		ans -= 1
		n //= 2
	n += num < 0
	if n > 1:
		print(-1)
	else:
		print(ans)


from math import gcd
t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0 and n % 2 != 0:
		print(-1)
	else:
		c = 0
		while n != 1:
			if n % 6 == 0:
				n = n // 6
				c += 1
			else:
				n = n * 2
				c += 1
			if n != 1 and gcd(n, 3) != 3:
				c = -1
				break
		print(c)


t = int(input())
for i in range(t):
	n = int(input())
	ans = 0
	while n != 1:
		if n % 3 != 0:
			ans = -1
			break
		if n % 2 != 0:
			n *= 2
			ans += 1
			continue
		elif n % 2 == 0 and n % 3 == 0:
			n //= 6
			ans += 1
	print(ans)


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	if n == 1:
		ans.append(0)
		continue
	n1 = n
	n2 = n
	d2 = 0
	d3 = 0
	while n1 % 2 == 0:
		d2 += 1
		n1 //= 2
	while n2 % 3 == 0:
		d3 += 1
		n2 //= 3
	if 2 ** d2 * 3 ** d3 != n:
		ans.append(-1)
		continue
	if d3 < d2:
		ans.append(-1)
	if d3 == d2:
		ans.append(d2)
	if d3 > d2:
		te = d3 - d2
		ans.append(d3 + te)
for an in ans:
	print(an)


a = int(input())
for i in range(a):
	temp = int(input())
	original = temp
	counter = 0
	p = 0
	while temp != 1 and temp % 3 == 0:
		p = 1
		counter += 1
		if temp % 6 == 0:
			temp /= 6
		else:
			temp *= 2
	if p == 1 and temp == 1:
		print(counter)
	elif temp == 1:
		print(0)
	else:
		print(-1)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	x = n & ~(n - 1)
	y = n // x
	b = 1162261467 % y
	if b != 0:
		print(-1)
	else:
		z2 = math.ceil(math.log(x) / math.log(2))
		z3 = math.ceil(math.log(y) / math.log(3))
		if z2 > z3:
			print(-1)
		else:
			print(2 * z3 - z2)


import math
for t in range(int(input())):
	n = int(input())
	s = 0
	tr = 0
	while n != 1 and n % 6 == 0:
		n = n // 6
		s = s + 1
	while n != 1 and n % 3 == 0:
		n = n // 3
		tr = tr + 1
	if n != 1:
		print(-1)
	else:
		print(2 * tr + s)


for x in range(int(input())):
	n = int(input())
	k = 0
	while n % 3 == 0:
		n /= 3
		k += 1
	b = 0
	while n % 2 == 0:
		n /= 2
		b += 1
	if n > 1:
		print(-1)
	elif n == 1:
		if b > k:
			print(-1)
		else:
			print(2 * k - b)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	a = int(input())
	counter = 0
	while a % 6 == 0:
		a = a // 6
		counter += 1
	while a % 3 == 0:
		a = a // 3
		counter += 2
	if a == 1:
		print(counter)
	else:
		print(-1)


for i in range(int(input())):
	p = int(input())
	c = 0
	while p != 1 and p == int(p):
		if p % 6 == 0:
			c += 1
			p /= 6
		elif p % 3 == 0:
			c += 2
			p /= 3
		else:
			break
	if p == 1:
		print(c)
	else:
		print(-1)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def fi():
	return float(input())

def si():
	return input()

def msi():
	return map(str, input().split())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
mod = 1000000007
from math import log, log2, sqrt, factorial, cos, tan, sin, radians, ceil, floor
import bisect
from decimal import *
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def main():
	for _ in range(ii()):
		n = ii()
		if n == 1:
			print(0)
			continue
		a = b = 0
		while n > 1:
			if n % 2 == 0:
				a += 1
				n //= 2
			elif n % 3 == 0:
				b += 1
				n //= 3
			else:
				break
		if n > 1:
			print(-1)
			continue
		if a > b:
			print(-1)
			continue
		ans = a + (b - a) * 2
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from math import log

def solve():
	n = int(input())
	st3 = 0
	while n % 3 == 0:
		st3 += 1
		n /= 3
	st2 = int(log(n, 2))
	if 2 ** st2 != n or st2 > st3:
		print(-1)
	else:
		print(st3 - st2 + st3)
t = int(input())
for _ in range(t):
	solve()


import sys
import math
input = sys.stdin.readline
t = int(input())
for t1 in range(t):
	n = int(input())
	a = 0
	b = 0
	if n == 1:
		print(0)
	else:
		while n % 2 == 0:
			n = n // 2
			a += 1
		while n % 3 == 0:
			n = n // 3
			b += 1
		if n > 1 or b == 0 or a > b:
			print(-1)
		elif a == 0:
			print((b - 1) * 2 + 2)
		else:
			z = a - 1
			b -= z
			a = 1
			print((b - 1) * 2 + z + 1)


import collections as cc
import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	c = 0
	fl = 0
	while n != 1:
		if n % 6 == 0:
			c += 1
			n //= 6
		else:
			c += 1
			n *= 2
		if n > 2 * 10 ** 9:
			fl = 1
			break
	if fl:
		print(-1)
	else:
		print(c)


import math
import datetime
import collections
import statistics
import itertools

def is_prime(num):
	for i in range(2, int(math.sqrt(num)) + 1):
		if num % i == 0:
			return False
	return True

def input_list():
	ll = list(map(int, input().split(' ')))
	return ll
tc = int(input())
for _ in range(tc):
	n = int(input())
	if n == 1:
		print(0)
	elif n == 2:
		print(-1)
	else:
		c = 0
		x = 0
		while n > 1 and x < 6:
			if n % 6 == 0:
				n = n // 6
				x = 0
			else:
				n = n * 2
				x += 1
			c += 1
		if n == 1:
			print(c)
		else:
			print(-1)


t = int(input())
l = []
for i in range(t):
	x = int(input())
	l.append(x)
r = []
for i in range(t):
	n = l[i]
	count = 0
	while n != 1:
		if n > 1 and n % 6 == 0:
			n = n / 6
			count += 1
		else:
			n = n * 2
			if n % 6 == 0:
				n = n / 6
				count += 2
			else:
				count = -1
				break
	r.append(count)
for i in r:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		count = 0
		while n % 6 == 0:
			n = n // 6
			count += 1
		while n % 3 == 0:
			n = n * 2
			n = n // 6
			count += 2
		if n == 1:
			print(count)
		elif n == 3:
			prinnt(count + 2)
		elif n % 3 != 0:
			print(-1)


for _ in range(int(input())):
	y = int(input())
	if y == 1:
		print(0)
	elif y == 6:
		print(1)
	elif y == 3:
		print(2)
	elif y == 9:
		print(4)
	else:
		sum = 0
		if y % 9 == 0:
			while y != 1:
				if y % 6 == 0:
					y = y // 6
					sum += 1
				elif y % 2 == 0 or y % 5 == 0:
					sum = -1
					break
				else:
					y = y * 2
					sum += 1
			print(sum)
		else:
			print(-1)


def get_arry():
	return list(map(int, input().split()))

def get_int():
	return int(input())

def get_input():
	return input()

def areParanthesisBalanced(expr):
	stack = []
	for char in expr:
		if char in ['(', '{', '[']:
			stack.append(char)
		else:
			if not stack:
				return False
			current_char = stack.pop()
			if current_char == '(':
				if char != ')':
					return False
			if current_char == '{':
				if char != '}':
					return False
			if current_char == '[':
				if char != ']':
					return False
	if stack:
		return False
	return True

def main():
	for _ in range(int(input())):
		n = get_int()
		cnt = 0
		flag = 0
		if n == 1:
			print(0)
		else:
			while n != 1:
				if n % 6 == 0:
					n = n // 6
				elif n * 2 % 6 != 0:
					print(-1)
					flag = 1
					break
				else:
					n = n * 2
				cnt += 1
			if flag == 0 and cnt != 0:
				print(cnt)
			elif flag == 0 and cnt == 0:
				print(-1)
main()


n = int(input())

def judge(m):
	c = 0
	while m > 1 and m % 3 == 0:
		if m % 6 != 0:
			m = m * 2
			c = c + 1
		else:
			m = int(m / 6)
			c = c + 1
	if m == 1:
		return c
	else:
		return -1
for i in range(0, n):
	t = int(input())
	print(judge(t))


T = int(input().rstrip())
for tt in range(T):
	n = int(input().rstrip())
	cnt2 = 0
	cnt3 = 0
	while n % 2 == 0:
		cnt2 += 1
		n //= 2
	while n % 3 == 0:
		cnt3 += 1
		n //= 3
	if n != 1:
		print(-1)
		continue
	cnt = 0
	if cnt3 >= cnt2:
		cnt += cnt3 - cnt2
		cnt += cnt3
		print(cnt)
		continue
	print(-1)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		if n == 1:
			print(0)
		else:
			res = 0
			while n > 1:
				if n % 6 == 0:
					n //= 6
					res += 1
				elif n % 3 == 0:
					n //= 3
					res += 2
				else:
					break
			if n == 1:
				print(res)
			else:
				print(-1)
main()


from sys import stdin

def readline():
	return stdin.readline()
tests = int(readline())

def solve(n):
	if n == 1:
		return 0
	ans = n
	i = 0
	while ans > 1:
		if ans % 3 != 0:
			return -1
		if ans % 6 != 0:
			ans *= 2
		else:
			ans /= 6
		i += 1
	return i
for t in range(0, tests):
	n = int(readline().rstrip('\n'))
	print(solve(n))


q = int(input())
for _ in range(q):
	n = int(input())
	num2 = num3 = 0
	can = True
	if n == 1:
		print(0)
		continue
	while n % 2 == 0:
		num2 += 1
		n //= 2
	while n % 3 == 0:
		num3 += 1
		n //= 3
	if n != 1:
		can = False
	if num3 < num2:
		can = False
	res = num3 + (num3 - num2)
	if can:
		print(res)
	else:
		print(-1)


t = int(input())
while t:
	t = t - 1
	n = int(input())
	f1 = True
	ans = 0
	while n > 1:
		if n % 6 == 0:
			n = n / 6
			ans = ans + 1
		elif n % 3 == 0:
			n = n / 3
			ans = ans + 2
		else:
			f1 = False
			break
	if f1 == True:
		print(ans)
	else:
		print(-1)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	twos = 0
	threes = 0
	n = int(input())
	while n % 3 == 0:
		n //= 3
		threes += 1
	while n % 2 == 0:
		n //= 2
		twos += 1
	if n != 1:
		print(-1)
		continue
	if twos > threes:
		print(-1)
		continue
	print(threes + threes - twos)


def Hello(n):
	c = 0
	while True:
		if n == 1:
			return c
		elif n % 6 == 0:
			n = n // 6
			c += 1
		elif n % 3 == 0:
			n = n * 2
			c += 1
		else:
			return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(Hello(n))


def devide(num, steps):
	if num == 1:
		return steps
	elif num % 6 == 0:
		steps += 1
		num //= 6
		return devide(num, steps)
	elif num % 6 == 3:
		steps += 1
		num *= 2
		return devide(num, steps)
	else:
		return -1
for i in range(int(input())):
	print(devide(int(input()), 0))


import sys

def getR(numb):
	moves = 0
	n = numb
	while True:
		if 1 == n:
			return moves
		if n < 3:
			return -1
		if 0 == n % 6:
			n = n / 6
			moves += 1
		elif 0 == n % 3:
			n = n / 3
			moves += 2
		else:
			return -1
data = sys.stdin.readlines()
data.pop(0)
for line in data:
	if not '' == line.rstrip():
		print(getR(int(line.rstrip())))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import math

def main():
	test = int(input())
	while test != 0:
		n = int(input())
		x = math.log(n, 10)
		y = math.log(6, 10)
		if round(x / y, 10) == int(x / y):
			print(int(x / y))
		else:
			m = n
			count3 = 0
			while m % 3 == 0:
				count3 += 1
				m = int(m // 3)
			ans = 0
			flag = 0
			while count3 != 0:
				n = n * 2
				ans += 1
				x = math.log(n, 10)
				if round(x / y, 10) == int(x / y):
					flag = 1
					break
				count3 -= 1
			if flag == 1:
				print(ans + int(x / y))
			else:
				print(-1)
		test -= 1
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	line = input().split()
	n = int(line[0])
	count = 0
	while n != 1:
		count += 1
		if n % 3 == 0:
			if n % 2 == 0:
				n = n // 6
			else:
				n = n * 2
		else:
			print('-1')
			break
	if n == 1:
		print(count)


from fractions import Fraction
import bisect
import os
import io
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
from queue import Queue

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
t = iinput()
for _ in range(t):
	n = iinput()
	if n == 1:
		print(0)
		continue
	sixs = 0
	while n > 0 and n % 6 == 0:
		n = n // 6
		sixs += 1
	threes = 0
	while n > 0 and n % 3 == 0:
		n = n // 3
		threes += 1
	if n == 1:
		print(sixs + 2 * threes)
	else:
		print(-1)


def mulTwoDivSix(num: int) -> None:
	cont = True
	count = 0
	while cont:
		if num == 1:
			cont = False
		else:
			if num % 6 == 0:
				num = num // 6
			elif (num % 1 == 0 or 2 % (num % 1) == 0) and num % 3 == 0:
				num *= 2
			else:
				cont = False
			count += 1
	if num == 1:
		print(count)
	else:
		print(-1)
	return
test_cases = int(input())
for _ in range(test_cases):
	num = int(input())
	mulTwoDivSix(num)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	cnt2 = 0
	cnt3 = 0
	while n % 2 == 0:
		n /= 2
		cnt2 += 1
	while n % 3 == 0:
		n /= 3
		cnt3 += 2
	print(cnt3 - cnt2 if n == 1 and cnt3 // 2 >= cnt2 else -1)


tests = int(input())
ans = [None] * tests
acc = 0
accAux = 0
for i in range(tests):
	x = int(input())
	acc = 0
	accAux = 0
	while x != 1 and accAux < 3:
		if x % 6 == 0:
			x = x / 6
			acc = acc + 1
			accAux = 0
		else:
			x = x * 2
			acc = acc + 1
			accAux = accAux + 1
	if x == 1:
		ans[i] = acc
	else:
		ans[i] = -1
for sol in ans:
	print(sol)


t = int(input())
l = []
for i in range(t):
	number = int(input())
	n = 0
	m = 0
	if number == 1:
		x = 1
	elif number % 3 != 0:
		x = -1
	while number % 3 == 0:
		n += 1
		number = number / 3
	while number % 2 == 0:
		m += 1
		number = number / 2
	if number != 1:
		x = -1
	elif m > n:
		x = -1
	elif m == n:
		x = m
	elif m < n:
		x = 2 * n - m
	l.append(x)
for i in l:
	print(i)


t = int(input())
import time
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		cnt = 0
		while n > 2 and n % 3 == 0:
			cnt = cnt + 1
			if n % 6 != 0:
				n = n * 2
			else:
				n = n // 6
		if n == 1:
			print(cnt)
		else:
			print(-1)


from math import *
from copy import *
from string import *
from random import *
from sys import stdin, stdout
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math
import math
import time
from queue import Queue

def seive(n):
	l = [True]
	l = l * (n + 1)
	for i in range(2, int(sqrt(n)) + 1):
		if l[i] == True:
			val = i * i
			while val < len(l):
				l[val] = False
				val += i
	prime = []
	for i in range(2, len(l)):
		if l[i] == True:
			prime.append(i)
	return prime

def dp(l, i, n, val, ans):
	if i >= n or val < 0:
		return 1000000
	elif val == 0:
		return 0
	else:
		if ans[i] == 1000000:
			ans[i] = min(min(1 + dp(l, i + 1, n, val - l[i], ans), 1 + dp(l, i, n, val - l[i], ans)), dp(l, i + 1, n, val, ans))
		return ans[i]
from queue import Queue

def factors(n):
	l = []
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def check1(s, v, x):
	while True:
		if v not in s:
			return v
		else:
			v -= 1

def check2(s, v, x):
	while True:
		if v not in s:
			return v
		else:
			v += 1

def up(n):
	j = n - 1
	while j > 0:
		if n % j == 0:
			break
		j -= 1
	return j

def check(n):
	ans = 0
	while n != 0:
		ans += n % 10
		n = n // 10
	if ans % 9 == 0:
		return True
	else:
		return False
t = int(input())
for j in range(1, t + 1):
	n = int(input())
	if n == 1:
		print(0)
	elif n == 3:
		print(2)
	elif n == 6:
		print(1)
	elif check(n):
		c = 0
		while n <= 1000000000000000000000000000000000 and n != 1:
			if n % 6 == 0:
				c += 1
				n = n // 6
			else:
				c += 1
				n = n * 2
		if n == 1:
			print(c)
		else:
			print(-1)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	k = n
	p = 0
	f = 0
	while n > 1:
		p += 1
		if n % 6 == 0:
			n = n // 6
		else:
			n = n * 2
		if p == 100:
			f = 1
			break
	if f == 0:
		print(p)
	else:
		print(-1)


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)

def mod():
	return 10 ** 9 + 7

def i():
	return sys.stdin.readline().strip()

def ii():
	return int(sys.stdin.readline())

def li():
	return list(sys.stdin.readline().strip())

def mii():
	return map(int, sys.stdin.readline().split())

def lii():
	return list(map(int, sys.stdin.readline().strip().split()))

def solve():
	a = ii()
	con2 = con3 = 0
	while a % 2 == 0:
		con2 += 1
		a //= 2
	while a % 3 == 0:
		con3 += 1
		a //= 3
	if a == 1 and con2 <= con3:
		print(2 * con3 - con2)
	else:
		print(-1)
for _ in range(ii()):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	temp1 = 0
	while n % 2 == 0:
		temp1 += 1
		n = n // 2
	temp2 = 0
	while n % 3 == 0:
		temp2 += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif temp1 > temp2:
		print(-1)
	elif temp1 == temp2:
		print(temp2)
	else:
		print(temp2 + (temp2 - temp1))


k = int(input())
res = [0] * k
for i in range(k):
	n = int(input())
	if n == 1:
		res[i] = 0
	else:
		s = 0
		while n % 6 == 0:
			n = int(n / 6)
			s += 1
		while n % 3 == 0:
			n = n / 3
			s += 2
		if n != 1:
			s = -1
		res[i] = s
for i in res:
	print(i, end='\n')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	cnt1 = 0
	cnt2 = 0
	while n % 2 == 0:
		cnt1 += 1
		n = n // 2
	while n % 3 == 0:
		cnt2 += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif cnt1 > cnt2:
		print(-1)
	else:
		print(2 * cnt2 - cnt1)


qtd = int(input())
lista = []
for i in range(qtd):
	num = int(input())
	cont = 0
	cont2 = 0
	while num > 1:
		if num % 6 == 0:
			num = num / 6
			cont2 = 0
		else:
			num = num * 2
			cont2 += 1
		cont += 1
		if cont2 > 4:
			cont = -1
			break
	lista.append(cont)
for i in range(len(lista)):
	print(lista[i])


import math
T = int(input())
for _ in range(0, T):
	n = int(input())
	cnt2 = 0
	cnt3 = 0
	if n == 1:
		print(0)
		continue
	else:
		while n % 2 == 0:
			cnt2 += 1
			n = n // 2
		while n % 3 == 0:
			cnt3 += 1
			n = n // 3
		if n != 1:
			print(-1)
			continue
		elif cnt2 > cnt3:
			print(-1)
			continue
		else:
			print(cnt2 + (cnt3 - cnt2) * 2)
			continue


T = int(input())
for i in range(T):
	n = int(input())
	ans = 0
	while n % 6 == 0:
		n /= 6
		ans += 1
	while n % 3 == 0:
		n /= 3
		ans += 2
	if n != 1:
		ans = -1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	su = 0
	se = 0
	sk = 0
	sj = 0
	count = 0
	if n == 1:
		print(0)
	else:
		while n != 1:
			if n % 6 == 0:
				count += 1
				n = n // 6
			elif n % 6 != 0:
				n *= 2
				count += 1
				if n % 6 == 0:
					count += 1
					n = n // 6
				else:
					se += 1
					print(-1)
					break
		if se != 1:
			print(count)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		fg = 0
		ct = 0
		while n != 1:
			if n % 3 == 0 and n % 2 != 0:
				n *= 2
			elif n % 6 == 0:
				n /= 6
			else:
				fg = 1
				break
			ct += 1
		if fg == 1:
			print(-1)
		else:
			print(ct)


production = True
import sys, math, collections

def input(input_format=0, multi=0):
	if multi > 0:
		return [input(input_format) for i in range(multi)]
	else:
		next_line = sys.stdin.readline()[:-1]
		if input_format >= 10:
			use_list = False
			input_format = int(str(input_format)[-1])
		else:
			use_list = True
		if input_format == 0:
			formatted_input = [next_line]
		elif input_format == 1:
			formatted_input = list(map(int, next_line.split()))
		elif input_format == 2:
			formatted_input = list(map(float, next_line.split()))
		elif input_format == 3:
			formatted_input = list(next_line)
		elif input_format == 4:
			formatted_input = (lambda x: [x[0], list(map(int, x[1:]))])(next_line.split())
		elif input_format == 5:
			formatted_input = (lambda x: [x[0], list(map(float, x[1:]))])(next_line.split())
		elif input_format == 6:
			formatted_input = next_line.split()
		else:
			formatted_input = [next_line]
		return formatted_input if use_list else formatted_input[0]

def out(output_line, output_format=0, newline=True):
	formatted_output = ''
	if output_format == 0:
		formatted_output = str(output_line)
	elif output_format == 1:
		formatted_output = ' '.join(map(str, output_line))
	elif output_format == 2:
		formatted_output = '\n'.join(map(str, output_line))
	print(formatted_output, end='\n' if newline else '')

def log(*args):
	if not production:
		print('$$$', end='')
		print(*args)
enum = enumerate

def solve():
	n = input(11)
	for i in range(100):
		if n == 1:
			out(i)
			break
		if not n % 6:
			n //= 6
		else:
			n *= 2
	else:
		out(-1)
	return
for i in range(input(11)):
	solve()


for _ in range(int(input())):
	a = int(input())
	count2 = 0
	a2 = a
	while True:
		a2 = a2 / 2
		if a2 == int(a2):
			count2 += 1
		else:
			break
	a3 = a
	count3 = 0
	while True:
		a3 = a3 / 3
		if a3 == int(a3):
			count3 += 1
		else:
			break
	if count2 > count3 or 2 ** count2 * 3 ** count3 != a:
		print(-1)
	else:
		ans = 2 * max(count2, count3) - min(count2, count3)
		print(ans)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
import datetime
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	starttime = datetime.datetime.now()
	for _ in range(int(input())):
		n = int(input())
		if n == 1:
			print(0)
		else:
			c = 0
			k = 0
			z = n
			while True:
				if z % 6 == 0:
					z = z // 6
					c += 1
				elif z * 2 % 6 == 0:
					z = z * 2
					c += 1
				elif z != 1:
					c = -1
					break
				else:
					break
			print(c)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	n = int(input())
	st = 0
	(th, tw) = (0, 0)
	while n != 1:
		if n % 2 == 0:
			tw += 1
			n //= 2
		elif n % 3 == 0:
			th += 1
			n //= 3
		else:
			print(-1)
			break
	if n == 1:
		if tw <= th:
			print(2 * th - tw)
		else:
			print(-1)


from math import *
from copy import *
from heapq import *
from string import *
from random import *
from bisect import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from decimal import Decimal
from collections import Counter
from collections import defaultdict

def solve(n):
	if n == 1:
		return 0
	ans = 0
	while n != 1:
		if n % 3 != 0:
			return -1
		elif n % 6 == 0:
			n //= 6
		else:
			n *= 2
		ans += 1
	return ans
T = int(input())
for _ in range(T):
	N = int(stdin.readline())
	print(solve(N))


for _ in range(int(input())):
	t = int(input())
	count = 0
	imp = t
	if t == 1:
		print(0)
	elif t < 1:
		print(-1)
	else:
		while t > 1:
			if t % 6 == 0:
				t /= 6
				count += 1
				if t == 1:
					print(count)
					break
			else:
				t *= 2
				count += 1
				if t >= imp * 6:
					print(-1)
					break


t = int(input())
n = []
for i in range(0, t):
	ele = int(input())
	n.append(ele)
for i in range(t):
	c = 0
	k = n[i]
	while k != 1:
		if k % 6 == 0:
			c += 1
			k = k / 6
		elif k >= 10 ** 9 or k <= 1:
			c = -1
			break
		else:
			c += 1
			k = k * 2
	print(c)


import math
for i in range(int(input())):
	k = int(input())
	n = k
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		(a, b) = (0, 0)
		while n % 3 == 0:
			a = a + 1
			n = n // 3
		b = int(math.log(n, 2))
		x = a
		y = a - b
		if a < b or k - 2 ** b * 3 ** a != 0:
			print(-1)
		else:
			print(int(2 * a - b))


import math
import sys
import itertools
import fractions
wtf = sys.stdin.read()
wtf = wtf.strip().split('\n')
t = int(wtf[0])
for n in wtf[1:]:
	ans = []
	n = int(n)
	ans = [0, 0]
	while n % 3 == 0:
		n //= 3
		ans[0] += 1
	while n % 2 == 0:
		n //= 2
		ans[1] += 1
	if n == 1 and ans[1] <= ans[0]:
		print(ans[0] * 2 - ans[1])
	else:
		print(-1)


s = 1
tr = []
while s <= 10 ** 9:
	tr.append(s)
	s *= 3
for i in range(int(input())):
	n = int(input())
	count = 0
	while not n % 6:
		n //= 6
		count += 1
	if n in tr:
		a = tr.index(n)
		print(a * 2 + count)
	else:
		print(-1)


def multi(n, s):
	if n == 1:
		return s
	elif n % 6 == 0:
		n = n // 6
		s += 1
		return multi(n, s)
	else:
		n = n * 2
		if n % 6 == 0:
			s += 1
			return multi(n, s)
		else:
			return -(s + 1)
n = int(input())
for x in range(n):
	k = multi(int(input()), 0)
	if k < 0:
		print(-1)
	else:
		print(k)


t = int(input())

def count(n):
	(count_two, count_three) = (0, 0)
	while n % 3 == 0:
		n = n // 3
		count_three += 1
	while n % 2 == 0:
		n = n // 2
		count_two += 1
	if n == 1:
		return (True, count_two, count_three)
	return (False, count_two, count_three)
for _ in range(t):
	n = int(input())
	(is_ok, c2, c3) = count(n)
	if c2 > c3 or not is_ok:
		print(-1)
	else:
		rem = c3 - c2
		print(c3 + rem)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 0:
		print(-1)
	elif n == 1:
		print(0)
	else:
		s = 0
		done = False
		while not done:
			if n % 3 != 0:
				s = -1
				done = True
			elif n % 2 == 0:
				n = n / 6
				s += 1
			else:
				n = n / 3
				s += 2
			if n == 1:
				done = True
		print(s)


for _ in range(int(input())):
	num = int(input())
	cnt = 0
	need_two = 0
	while not num % 6:
		num //= 6
		cnt += 1
	while not num % 3:
		num //= 3
		need_two += 1
	print(cnt + need_two * 2 if num == 1 else -1)


def solve(n):
	cnt = 0
	if n == 1:
		return 0
	while True:
		if n >= 6 and n % 6 == 0:
			n = n // 6
			cnt += 1
			if n == 1:
				return cnt
			elif n == 6:
				return cnt + 1
			elif n < 6:
				n *= 2
				cnt += 1
				if n % 6 == 0:
					return cnt + 1
				else:
					return -1
		else:
			n *= 2
			cnt += 1
			if n % 6 != 0:
				return -1

def main():
	t = int(input())
	for i in range(t):
		print(solve(int(input())))
main()


def solve(n):
	if n == 1:
		return 0
	res = 0
	three = 0
	two = 0
	while n % 6 == 0:
		res += 1
		n = n // 6
	if n == 1:
		return res
	while n % 3 == 0:
		three += 1
		n = n // 3
	while n % 2 == 0:
		two += 1
		n = n // 2
	if n != 1:
		return -1
	if three > two:
		k = (three - two) * 2
		return res + k
	return -1
test = int(input())
for i in range(test):
	n = int(input())
	print(solve(n))


try:
	t = int(input())
	for j in range(t):
		n = int(input())
		c = 0
		while n > 1:
			if n % 6 == 0:
				n = n // 6
				c += 1
			elif n % 3 == 0:
				n = n * 2
				c += 1
			else:
				c = -1
				break
		print(c)
except:
	pass


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []

def pow_of_k(n, k):
	count = 0
	while n % k == 0:
		n = n // k
		count += 1
	return count
for _ in range(testCases):
	n = int(input())
	(n3, n2) = (pow_of_k(n, 3), pow_of_k(n, 2))
	if n != int(3 ** n3) * int(2 ** n2):
		ans = -1
	elif n3 < n2:
		ans = -1
	else:
		ans = n3 - n2 + n3
	answers.append(ans)
print(*answers, sep='\n')


for _ in range(int(input())):
	n = int(input())
	count = 0
	ans = 0
	flag = True
	if n == 1:
		print(0)
	elif n % 3 != 0 and n != 1:
		print(-1)
	else:
		while n != 1:
			if n % 6 == 0:
				n = n // 6
				count = 0
				ans += 1
			elif n % 3 == 0:
				n = n // 3
				count += 1
				ans += 2
			elif n % 3 != 0:
				flag = False
				print(-1)
				break
		if flag:
			print(ans)


def steps(n):
	s = 0
	c = 0
	while True:
		if n == 1:
			return s
		if n % 6 == 0:
			n = n // 6
			c = 0
		else:
			n = n * 2
			c += 1
		if c == 3:
			return -1
		s += 1
step = []
for _ in range(int(input())):
	n = int(input())
	step.append(steps(n))
for i in step:
	print(i)


def one_case():
	n = int(input())
	on = n
	c2 = 0
	c3 = 0
	while n % 2 == 0:
		n = n // 2
		c2 += 1
	while n % 3 == 0:
		n = n // 3
		c3 += 1
	if n != 1 or c2 > c3:
		print(-1)
	else:
		print(c3 + (c3 - c2))
t = int(input())
for i in range(t):
	one_case()


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	three = 0
	two = 0
	while n % 3 == 0:
		three += 1
		n = n / 3
	while n % 2 == 0:
		two += 1
		n = n / 2
	if n == 1 and three >= two:
		ans.append(three * 2 - two)
	else:
		ans.append(-1)
for k in range(len(ans)):
	print(ans[k])


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	(a, b) = (0, 0)
	while n % 3 == 0:
		a += 1
		n //= 3
	while n % 2 == 0:
		b += 1
		n //= 2
	if a == b and n == 1:
		print(a)
	elif a > b and n == 1:
		print(b + (a - b) * 2)
	else:
		print(-1)


from math import log
t = int(input())
for _ in range(t):
	n = int(input())
	twos = threes = 0
	while n % 2 == 0 or n % 3 == 0:
		if n % 2 == 0:
			n = n // 2
			twos += 1
		elif n % 3 == 0:
			n = n // 3
			threes += 1
	if n != 1 or twos > threes:
		print(-1)
	else:
		print(f'{threes - twos + threes}')


t = int(input())
while t > 0:
	t = t - 1
	x = int(input())
	c = 0
	f = 0
	while x != 1:
		if x % 6 == 0:
			c += 1
			x = int(x / 6)
		elif x % 3 == 0:
			x = x * 2
			c += 1
		elif x % 3 != 0 and x != 1:
			print('-1')
			f = 1
			break
	if f == 0:
		print(c)


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	n = inp()
	ct = 0
	cnt = 0
	flag = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			ct += 1
			cnt = 0
		else:
			if cnt > 2:
				flag = 1
				break
			n *= 2
			ct += 1
			cnt += 1
	if flag:
		print(-1)
	else:
		print(ct)


T = int(input())

def getDiv(n, d):
	p = 0
	r = n % d
	while r == 0:
		r = n % d
		if n % d == 0:
			n = n / d
			p += 1
	return p
while T > 0:
	n = int(input())
	p2 = getDiv(n, 2)
	p3 = getDiv(n, 3)
	if n == 1:
		print(0)
	elif p3 == 0:
		print(-1)
	elif p2 > p3:
		print(-1)
	elif p3 >= p2 and n == 2 ** p2 * 3 ** p3:
		a = p3 - p2
		print(a + p3)
	else:
		print(-1)
	T -= 1


def main():
	t = int(input())
	nums = [int(input()) for i in range(t)]
	for n in nums:
		a = 0
		b = 0
		while n % 2 == 0:
			n //= 2
			a += 1
		while n % 3 == 0:
			n //= 3
			b += 1
		if n == 1 and a <= b:
			print(2 * b - a)
		else:
			print(-1)
main()


for i in range(int(input())):
	a = int(input())
	count1 = 0
	count2 = 0
	while a >= 2:
		if a % 3 == 0:
			a = a / 3
			count1 = count1 + 1
		elif a % 2 == 0:
			a = a / 2
			count2 = count2 + 1
		else:
			break
	if a > 2:
		print(-1)
	elif count1 >= count2:
		c = count1 - count2
		c += count1
		print(c)
	else:
		print(-1)


T = int(input())
while T > 0:
	T -= 1
	x = int(input())
	s1 = 0
	s2 = 0
	while x % 3 == 0:
		x //= 3
		s1 += 1
	while x % 2 == 0:
		x //= 2
		s2 += 1
	if x != 1 or s2 > s1:
		print(-1)
	else:
		print(2 * s1 - s2)


import math
tests = int(input())
while tests > 0:
	n = int(input())
	(m, x, ans) = (n, 0, 0)
	while m > 0:
		if m % 3 == 0:
			x += 1
			m = m // 3
		else:
			break
	y = math.log(int(n / 3 ** x), 2)
	if y != int(y) or y > x:
		ans = -1
	else:
		ans = 2 * int(x) - int(y)
	print(ans)
	tests -= 1


t = int(input())
for i in range(0, t):
	n = int(input())
	c = 0
	steps = 0
	while n != 1:
		while n != 1 and n % 6 == 0:
			n /= 6
			steps += 1
			c = 0
		while n != 1 and n % 6 != 0:
			n *= 2
			steps += 1
			if c == 1:
				c += 1
				break
			c += 1
		if c == 2:
			print('-1')
			break
	if n == 1:
		print(steps)


t = int(input())
for _ in range(t):
	n = int(input())
	div = [0, 0]
	while n % 2 == 0:
		n //= 2
		div[0] += 1
	while n % 3 == 0:
		n //= 3
		div[1] += 1
	if n != 1:
		print(-1)
		continue
	print(div[1] - div[0] + div[1] if div[1] >= div[0] else -1)


t = int(input())
for i in range(t):
	count3 = 0
	count2 = 0
	a = int(input())
	if a == 1:
		print(0)
	elif a % 3 != 0 or a == 0:
		print(-1)
	else:
		while a % 3 == 0:
			count3 += 1
			a = a // 3
		while a % 2 == 0:
			count2 += 1
			a = a // 2
		if a == 1:
			if count2 > count3:
				print(-1)
			else:
				res = count3 - count2 + count3
				print(res)
		else:
			print(-1)


for _ in range(int(input())):
	n = int(input())
	ans = 0
	fla = 0
	while n > 1:
		if n % 6 == 0:
			ans += 1
			n = n // 6
		else:
			n = n * 2
			if n % 6 == 0:
				ans += 2
				n = n // 6
			else:
				fla = 1
				break
	if fla == 1:
		print('-1')
	else:
		print(ans)


from sys import stdout
import math
t = int(input())
for _ in range(t):
	n = int(input())
	z = n
	k = 0
	while z % 2 == 0:
		k += 1
		z //= 2
	kk = 0
	while z % 3 == 0:
		kk += 1
		z //= 3
	if kk < k or z != 1:
		print(-1)
	elif kk == k:
		print(kk)
	else:
		ans = kk - k
		print(ans + kk)


def b(n, ans):
	if n == 1:
		return ans
	if n % 3 != 0 and n % 6 != 0:
		return -1
	if n % 6 == 0:
		return b(n // 6, ans + 1)
	if n % 6 != 0 and n % 3 == 0:
		return b(n * 2, ans + 1)
t = int(input())
for _ in range(t):
	n = int(input())
	print(b(n, 0))


num = int(input())
for i in range(num):
	n = int(input())
	c1 = 0
	c2 = 0
	while n % 3 == 0:
		n = n / 3
		c1 += 1
	while n % 2 == 0:
		n = n / 2
		c2 += 1
	if n != 1:
		print('-1')
	elif c1 >= 0 and c1 - c2 >= 0:
		print(2 * c1 - c2)
	else:
		print('-1')


import math

def primeFactors(n):
	count2 = 0
	count = 0
	while n % 2 == 0:
		n = n / 2
		count2 += 1
	count3 = 0
	while n % 3 == 0:
		n = n / 3
		count3 += 1
	if n > 2:
		count = -1
	elif count2 > count3:
		count = -1
	else:
		count = 2 * count3 - count2
	return count
for _ in range(int(input())):
	n = int(input())
	counter = primeFactors(n)
	print(counter)


def go(num):
	s = 0
	while num != 1:
		if num <= 0 or s > 60:
			return -1
		if num % 6 == 0:
			num = num / 6
		else:
			num = num * 2
		s += 1
	return s
t = int(input())
for i in range(t):
	num = int(input())
	print(go(num))


t = int(input())
while t:
	t -= 1
	n = int(input())
	c = 0
	flag = 0
	while n > 1:
		if n & n - 1 == 0 or (n % 2 == 0 and n % 6 != 0):
			flag = 1
			break
		elif n % 6 == 0:
			n //= 6
			c += 1
		else:
			n *= 2
			c += 1
	if flag == 0:
		print(c)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	cnt = 0
	cnt1 = 0
	if n == 1:
		print(0)
	else:
		while n % 2 == 0:
			n = n // 2
			cnt += 1
		while n % 3 == 0:
			n = n // 3
			cnt1 += 1
		if n == 1 and cnt <= cnt1:
			print(2 * cnt1 - cnt)
		else:
			print(-1)


def pow_2(num):
	count = 0
	while num % 2 == 0:
		count += 1
		num = int(num / 2)
	return count

def pow_3(num):
	count = 0
	while num % 3 == 0:
		count += 1
		num = int(num / 3)
	return count
n = int(input())
for i in range(n):
	k = int(input())
	two = pow_2(k)
	three = pow_3(k)
	if 2 ** two * 3 ** three == k:
		if two <= three:
			print(three + three - two)
		else:
			print(-1)
	else:
		print(-1)


import math
from sys import stdin
from collections import Counter, defaultdict, deque
input = stdin.readline
mod = pow(10, 9) + 7

def solve():
	n = int(input())
	c1 = 0
	c2 = 0
	while 1:
		flag = 0
		if n % 3 == 0:
			c2 += 1
			n = n // 3
			flag = 1
		if n % 2 == 0:
			c1 += 1
			n = n // 2
			flag = 1
		if flag == 0:
			break
	if c1 > c2 or n > 1:
		print(-1)
	elif c1 == c2:
		print(c1)
	else:
		x = c2 - c1
		print(c1 + 2 * x)
for _ in range(int(input())):
	solve()


fridge_numbers = [int(input()) for i in range(int(input()))]
fridge_counter = list()
for i in range(len(fridge_numbers)):
	counter_error = 0
	counter = 0
	while fridge_numbers[i] != 1 and counter_error != 100:
		counter_error += 1
		counter += 1
		if fridge_numbers[i] % 6 == 0:
			fridge_numbers[i] = fridge_numbers[i] // 6
		else:
			fridge_numbers[i] = fridge_numbers[i] * 2
	if counter_error == 100:
		fridge_counter.append(-1)
	else:
		fridge_counter.append(counter)
print(*fridge_counter, sep='\n')


t = int(input())
for i in range(t):
	k = int(input())
	if k == 1:
		print('0')
	elif k % 3 != 0:
		print('-1')
	else:
		counter = 0
		while k % 3 == 0 and k > 1:
			if k % 6 == 0:
				k /= 6
				counter += 1
			else:
				k *= 2
				counter += 1
		if k == 1:
			print(counter)
		else:
			print('-1')


from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	if n == 1:
		stdout.write('0' + '\n')
		continue
	elif n == 3:
		stdout.write('2' + '\n')
		continue
	elif n < 6:
		stdout.write('-1' + '\n')
		continue
	c = 0
	f = 0
	while n >= 6 or n == 3:
		if n % 6 == 0:
			n = int(n / 6)
			c += 1
		elif n % 3 == 0:
			n = int(n * 2 / 6)
			c += 2
		else:
			f = 1
			break
	if f == 1:
		stdout.write('-1' + '\n')
	elif n == 1:
		stdout.write(str(c) + '\n')
	else:
		stdout.write('-1' + '\n')


import math
t = int(input())
while t > 0:
	n = int(input())
	m = n
	x = 0
	p = 0
	while m > 1:
		if m % 3 == 0:
			x += 1
			m = m // 3
		else:
			break
	y = math.log(int(n / 3 ** x), 2)
	if y != int(y) or y > x:
		p = -1
	else:
		p = 2 * int(x) - int(y)
	print(p)
	t -= 1


import sys
import math
import heapq
from collections import defaultdict, deque
input = sys.stdin.readline

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	n = r()
	m = n
	twos = 0
	while m % 2 == 0:
		twos += 1
		m //= 2
	threes = 0
	while m % 3 == 0:
		threes += 1
		m //= 3
	if m != 1 or twos > threes:
		print(-1)
	else:
		cnt = twos
		threes -= twos
		cnt += 2 * threes
		print(cnt)


import sys, math
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	t = 0
	while n != 1 and n % 3 == 0:
		while n % 6 == 0:
			n //= 6
			t += 1
		if n != 1 and n % 3 == 0:
			n *= 2
			t += 1
	if n == 1:
		print(t)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	a = 1
	rs = 0
	while True:
		if a < n:
			a *= 6
			rs += 1
		elif a == n:
			print(rs)
			break
		elif a % 2 == 0:
			a /= 2
			rs += 1
		else:
			print(-1)
			break


for _ in range(int(input())):
	m = int(input())
	twos = 0
	threes = 0
	while m > 1 and m % 2 == 0:
		m = m // 2
		twos += 1
	while m > 1 and m % 3 == 0:
		m = m // 3
		threes += 1
	if m != 1:
		print(-1)
	elif twos > threes:
		print(-1)
	else:
		print(threes + abs(threes - twos))


n = int(input())
for i in range(n):
	count2 = 0
	count3 = 0
	count_total = 0
	x = int(input())
	if x == 1:
		print('0')
	elif x % 3 == 0:
		while x % 3 == 0:
			count3 += 1
			x = x / 3
		while x % 2 == 0:
			count2 += 1
			x = x / 2
		if x == 1 and count2 <= count3:
			count_total = count3 * 2 - count2
			print(count_total)
		else:
			print('-1')
	else:
		print('-1')


t = int(input())
for _ in range(t):
	n = int(input())
	N = n
	cnt_2 = 0
	cnt_3 = 0
	while True:
		if n % 2 == 0:
			n //= 2
			cnt_2 += 1
		else:
			break
	while True:
		if n % 3 == 0:
			n //= 3
			cnt_3 += 1
		else:
			break
	if N == 1:
		res = 0
	elif cnt_2 > cnt_3:
		res = -1
	else:
		k = 6 ** cnt_3 // 2 ** (cnt_3 - cnt_2)
		if k == N:
			res = cnt_3 + (cnt_3 - cnt_2)
		else:
			res = -1
	print(res)


n = int(input())
temp = []
for i in range(n):
	temp.append(int(input()))
for i in range(n):
	a = temp[i]
	p = 0
	q = 0
	while a % 2 == 0:
		p = p + 1
		a = a / 2
	while a % 3 == 0:
		q = q + 1
		a = a / 3
	if p <= q and a == 1:
		print(q * 2 - p)
	else:
		print(-1)


import collections
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	(two, thr) = (0, 0)
	while n % 2 == 0:
		two += 1
		n //= 2
	while n % 3 == 0:
		thr += 1
		n //= 3
	if n == 1:
		if two <= thr:
			print(thr * 2 - two)
		else:
			print(-1)
	else:
		print(-1)


def count_three(t):
	count = 0
	while t % 3 == 0:
		t = t / 3
		count += 1
	return count

def count_two(t):
	count = 0
	while t % 2 == 0:
		t = t / 2
		count += 1
	return count
num = int(input())
res = 0
for i in range(0, num):
	t = int(input())
	if t == 1:
		res = 0
	elif t % 3 != 0:
		res = -1
	else:
		ct3 = count_three(t)
		ct2 = count_two(t)
		if ct2 > ct3:
			res = -1
		else:
			n = 2 * ct3 - ct2
			if t * pow(2, ct3 - ct2) == pow(6, ct3):
				res = n
			else:
				res = -1
	print(res)


f = int(input())
l = []
for _ in range(f):
	n = int(input())
	count = 0
	run = True
	if n == 1:
		l.append(count)
	else:
		while run:
			if n % 6 == 0:
				count += 1
				n /= 6
			else:
				count += 1
				n *= 2
			if n == 1:
				l.append(count)
				run = False
			if count > 100:
				l.append(-1)
				run = False
for i in l:
	print(i)


import math
from collections import *
import sys
from functools import reduce

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def getlistofstring():
	return list(input().strip().split())

def printxsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
UGLYMOD = int(1000000000.0) + 7
SEXYMOD = 998244353
MAXN = int(100000.0)
for _testcases_ in range(int(input())):
	n = int(input())
	ans = 0
	start = 1
	while start != n:
		if start > n:
			start //= 2
		else:
			start *= 6
		ans += 1
		if start & 1:
			break
	if start != n:
		ans = -1
	print(ans)


def main():
	t = int(input())
	x = []
	c = []
	temp = int(0)
	for i in range(t):
		c.append(0)
	for i in range(t):
		x.append(int(input()))
		if x[temp] == 0:
			print(c[i])
		else:
			while x[temp] != 1 and c[i] != -1:
				if x[temp] % 6 == 0:
					x[temp] = x[temp] // 6
					c[i] += 1
				elif x[temp] * 2 % 6 == 0:
					x[temp] = x[temp] * 2
					c[i] += 1
					x[temp] = x[temp] // 6
					c[i] += 1
				else:
					c[i] = -1
		temp = temp + 1
	for i in range(t):
		print(c[i])
main()


t = int(input())
nums = []
for i in range(t):
	nums.append(int(input()))
output = []
for num in nums:
	counter_3 = 0
	while num % 3 == 0:
		num = num // 3
		counter_3 += 1
	counter_2 = 0
	while num % 2 == 0:
		num = num // 2
		counter_2 += 1
	if num == 1 and counter_2 <= counter_3:
		output.append(counter_3 + counter_3 - counter_2)
	else:
		output.append(-1)
for a in output:
	print(a)


from math import log, log2

def func(a):
	if a == 1:
		return 0
	div2 = a & -a
	div3 = a // div2
	pow2 = round(log2(div2))
	pow3 = round(log(div3, 3))
	if 3 ** pow3 != div3:
		return -1
	if pow2 > pow3:
		return -1
	if pow2 == pow3:
		return pow2
	return pow2 + (pow3 - pow2) * 2
t = int(input())
for _ in range(t):
	a = int(input())
	print(func(a))


from collections import Counter, defaultdict
BS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def to_base(s, b):
	res = ''
	while s:
		res += BS[s % b]
		s //= b
	return res[::-1] or '0'
alpha = 'abcdefghijklmnopqrstuvwxyz'
from math import floor, ceil, pi
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919]

def primef(n, plst=[]):
	if n == 1:
		return plst
	else:
		for m in primes:
			if n % m == 0:
				return primef(n // m, plst + [m])
		return primef(1, plst + [n])

def lmii():
	return list(map(int, input().split()))

def ii():
	return int(input())

def countOverlapping(string, sub):
	count = start = 0
	while True:
		start = string.find(sub, start) + 1
		if start > 0:
			count += 1
		else:
			return count
t = ii()
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		pr = primef(n)
		c = Counter(pr)
		if c[2] > c[3]:
			print(-1)
		elif c[3] == 0 or len(c) > 2:
			print(-1)
		elif c[3] > 0 and (c[2] > 0 or len(c) == 1):
			print(c[3] + c[3] - c[2])
		else:
			print(-1)


def main():
	t = int(input())
	while t:
		t = t - 1
		n = int(input())
		c = 0
		while n != 1:
			if n % 6:
				if n % 3:
					c = -1
					break
				else:
					c = c + 2
					n = n / 3
			else:
				c = c + 1
				n = n / 6
		print(c)
main()


def movesrequired(n):
	if n == 1:
		return 0
	x = n
	y = n
	count3 = 0
	while x > 2:
		if x % 3 == 0:
			count3 += 1
			x = x // 3
		else:
			break
	count2 = 0
	while y > 1:
		if y % 2 == 0:
			count2 += 1
			y = y // 2
		else:
			break
	m = 2 ** count2 * 3 ** count3
	if n // m != 1:
		return -1
	if count2 > count3:
		return -1
	elif count3 > count2:
		return 2 * count3 - count2
	elif count3 == count3 and count3 != 0:
		return count3
t = int(input())
for _ in range(t):
	n = int(input())
	print(movesrequired(n))


n = int(input())
query = []
for i in range(n):
	query.append(int(input()))
for i in query:
	if i == 1:
		print(0)
		continue
	if i == 3:
		print(2)
		continue
	if i < 6:
		print(-1)
		continue
	elif i % 3 != 0:
		print(-1)
		continue
	else:
		count = 0
		while i >= 6 and i % 3 == 0:
			if i % 6 == 0:
				i //= 6
				count += 1
			else:
				i *= 2
				count += 1
		if i == 1:
			print(count)
			continue
		if i == 3:
			print(count + 2)
			continue
		else:
			print(-1)
			continue


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	TWO = 0
	THREE = 0
	while n % 2 == 0:
		TWO += 1
		n //= 2
	while n % 3 == 0:
		THREE += 1
		n //= 3
	if n != 1:
		print(-1)
	elif TWO > THREE:
		print(-1)
	else:
		print(THREE - TWO + THREE)


def counter(n):
	count = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			count += 1
		elif n % 6 == 3:
			n = n * 2
			count += 1
		else:
			return -1
	return count
res = []
m = int(input())
for i in range(m):
	res.append(counter(int(input())))
for v in res:
	print(v)


outputs = []
for __ in range(int(input())):
	n = int(input())
	if n == 1:
		outputs.append(0)
	else:
		d = {}
		i = 2
		req = True
		while i * i <= n:
			if i != 2 and i != 3:
				req = False
				break
			while n % i == 0:
				d[i] = d.get(i, 0) + 1
				n = n // i
			i += 1
		if n == 1:
			pass
		elif n == 2:
			d[2] = d.get(2, 0) + 1
		elif n == 3:
			d[3] = d.get(3, 0) + 1
		else:
			req = False
		if req and d.get(2, 0) <= d.get(3, 0):
			outputs.append(d.get(3, 0) + (d.get(3, 0) - d.get(2, 0)))
		else:
			outputs.append(-1)
for output in outputs:
	print(output)


import math
for i in range(int(input())):
	(x, counter) = (int(input()), 0)
	while x > 1:
		if x / 6 == x // 6:
			counter += 1
			x /= 6
		else:
			x *= 2
			x /= 6
			counter += 2
	print(counter if x == 1 else -1)


for i in range(int(input())):
	c = 0
	m = int(input())
	while m != 1:
		if m % 6 == 0:
			m = m // 6
			c += 1
		else:
			m = m * 2
			c += 1
			if m % 6 != 0:
				c = -1
				break
	print(c)


T = int(input())
while T:
	n = int(input())
	c = 0
	while n > 0 and n % 6 == 0:
		c = c + 1
		n = n // 6
	while n > 0 and n % 3 == 0:
		c = c + 2
		n = n // 3
	if n == 1:
		print(c)
	else:
		print(-1)
	T = T - 1


def f():
	n = int(input())
	k2 = 0
	k3 = 0
	while n % 2 == 0:
		n //= 2
		k2 += 1
	while n % 3 == 0:
		n //= 3
		k3 += 1
	if k2 > k3 or n != 1:
		print(-1)
	else:
		print(k3 * 2 - k2)
t = int(input())
for i in range(t):
	f()


def f(n):
	col_3 = 0
	d = 2
	col_2 = 0
	while n % d == 0:
		col_2 += 1
		n //= d
	d = 3
	while n % d == 0:
		col_3 += 1
		n //= d
	if n == 1:
		if col_2 > col_3:
			print(-1)
		else:
			print(col_3 - col_2 + col_3)
	else:
		print(-1)
t = int(input())
for i in range(t):
	n = int(input())
	f(n)


t = int(input())
a = []
for i in range(t):
	a.append([])
	a[i] = [int(a[i]) for a[i] in input().split()]
res = []

def po(N):
	n = N
	ecnt = 0
	ccnt = 0
	if n == 1:
		return [0, 0]
	else:
		while n % 3 == 0:
			n = n / 3
			ecnt = ecnt + 1
		n = N
		while n % 2 == 0:
			n = n / 2
			ccnt = ccnt + 1
		return [ccnt, ecnt]
for i in range(t):
	n = a[i][0]
	if n == 1:
		res.append(0)
	elif n % 3 != 0:
		res.append(-1)
	else:
		p = []
		p = po(n)
		if n != 2 ** p[0] * 3 ** p[1]:
			res.append(-1)
		elif p[0] > p[1]:
			res.append(-1)
		else:
			res.append(p[1] + (p[1] - p[0]))
for i in range(t):
	print(res[i])


n = int(input())
arr = []

def solver(k):
	i = 0
	j = 0
	if k == 0:
		return 0
	while True:
		if k % 3 == 0:
			k = k // 3
			i += 1
		elif k % 2 == 0:
			j += 1
			k = k // 2
		else:
			break
	if k > 1:
		return -1
	if i < j:
		return -1
	if i >= j:
		return 2 * i - j
for i in range(n):
	arr.append(solver(int(input())))
for i in arr:
	print(i)


import math
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		i = 2
		count = 0
		while n % 2 == 0:
			n = int(n / 2)
			count += 1
		i = 3
		count3 = 0
		while n % 3 == 0:
			n = int(n / 3)
			count3 += 1
		if n != 1 or count3 < count:
			print(-1)
		else:
			print(2 * count3 - count)


def primeFactors(n):
	a = 0
	b = 0
	while n % 2 == 0:
		n = n // 2
		a += 1
	while n % 3 == 0:
		n = n // 3
		b += 1
	if n != 1:
		return -1
	elif a > b:
		return -1
	elif a == b:
		return a
	else:
		return 2 * b - a
import math

def main():
	t = int(input())
	for i in range(t):
		solve()

def solve():
	n = int(input())
	if n == 1:
		print(0)
		return
	print(primeFactors(n))
main()


for x in range(int(input())):
	liczba = int(input())
	i_ruch = 0
	if liczba % 3 > 0 and liczba != 1:
		print(-1)
		continue
	while liczba != 1:
		if liczba % 6 == 0:
			liczba //= 6
			if liczba % 3 > 0 and liczba != 1:
				print(-1)
				break
		else:
			liczba *= 2
		i_ruch += 1
	else:
		print(i_ruch)


def f(n):
	count_3 = 0
	count_2 = 0
	while True:
		if n % 3 == 0:
			n /= 3
			count_3 += 1
		else:
			break
	while True:
		if n % 2 == 0:
			n /= 2
			count_2 += 1
		else:
			break
	if n != 1:
		print(-1)
	elif count_2 > count_3:
		print(-1)
	else:
		print(abs(count_2 - count_3) + max(count_2, count_3))
x = int(input())
for i in range(x):
	n = int(input())
	f(n)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, sqrt, trunc, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var, end='\n'):
	sys.stdout.write(str(var) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	if n == 1:
		out(0)
		continue
	if n % 3 != 0:
		out(-1)
		continue
	(two, three) = (0, 0)
	while n % 2 == 0:
		two += 1
		n //= 2
	while n % 3 == 0:
		three += 1
		n //= 3
	if n > 1 or two > three:
		out(-1)
		continue
	out(2 * three - two)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
from collections import Counter
from bisect import bisect_left, bisect_right
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007

def primeFactors(n):
	d = {}
	while n % 2 == 0:
		d.setdefault(2, [0])[0] += 1
		n = n // 2
	while n % 3 == 0:
		d.setdefault(3, [0])[0] += 1
		n = n // 3
	d.setdefault(n, [1])
	return d
tc = ii1()
for _ in range(tc):
	n = ii1()
	if n == 1:
		print(0)
	else:
		fact = primeFactors(n)
		for i in fact:
			if i == 2 or i == 3 or i == 1:
				continue
			else:
				print(-1)
				break
		else:
			res = 0
			two = fact.get(2, [0])[0]
			three = fact.get(3, [0])[0]
			op = min(two, three)
			two -= op
			three -= op
			if two > three:
				print(-1)
			else:
				res += op
				res += three * 2
				print(res)


for u in range(int(input())):
	n = int(input())
	c = 0
	s = 0
	while n % 2 == 0:
		n = n // 2
		c = c + 1
	while n % 3 == 0:
		n //= 3
		s += 1
	if n > 1:
		print(-1)
	elif c > s:
		print(-1)
	else:
		print(2 * s - c)


import sys
import math
from collections import defaultdict

def solve(test):
	n = int(input())
	(three, two) = (0, 0)
	while n % 3 == 0:
		n //= 3
		three += 1
	while n % 2 == 0:
		n //= 2
		two += 1
	if n > 1:
		print(-1)
	elif two <= three:
		print(2 * three - two)
	else:
		print(-1)
test_cases = int(input())
for t in range(1, test_cases + 1):
	solve(t)


tests = int(input())
for test in range(tests):
	n = int(input())
	(k2, k3) = (0, 0)
	while n % 2 == 0:
		k2 += 1
		n //= 2
	while n % 3 == 0:
		k3 += 1
		n //= 3
	if n != 1 or k2 > k3:
		print(-1)
	else:
		print(2 * k3 - k2)


def check(x):
	t = 0
	while x != 1:
		if x % 6 == 0:
			t = t + 1
			x = x / 6
		else:
			x = x * 2
			t = t + 1
			if x % 6 != 0:
				return -1
	return t
t = int(input())
for i in range(t):
	y = int(input())
	t = check(y)
	print(t)


def min_steps(n):
	count = 0
	while n != 1:
		if n % 6 == 0:
			n = n / 6
			count += 1
		elif n % 3 == 0:
			n = n * 2
			count += 1
		else:
			return -1
	return count
t = int(input())
for case in range(t):
	n = int(input())
	print(min_steps(n))


t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	if n != 1:
		if n % 3 != 0:
			c = -1
		else:
			x = 0
			y = 0
			m = n
			while m % 3 == 0:
				x += 1
				m = m // 3
			m = n
			while m % 2 == 0:
				y += 1
				m = m // 2
			if 2 ** y * 3 ** x != n:
				c = -1
			elif y > x:
				c = -1
			else:
				c = x - y + x
	print(c)


import sys
input = sys.stdin.buffer.readline

def solution():
	for _ in range(int(input())):
		n = int(input())
		cnt2 = 0
		cnt3 = 0
		while n % 2 == 0:
			n //= 2
			cnt2 += 1
		while n % 3 == 0:
			n //= 3
			cnt3 += 1
		if n != 1:
			print(-1)
		elif cnt2 > cnt3:
			print(-1)
		else:
			print(2 * cnt3 - cnt2)
solution()


for _ in range(int(input())):
	n = int(input())
	cnt = 0
	lock = 0
	while n != 1:
		cnt += 1
		if n % 6 == 0:
			n = n // 6
		elif n % 4 == 0 or n % 5 == 0 or n % 7 == 0:
			lock = 1
			break
		else:
			n = n * 2
	if lock == 1:
		print(-1)
	else:
		print(cnt)


t = int(input())
for item in range(t):
	a = int(input())
	if a % 3 != 0 and a != 1:
		print(-1)
	elif a == 1:
		print(0)
	else:
		t = 0
		while a != 1:
			if a % 2 != 0:
				a *= 2
				t += 1
			elif a / 6 != a // 6:
				print(-1)
				break
			else:
				a /= 6
				t += 1
		else:
			print(t)


t = int(input())
while t > 0:
	n = int(input())
	ntwo = 0
	nthree = 0
	while n % 2 == 0:
		n = n // 2
		ntwo += 1
	while n % 3 == 0:
		n = n // 3
		nthree += 1
	if n != 1 or ntwo > nthree:
		print(-1)
	else:
		print(nthree + nthree - ntwo)
	t -= 1


for _ in range(int(input())):
	x = int(input())
	if x == 1:
		print(0)
		continue
	count_2 = 0
	count_3 = 0
	while x % 2 == 0:
		x /= 2
		count_2 += 1
	while x % 3 == 0:
		x /= 3
		count_3 += 1
	if x == 1:
		if count_2 <= count_3:
			print(count_3 - count_2 + count_3)
		else:
			print('-1')
	else:
		print('-1')


t = int(input())
while t:
	n = int(input())
	result = 0
	while n % 6 == 0:
		n = n / 6
		result += 1
	while n % 3 == 0:
		n = n / 3
		result += 2
	if n == 1:
		print(result)
	else:
		print('-1')
	t = t - 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = 1000000000
	count = 0
	if n < 1:
		print(-1)
	elif n == 1:
		print(0)
	else:
		while n != 1:
			if n > a:
				count = 0
				break
			if n % 6 == 0:
				n = n // 6
				count += 1
			else:
				n = n * 2
				count += 1
		if count:
			print(count)
		else:
			print(-1)


import math

def nat(n):
	if math.floor(n) == n and n > 0:
		return True
	else:
		return False

def two(n):
	count = 0
	while 1:
		if n % 2 == 0:
			count += 1
			n //= 2
		else:
			return count

def three(n):
	count = 0
	while 1:
		if n % 3 == 0:
			count += 1
			n //= 3
		else:
			return count
t = int(input())
for _ in range(t):
	n = int(input())
	if nat(math.log(n, 3)):
		print(int(2 * math.log(n, 3)))
	elif nat(math.log(n, 6)):
		print(int(math.log(n, 6)))
	elif two(n) <= three(n):
		m = n
		m //= 2 ** two(n)
		m //= 3 ** three(n)
		if m == 1:
			print(int(2 * three(n) - two(n)))
		else:
			print(-1)
	else:
		print(-1)


def solve(n: int) -> int:
	p3 = 0
	while n > 1 and n % 3 == 0:
		p3 += 1
		n //= 3
	if n == 1:
		return p3 * 2
	p2 = 0
	while n > 1 and n % 2 == 0:
		p2 += 1
		n //= 2
	return -1 if n != 1 or p2 > p3 else p3 * 2 - p2
tc = int(input())
while tc > 0:
	n = int(input())
	print(solve(n))
	tc -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	num2 = 0
	num3 = 0
	while n % 2 == 0:
		n //= 2
		num2 += 1
	while n % 3 == 0:
		n //= 3
		num3 += 1
	if n != 1 or num2 > num3:
		print(-1)
	else:
		print(2 * num3 - num2)


t = int(input())
while t != 0:
	n = int(input())
	(count1, count2) = (0, 0)
	while n % 2 == 0:
		n = n // 2
		count1 += 1
	while n % 3 == 0:
		n = n // 3
		count2 += 1
	if n == 1 and count1 <= count2:
		print(2 * count2 - count1)
	else:
		print(-1)
	t = t - 1


for i in range(int(input())):
	n = int(input())
	cnt = 0
	while n > 1:
		if n % 6 == 0:
			n = int(n / 6)
			cnt = cnt + 1
		elif n % 2 != 0:
			n = n * 2
			cnt = cnt + 1
		else:
			n = 0
			cnt = -1
	print(cnt)


from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from math import ceil, sqrt, gcd, factorial
from collections import defaultdict as dd
for _ in range(it()):
	n = it()
	ans = flag = 0
	while n > 1:
		if n % 6 == 0:
			n //= 6
			ans += 1
		elif n % 3 == 0:
			n //= 3
			ans += 2
		else:
			flag = 1
			break
	if flag:
		print(-1)
	else:
		print(ans)


import math
t = int(input())
list = []

def vtwo(n):
	if n % 2 != 0:
		return 0
	else:
		c = 0
		while n % 2 == 0:
			n = n / 2
			c += 1
		return c

def vthree(n):
	if n % 3 != 0:
		return 0
	else:
		c = 0
		while n % 3 == 0:
			n = n / 3
			c += 1
		return c
for x in range(1, t + 1):
	n = int(input())
	if n == 1:
		list.append(0)
	elif vthree(n) == 0:
		list.append(-1)
	elif n / (2 ** vtwo(n) * 3 ** vthree(n)) != 1:
		list.append(-1)
	elif vthree(n) - vtwo(n) < 0:
		list.append(-1)
	else:
		list.append(2 * vthree(n) - vtwo(n))
for x in list:
	print(x)


t = int(input())
res = []
for _ in range(t):
	n = int(input())
	(d2, d3) = (0, 0)
	if n == 1:
		res.append(0)
		continue
	while n % 2 == 0:
		d2 += 1
		n //= 2
	while n % 3 == 0:
		d3 += 1
		n //= 3
	if n != 1 or d2 > d3:
		res.append(-1)
	else:
		res.append(d3 - d2 + d3)
for el in res:
	print(el)


def mi():
	return map(int, input().split())
for _ in range(int(input())):
	(tw, th) = (0, 0)
	n = int(input())
	while n % 2 == 0:
		n //= 2
		tw += 1
	while n % 3 == 0:
		th += 1
		n //= 3
	if n > 1 or tw > th:
		print(-1)
	else:
		print(2 * th - tw)


def func(n, m):
	if n == 1:
		return m
	elif n < 6:
		return func(2 * n, m + 1)
	elif n <= 0:
		return 1
	elif n % 6 == 0:
		return func(n // 6, m + 1)
	elif n % 3 == 0:
		return func(2 * n, m + 1)
	else:
		return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(func(n, 0))


test_case = int(input())
for _ in range(test_case):
	a = int(input())
	count = 0
	while a != 1 and a <= 1000000000.0:
		if a % 6 == 0:
			a = a // 6
		else:
			a = a * 2
		count += 1
	if a >= 1000000000.0:
		print(-1)
	else:
		print(count)


for test in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	n1 = n
	three = 0
	two = 0
	while n1 % 3 == 0:
		n1 //= 3
		three += 1
	while n1 % 2 == 0:
		n1 //= 2
		two += 1
	if two <= three and 3 ** three * 2 ** two == n:
		print(three + three - two)
	else:
		print(-1)


t = int(input())
for u in range(t):
	n = int(input())
	fac2 = 0
	fac3 = 0
	if n % 3 != 0 and n != 1:
		print(-1)
	elif n == 1:
		print(0)
	else:
		if n % 2 == 0:
			while n % 2 == 0:
				n = n / 2
				fac2 = fac2 + 1
			else:
				pass
		while n % 3 == 0:
			n = n / 3
			fac3 = fac3 + 1
		if n != 1:
			print(-1)
		elif fac2 > fac3:
			print(-1)
		else:
			print(2 * fac3 - fac2)


t = int(input())
for i in range(t):
	z = int(input())
	s = 0
	while z % 6 == 0:
		z //= 6
		s += 1
	while z % 3 == 0:
		z //= 3
		s += 2
	if z == 1:
		print(s)
	else:
		print(-1)


n = int(input())
for w in range(n):
	a = int(input())
	(count1, count2) = (0, 0)
	while a % 6 == 0:
		a = a // 6
		count1 += 1
	while a % 3 == 0:
		a = a // 3
		count2 += 2
	if a == 1:
		print(count1 + count2)
	else:
		print('-1')


import math
t = int(input())
while t:
	n = int(input())
	x = 0
	y = 0
	while n % 2 == 0:
		x += 1
		n = n / 2
	while n % 3 == 0:
		y += 1
		n = n / 3
	if n != 1 or x > y:
		print(-1)
		t -= 1
		continue
	y = y - x
	print(y * 2 + x * 1)
	t -= 1


import sys
caseCount = int(next(sys.stdin))

def v(p, n):
	lo = 0
	hi = 30
	while hi > lo + 1:
		mid = (lo + hi) // 2
		if n % p ** mid == 0:
			lo = mid
		else:
			hi = mid - 1
	if n % p ** hi == 0:
		return hi
	return lo

def solve(n):
	threes = v(3, n)
	twos = v(2, n)
	if threes < twos:
		return -1
	if 3 ** threes * 2 ** twos != n:
		return -1
	return threes + (threes - twos)
ans = ''
for i in range(caseCount):
	n = int(next(sys.stdin))
	ans += str(solve(n)) + '\n'
print(ans)


ct = int(input())
v = []
for _ in range(ct):
	n = int(input())
	count = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			count += 1
		elif n % 3 == 0:
			n *= 2
			count += 1
		else:
			count = -1
			break
	v.append(count)
for i in range(ct):
	print(v[i])


import math
t = int(input())
for q in range(t):
	n = int(input())
	cont = 0
	flag = False
	while n != 1:
		if n % 6 == 0:
			cont += 1
			n //= 6
			flag = False
		elif flag == False:
			cont += 1
			n *= 2
			flag = True
		else:
			break
	if flag:
		print(-1)
	else:
		print(cont)


for nt in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	(c3, c2) = (0, 0)
	m = n
	while n % 3 == 0:
		n = n // 3
		c3 += 1
	n = m
	while n % 2 == 0:
		n = n // 2
		c2 += 1
	if c2 == c3:
		n = m
		ans = 0
		while n % 6 == 0:
			n = n // 6
			ans += 1
		if n == 1:
			print(ans)
		else:
			print(-1)
	elif c3 > c2:
		n = m
		for i in range(c3 - c2):
			n = n * 2
		ans = c3 - c2
		while n % 6 == 0:
			n = n // 6
			ans += 1
		if n == 1:
			print(ans)
		else:
			print(-1)
	else:
		print(-1)


t = int(input())
while t:
	n = int(input())
	if n == 1:
		print(0)
		t -= 1
		continue
	twoes = 0
	threes = 0
	while n % 2 == 0:
		twoes += 1
		n //= 2
	while n % 3 == 0:
		threes += 1
		n //= 3
	if n != 1:
		print(-1)
	elif threes == 0:
		print(-1)
	elif twoes == 0:
		print(2 * threes)
	elif threes > twoes:
		print(twoes + 2 * (threes - twoes))
	elif threes == twoes:
		print(threes)
	else:
		print(-1)
	t -= 1


t = int(input())
for case in range(t):
	n = int(input())
	m = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
		elif n % 10 in [1, 2, 3, 6, 7, 9]:
			n *= 2
		else:
			m = -1
			break
		m += 1
	print(m)


t = int(input())
for test in range(t):
	n = int(input())
	nc = n
	tri = 0
	while n % 3 == 0:
		n //= 3
		tri += 1
	dva = 0
	while n % 2 == 0:
		n //= 2
		dva += 1
	if nc == 1:
		print(0)
	elif tri == 0 or n != 1:
		print(-1)
	elif dva > tri:
		print(-1)
	else:
		print(tri - dva + tri)


from sys import stdin, stdout
import math
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))

def mlog(a, b):
	x = 0
	while not a % b:
		x += 1
		a //= b
	return x
(t,) = mp()
for _ in range(t):
	(n,) = mp()
	count2 = mlog(n, 2)
	count3 = mlog(n, 3)
	if count2 > count3:
		cout('-1\n')
	elif 2 ** count2 * 3 ** count3 == n:
		cout(str(count3 * 2 - count2) + '\n')
	else:
		cout('-1\n')


for _ in range(int(input())):
	n = int(input())
	num = n
	c = d = 0
	while num % 2 == 0:
		c += 1
		num //= 2
	while num % 3 == 0:
		d += 1
		num //= 3
	if c > d or num > 1:
		print(-1)
	else:
		print(d + (d - c))


T = int(input())
for i in range(0, T):
	n = int(input())
	three_f = 0
	two_f = 0
	if n == 1:
		print(0)
	else:
		flag = 0
		while n % 3 == 0:
			three_f += 1
			n = n / 3
		while n % 2 == 0:
			two_f += 1
			n = n / 2
		if n != 1 or three_f < two_f:
			print(-1)
		else:
			print(2 * three_f - two_f)


t = int(input())
for _ in range(t):
	n = int(input())
	to = 0
	th = 0
	notFound = True
	k = n
	l = n
	while notFound and k > 0:
		if k % 2 != 0:
			notFound = False
		else:
			to += 1
			k /= 2
	notFound = True
	while notFound and l > 0:
		if k % 3 != 0:
			notFound = False
		else:
			th += 1
			k /= 3
	if 2 ** to * 3 ** th != n:
		print(-1)
	elif to > th:
		print(-1)
	else:
		print(th - to + th)


t = int(input())
for k in range(t):
	n = int(input())
	cont = 0
	while not n % 6:
		cont += 1
		n //= 6
	while not n % 3:
		cont += 2
		n //= 3
	if n == 1:
		print(cont)
	else:
		print(-1)


t = int(input())
for test in range(t):
	x = int(input())
	x1 = x
	c2 = 0
	c = 0
	while x1 > 0:
		if x1 % 3 == 0:
			x1 = int(x1 / 3)
			c2 = c2 + 1
		else:
			break
	while True:
		if x == 1:
			break
		elif c2 <= 0:
			c = -1
			break
		elif x % 6 == 0:
			x = int(x / 6)
			c = c + 1
			c2 = c2 - 1
		else:
			x = x * 2
			c = c + 1
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	count = 0
	tmp = n
	while tmp % 2 == 0:
		tmp //= 2
		count += 1
	count2 = 0
	while tmp % 3 == 0:
		tmp //= 3
		count2 += 1
	if tmp != 1:
		print(-1)
	elif count > count2:
		print(-1)
	else:
		ans = count2 - count
		ans += count2
		print(ans)


def solve(n):
	c = 0
	count = 0
	while n != 1:
		if c == 0:
			if n % 6 != 0:
				n = n * 2
				c = 1
				count += 1
			else:
				n = n // 6
				c = 0
				count += 1
		elif n % 6 == 0:
			n = n // 6
			count += 1
			c = 0
		else:
			return -1
	return count
t = int(input())
while t:
	n = int(input())
	print(solve(n))
	t -= 1


import sys

def rl():
	return sys.stdin.readline().strip()
T = int(rl())
for test in range(T):
	n = int(rl())
	count2 = 0
	count3 = 0
	while n % 2 == 0:
		n //= 2
		count2 += 1
	while n % 3 == 0:
		n //= 3
		count3 += 1
	if n != 1:
		print(-1)
	elif count2 > count3:
		print(-1)
	else:
		print(2 * count3 - count2)


import sys
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
from collections import defaultdict as df
import copy
import math
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	two = 0
	three = 0
	while n % 2 == 0:
		n //= 2
		two += 1
	while n % 3 == 0:
		n //= 3
		three += 1
	if n != 1:
		print('-1')
	elif two > three:
		print('-1')
	elif two == three:
		print(two)
	else:
		print(three - two + three)


from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
import math
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()
rli = lambda : map(int, stdin.readline().split())
rlf = lambda : map(float, stdin.readline().split())
(INF, NINF) = (float('inf'), float('-inf'))

def solv(n):
	upperlim = 10 ** 9 + 10
	Q = deque()
	seen = defaultdict(lambda : False)
	layer = 0
	Q.appendleft(n)
	while Q:
		for _ in range(len(Q)):
			curr = Q.pop()
			if seen[curr]:
				continue
			seen[curr] = True
			if curr == 1:
				return layer
			a = 2 * curr
			if a <= upperlim and (not seen[a]):
				Q.appendleft(a)
			if curr % 6 == 0 and (not seen[curr // 6]):
				Q.appendleft(curr // 6)
		layer += 1
	return -1

def main():
	T = int(rl())
	for _ in range(T):
		n = int(rl())
		print(solv(n))
	stdout.close()
main()


t = int(input())
for isc in range(t):
	n = int(input())
	count3 = 0
	count2 = 0
	if n == 1:
		print(0)
		continue
	f = False
	while n != 1:
		if n % 3 == 0:
			n = n // 3
			count3 += 1
		elif n % 2 == 0:
			n = n // 2
			count2 += 1
		else:
			f = True
			break
	if f:
		print('-1')
	elif count2 == count3:
		print(count2)
	elif count2 > count3:
		print('-1')
	else:
		print(2 * count3 - count2)


def main(n):
	count = 0
	while n != 1:
		if n % 3 != 0:
			return -1
		elif n % 2 != 0:
			n *= 2
			count += 1
		else:
			n /= 6
			count += 1
	return count
for i in range(int(input())):
	n = int(input())
	print(main(n))


for testcase in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	twos = 0
	while 1:
		if n % 2 == 0:
			n //= 2
			twos += 1
		else:
			break
	threes = 0
	while 1:
		if n % 3 == 0:
			n //= 3
			threes += 1
		else:
			break
	if n != 1:
		print(-1)
	elif twos > threes:
		print(-1)
	else:
		print(threes - twos + threes)


PI = 3.141592653589793
INF = 999999999
MOD = 1000000007

def add(x, y):
	return (x + y) % MOD

def sub(x, y):
	return (x - y + MOD) % MOD

def mul(x, y):
	return x * y % MOD

def gcd(x, y):
	if y == 0:
		return x
	return gcd(y, x % y)

def lcm(x, y):
	return x * y // gcd(x, y)

def power(x, y):
	res = 1
	x %= MOD
	while y != 0:
		if y & 1:
			res = mul(res, x)
		y >>= 1
		x = mul(x, x)
	return res

def mod_inv(n):
	return power(n, MOD - 2)

def prob(p, q):
	return mul(p, power(q, MOD - 2))

def ii():
	return int(input())

def li():
	return [int(i) for i in input().split()]

def ls():
	return [i for i in input().split()]
store = [6 ** i for i in range(30)]
s2 = [2 ** i for i in range(30)]
for t in range(ii()):
	n = ii()
	ans = -1
	for i in range(30):
		temp = n * s2[i]
		for j in range(30):
			if temp == store[j]:
				ans = i + j
				break
		if ans != -1:
			break
	print(ans)


for _ in range(int(input())):
	x = int(input())
	c = 0
	while x != 1:
		if x % 2 == 0 and x % 6 != 0:
			c = -1
			break
		elif x % 6 == 0:
			x //= 6
			c += 1
		else:
			x *= 2
			c += 1
	print(c)


from sys import stdin

def ip():
	return [int(i) for i in stdin.readline().split()]

def sp():
	return [str(i) for i in stdin.readline().split()]

def solve():
	n = int(input())
	ans = 0
	while n != 1:
		if n % 2 == 0 and n % 6 != 0:
			return -1
		if n % 6 == 0:
			n = n // 6
		else:
			n *= 2
		ans += 1
	return ans
t = int(input())
for i in range(t):
	print(solve())


t = input()
for test in range(int(t)):
	num = int(input())
	steps = 0
	while True:
		if num == 1:
			print(steps)
			break
		elif num % 6 == 0:
			num = num / 6
		elif num % 3 == 0:
			num = num * 2
		else:
			print(-1)
			break
		steps = steps + 1


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	(c2, c3) = (0, 0)
	while n % 2 == 0:
		n = n // 2
		c2 += 1
	while n % 3 == 0:
		n = n // 3
		c3 += 1
	if n == 1 and c2 <= c3:
		print(2 * c3 - c2)
	else:
		print(-1)


numbersCount = int(input())
for _ in range(numbersCount):
	n = int(input())
	d2 = 0
	d3 = 0
	while n % 2 == 0:
		n = n / 2
		d2 += 1
	while n % 3 == 0:
		n = n / 3
		d3 += 1
	if n != 1 or d2 > d3:
		print(-1)
	else:
		print(min(d2, d3) + (d3 - d2) * 2)


z = int(input())
for h in range(z):
	n = int(input())
	cnt = 0
	while True:
		if n <= 6:
			if n == 3:
				cnt += 2
				n = 1
			if n == 6:
				cnt += 1
				n = 1
			break
		elif n % 6 == 0:
			n = n // 6
			cnt += 1
		elif n * 2 % 6 == 0:
			n = n * 2 // 6
			cnt += 2
		else:
			break
	if n == 1:
		print(cnt)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	count = 0
	while n % 6 == 0:
		n = n / 6
		count += 1
	countt = 0
	while n % 3 == 0:
		n = n / 3
		countt += 1
	if n == 1:
		print(countt * 2 + count)
	else:
		print(-1)


testn = int(input())

def factorise(n):
	factors = {2: 0, 3: 0}
	temp = n
	while temp % 2 == 0:
		temp //= 2
		factors[2] += 1
	while temp % 3 == 0:
		temp //= 3
		factors[3] += 1
	return (temp == 1, factors)
for i in range(testn):
	n = int(input())
	(is_divved, factors) = factorise(n)
	if not is_divved:
		print(-1)
	elif factors[2] > factors[3]:
		print(-1)
	else:
		print(factors[3] + (factors[3] - factors[2]))


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
	n = read_int()
	if n == 1:
		print(0)
	else:
		two = 0
		three = 0
		while n % 2 == 0:
			n //= 2
			two += 1
		while n % 3 == 0:
			n //= 3
			three += 1
		if n != 1 or two > three:
			print(-1)
		else:
			print(three * 2 - two)


def check(a):
	(i, j) = (0, 0)
	if a == 1:
		return 0
	while a % 2 == 0:
		a = a / 2
		i += 1
	while a % 3 == 0:
		a = a / 3
		j += 1
	if a == 1 and i <= j:
		return 2 * j - i
	else:
		return -1
n = int(input())
output = []
for i in range(n):
	output.append(check(int(input())))
for i in output:
	print(i)


import math
import random
from collections import *
mod = 1000000007
r1 = lambda : int(input())
rm = lambda : map(int, input().split())
rms = lambda : map(str, input().split())
rls = lambda : list(rm())

def def_value():
	return 'nan'

def solve():
	test = r1()
	while test:
		test -= 1
		n = r1()
		flag = True
		ans = 0
		while n > 1:
			if n % 6 == 0:
				n = n // 6
			elif n % 6 != 3:
				flag = False
				break
			else:
				n *= 2
			ans += 1
		if flag:
			print(ans)
		else:
			print(-1)
solve()


from heapq import *
from collections import *
import os
import sys
from io import BytesIO, IOBase
import math
ins = lambda : [int(x) for x in input()]
inp = lambda : int(input())
inps = lambda : [int(x) for x in input().split()]

def main():
	t = inp()
	for i in range(t):
		vis = set()
		n = inp()
		if n == 1:
			print(0)
			continue
		if n % 3 != 0:
			print(-1)
			continue
		step = 0
		f = 0
		while n != 1:
			if n % 3 != 0:
				f = 1
				break
			if n not in vis:
				vis.add(n)
			else:
				f = 1
				break
			if n % 6 == 0:
				n //= 6
			else:
				n *= 2
			step += 1
		if not f:
			print(step)
		else:
			print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline

def prime_factorize(n):
	count = {3: 0, 2: 0}
	for p in [2, 3]:
		while n % p == 0:
			count[p] += 1
			n //= p
	flag = True if n == 1 else False
	return (flag, count)

def solve(n):
	(flag, count) = prime_factorize(n)
	if not flag:
		return -1
	elif count[3] >= count[2]:
		return count[3] + (count[3] - count[2])
	else:
		return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(solve(n))


import math

def primeFactors(n):
	r = []
	while n % 2 == 0:
		r.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			r.append(int(i))
			n = n // i
	if n > 2:
		r.append(n)
	return r
for _ in range(0, int(input())):
	n = int(input())
	move = 0
	while True:
		if n == 1:
			print(move)
			break
		move += 1
		if n % 6 == 0:
			n //= 6
		elif n % 3 == 0:
			n *= 2
		else:
			print(-1)
			break


from math import log
for i in range(int(input())):
	(N, Two, Three) = (int(input()), 0, 0)
	while N % 2 == 0:
		Two += 1
		N //= 2
	while N % 3 == 0:
		Three += 1
		N //= 3
	print(-1 if Three < Two or N != 1 else Three - Two + Three)


from math import *
tests = int(input())
for t in range(tests):
	n = int(input())
	res = -1
	l = 0
	m = 0
	while n % 2 == 0:
		l += 1
		n //= 2
	while n % 3 == 0:
		m += 1
		n //= 3
	if n == 1 and m >= l:
		res = 2 * m - l
	print(res)


n = int(input())

def delimost(a):
	c = 0
	if a == 1:
		return 0
	while a % 3 == 0 and a > 1:
		if a % 6 == 0:
			a = a // 6
			c += 1
		if a % 3 == 0 and a % 6 != 0:
			a = a // 3
			c += 2
	if a == 1:
		return c
	else:
		return -1
for i in range(n):
	b = int(input())
	print(delimost(b))


import math
from collections import deque
import sys
sys.setrecursionlimit(10 ** 4)

def Divisors(n):
	l = []
	i = 2
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
		i = i + 1
	return l

def SieveOfEratosthenes(n):
	l = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			l.append(p)
	return l

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(n)
	return l

def Factors(n):
	result = []
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			if i == n / i:
				result.append(i)
			else:
				result.append(i)
				result.append(n // i)
	result.append(1)
	result.append(n)
	return result

def maxSubArraySum(a):
	max_so_far = 0
	max_ending_here = 0
	size = len(a)
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < abs(max_ending_here):
			max_so_far = max_ending_here
	return max_so_far

def longestsubarray(arr, n, k):
	current_count = 0
	max_count = 0
	for i in range(0, n, 1):
		if arr[i] % k != 0:
			current_count += 1
		else:
			current_count = 0
		max_count = max(current_count, max_count)
	return max_count

def main():
	n = int(input())
	if n == 1:
		print(0)
	else:
		c1 = 0
		while n > 0:
			if n % 3 == 0:
				n = n // 3
				c1 += 1
			else:
				break
		if n == 1:
			print(2 * c1)
		else:
			c2 = 0
			while n > 0:
				if n % 2 == 0:
					n = n // 2
					c2 += 1
				else:
					break
			if c2 > c1 or n != 1:
				print(-1)
				return
			else:
				print(c1 - c2 + c1)
t = int(input())
for i in range(0, t):
	main()


def ans(n):
	cnt = 0
	if n == 1:
		return 0
	while True:
		if n == 1:
			break
		elif n % 6 == 0:
			n //= 6
			cnt += 1
		elif n % 3 == 0 and n % 2 != 0:
			n *= 2
			cnt += 1
		elif n % 3 != 0:
			break
	if cnt == 0 or n != 1:
		return -1
	else:
		return cnt
for _ in range(int(input())):
	n = int(input())
	print(ans(n))


no = int(input())
n = []
for i in range(0, no):
	n.append(int(input()))
for i in range(0, no):
	k = 0
	br = 0
	while True:
		if n[i] < 6 and n[i] != 3:
			if n[i] == 1:
				print(k)
				break
			elif n[i] == 2 or n[i] == 4 or n[i] == 5:
				print(-1)
				break
		elif n[i] % 6 == 0:
			n[i] = n[i] / 6
			k += 1
			br = 0
		else:
			if br == 1:
				print(-1)
				break
			n[i] = n[i] * 2
			k += 1
			br = 1


def find_cnt():
	n = int(input())
	w = 0
	cnt = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			cnt += 1
			w = 0
		else:
			n = n * 2
			cnt += 1
			w += 1
		if w == 2:
			return -1
	return cnt
t = int(input())
for i in range(t):
	print(find_cnt())


T = int(input())
for _ in range(T):
	n = int(input())
	moves = 0
	while n % 3 == 0:
		if n % 2 == 0:
			n = n // 6
			moves += 1
		else:
			n = n // 3
			moves += 2
	if n == 1:
		print(moves)
	else:
		print(-1)


def solve(n):
	if n == 1:
		return 0
	no2 = 0
	while n % 2 == 0:
		n = n // 2
		no2 += 1
	no3 = 0
	while n % 3 == 0:
		n = n // 3
		no3 += 1
	if n > 1:
		return -1
	if no2 > no3:
		return -1
	if no2 <= no3:
		return 2 * no3 - no2
t = int(input())
for _ in range(t):
	n = int(input())
	print(solve(n))


import math
import os
import random
import re
import sys
from collections import defaultdict
case = int(input())
for i in range(case):
	inp = int(input())
	if inp == 1:
		print(0)
	elif inp % 3 != 0:
		print(-1)
	else:
		(cnt2, cnt3) = (0, 0)
		flg = True
		while inp != 1:
			if inp % 2 == 0:
				inp /= 2
				cnt2 += 1
			elif inp % 3 == 0:
				inp /= 3
				cnt3 += 1
			else:
				flg = False
				break
		if cnt2 > cnt3 or flg is False:
			print(-1)
		else:
			print(cnt3 + (cnt3 - cnt2))


for i in range(int(input())):
	n = int(input())
	p = 0
	q = 0
	while n % 3 ** (p + 1) == 0:
		p += 1
	while n % 2 ** (q + 1) == 0:
		q += 1
	if n != 3 ** p * 2 ** q or p < q:
		print(-1)
	else:
		print(2 * p - q)


t = int(input())

def count_3(n):
	counter = 0
	while n % 3 == 0:
		counter += 1
		n = n / 3
	return counter

def count_2(n):
	counter = 0
	while n % 2 == 0:
		counter += 1
		n = n / 2
	return counter
for i in range(t):
	n = int(input())
	c3 = count_3(n)
	c2 = count_2(n)
	r = n
	if c3 > 0:
		r = r / 3 ** c3
	if c2 > 0:
		r = r / 2 ** c2
	if r == 1 and c3 >= c2:
		print(c3 + (c3 - c2))
	else:
		print(-1)


from sys import stdin, stdout
t = int(stdin.readline().strip())
for _ in range(t):
	n = int(stdin.readline().strip())
	if n == 1:
		stdout.write('0\n')
	else:
		(answer, times) = (0, 0)
		while n != 1:
			if n % 6 == 0:
				n //= 6
				answer += 1
			else:
				n *= 2
				times += 1
				answer += 1
			if times > 128 or (n % 3 != 0 and n != 1):
				answer = -1
				break
		stdout.write(str(answer) + '\n')


for T in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		two = two + 1
		n >>= 1
	while n % 3 == 0:
		three = three + 1
		n //= 3
	if n > 1:
		print(-1)
		continue
	if two > three:
		print(-1)
		continue
	print(three - two + three)


from math import *
t = int(input())
for _ in range(t):
	n = int(input())
	k = 0
	flag = 0
	while True:
		if n == 1:
			break
		if gcd(n, 3) != 3:
			flag = 1
			break
		if n % 6 == 0:
			n = n // 6
			k += 1
		else:
			n = n * 2
			k += 1
	if flag:
		print(-1)
	else:
		print(k)


a = int(input())
i = 0
while i < a:
	t = 0
	f = 0
	b = int(input())
	while b != 1:
		if b % 6 == 0:
			b = b // 6
			if b == 1:
				t = t + 1
				break
		else:
			b = b * 2
		if b % 3 != 0:
			f = f - 1
			break
		t = t + 1
	if f != 0:
		print(-1)
	else:
		print(t)
	i = i + 1


n = int(input())
for i in range(n):
	a = int(input())
	if a == 1:
		print(0)
	else:
		op = 0
		if a % 2 == 0 or a % 3 == 0:
			while a % 6 == 0:
				a = a // 6
				op += 1
			if a == 1:
				print(op)
			elif a % 3 != 0:
				print(-1)
			else:
				k = 0
				while a % 3 == 0:
					k += 1
					a = a // 3
				if a == 1:
					op += k * 2
					print(op)
				else:
					print(-1)
		else:
			print(-1)


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
t = int(input())
for _ in range(t):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		two += 1
		n = n // 2
	while n % 3 == 0:
		three += 1
		n = n // 3
	if n > 1:
		print(-1)
	elif three < two:
		print(-1)
	else:
		print(three - two + three)


import string
import math
n = int(input())
while n > 0:
	arr = list(map(int, input().strip().split()))
	x = arr[0]
	y = x
	two = 0
	three = 0
	fl = 0
	while x > 1:
		if x % 2 == 0:
			x = x / 2
			two = two + 1
		elif x % 3 == 0:
			x = x / 3
			three = three + 1
		elif x != 1:
			fl = -1
			break
	if y == 1:
		print(0)
	elif fl == -1:
		print(-1)
	elif three >= two:
		print(three + three - two)
	else:
		print(-1)
	n = n - 1


num = int(input())
for _ in range(num):
	n = int(input())
	n2 = n
	count = 0
	while n != 1:
		if n % 6 != 0:
			count += 1
			n *= 2
			if n > 1000000000 or n <= 0:
				print(-1)
				break
		else:
			n = int(n / 6)
			count += 1
	else:
		print(count)


t = int(input())
for x in range(t):
	n = int(input())
	(twos, threes) = (0, 0)
	while n % 2 == 0:
		n /= 2
		twos += 1
	while n % 3 == 0:
		n /= 3
		threes += 1
	if n != 1:
		print(-1)
	elif twos > threes:
		print(-1)
	else:
		print(threes - twos + threes)


T = int(input())
res = [-1] * T
for t in range(T):
	n = int(input())
	temp = n
	flag = 0
	count = 0
	while temp != 1:
		if temp % 6 == 0:
			temp = temp / 6
			flag = 1
		else:
			temp = temp * 2
			if flag == 2:
				break
			else:
				flag = 2
		count += 1
	if temp == 1:
		res[t] = count
for i in range(T):
	print(res[i])


import math
t = int(input())
for i in range(t):
	n = int(input())
	n1 = n
	l = 0
	while n1 % 3 == 0:
		n1 //= 3
		l += 1
	d = 0
	while n1 % 2 == 0:
		n1 //= 2
		d += 1
	if l >= d and n1 == 1:
		print(l * 2 - d)
	else:
		print(-1)


times = int(input())
answers = []
for time in range(times):
	currentNumber = int(input())
	if currentNumber == 1:
		answers.append(0)
	else:
		moves = 0
		while currentNumber != 2:
			if currentNumber / 6 == 1:
				moves += 1
				break
			elif currentNumber % 3 != 0:
				moves = -1
				break
			elif currentNumber % 6 == 0:
				currentNumber = currentNumber / 6
			else:
				currentNumber = currentNumber * 2
			moves += 1
		if currentNumber == 2:
			moves = -1
		answers.append(moves)
for i in range(len(answers)):
	print(answers[i])


l = [int(input()) for i in range(int(input()))]

def verify(x):
	n = 0
	m = 0
	while x != 1:
		if x % 6 == 0:
			x = x // 6
			n += 1
			m = 0
		elif x < 6 and x != 3:
			print(-1)
			break
		elif m <= 3:
			x = 2 * x
			n += 1
			m += 1
		else:
			print(-1)
			break
	else:
		print(n)
for i in l:
	verify(i)


for _ in range(int(input())):
	n = int(input())
	(cont, ok) = (0, True)
	if n == 1:
		print(0)
		ok = False
	else:
		while n != 1:
			if n % 6 == 0:
				cont += 1
				n /= 6
			elif n % 3 == 0:
				cont += 2
				n /= 3
			else:
				print(-1)
				ok = False
				break
	if ok == True:
		print(cont)


t = int(input())
for i in range(t):
	num = int(input())
	a = 0
	if num == 1:
		a = 0
	elif num % 3:
		a = -1
	else:
		c = 0
		c1 = 0
		while num % 2 == 0:
			c += 1
			num = num // 2
		while not num % 3:
			c1 += 1
			num = num // 3
		if num != 1 or c > c1:
			a = -1
		else:
			a = 2 * c1 - c
	print(a)


from sys import stdin

def input():
	return stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	n2 = 0
	while n % 2 == 0:
		n2 += 1
		n = n // 2
	n3 = 0
	while n % 3 == 0:
		n3 += 1
		n = n // 3
	if n != 1:
		print(-1)
	elif n2 <= n3:
		print(n3 - n2 + n3)
	else:
		print(-1)


t = int(input())
for x in range(t):
	n = int(input())
	count = 0
	a = 0
	b = 0
	while n != 1:
		if n % 2 == 0:
			count += 1
			a += 1
			n = n / 2
		elif n % 3 == 0:
			count += 1
			b += 1
			n = n / 3
		else:
			count = -1
			break
	if count == -1:
		print(-1)
	elif b - a >= 0:
		print(2 * b - a)
	else:
		print(-1)


import sys
from sys import stdin
from math import factorial

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	p = 0
	q = 0
	while n % 3 == 0:
		n = n // 3
		p += 1
	while n % 2 == 0:
		n = n // 2
		q += 1
	if n != 1:
		print(-1)
	elif q > p:
		print(-1)
	else:
		print(p - q + p)


def getone(n):
	if n == 1:
		return 0
	if n % 3 != 0:
		return -1
	cnt = 0
	while n >= 6 and n % 6 == 0:
		n /= 6
		cnt += 1
	if n < 6:
		if n == 1:
			return cnt
		if n == 3:
			return cnt + 2
		else:
			return -1
	else:
		while n >= 3 and n % 3 == 0:
			n /= 3
			cnt += 2
		if n == 1:
			return cnt
		else:
			return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(getone(n))


def MultiplyandDivide(n):
	doses = 0
	treses = 0
	while n > 1:
		if n % 2 == 0:
			n /= 2
			doses += 1
		elif n % 3 == 0:
			n /= 3
			treses += 1
		else:
			return -1
	if doses > treses:
		return -1
	else:
		return treses + (treses - doses)
Lista = []
total = int(input())
for i in range(total):
	Lista.append(int(input()))
for j in Lista:
	print(MultiplyandDivide(j))


import sys
input = sys.stdin.readline
tc = int(input())
for _ in range(tc):
	n = int(input())
	tmp = n
	ans = 0
	three = 0
	flag = False
	while 1 < tmp:
		if tmp % 6 == 0:
			tmp = tmp // 6
			ans += 1
		else:
			while 1 < tmp:
				if tmp % 3 == 0:
					tmp = tmp // 3
					three += 1
				else:
					flag = True
					break
		if flag:
			break
	if flag:
		print(-1)
	else:
		print(ans + three * 2)


n = int(input())
for i in range(0, n):
	j = int(input())
	if j == 1:
		print('0')
	else:
		count = 0
		while True:
			if j % 3 == 0:
				if j % 6 == 0:
					j = j / 6
					count += 1
					if j == 1:
						print(count)
						break
				else:
					j = j * 2
					count += 1
			else:
				print('-1')
				break


for t in range(int(input())):
	a = int(input())
	x = a
	count = 0
	while x % 3 == 0:
		if x % 6 == 0:
			x /= 6
			count += 1
		else:
			x /= 3
			count += 2
	if x == 1:
		print(count)
	else:
		print(-1)


import math
from collections import deque
from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	cnt = 0
	bf = 0
	while n > 1:
		if n % 6:
			n *= 2
			bf += 1
		else:
			n //= 6
			bf = 0
		if bf == 2:
			cnt = -1
			break
		cnt += 1
	print(cnt)


x = int(input())
p = pow(10, 10)
for i in range(0, x):
	n = int(input())
	sum = 0
	while True:
		if n == 1:
			break
		if n % 6 == 0:
			n /= 6
			sum += 1
		else:
			n *= 2
			sum += 1
		if n > p:
			break
	if n == 1:
		print(sum)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	nn = n
	ct = 0
	while nn % 3 == 0:
		nn //= 3
		ct += 1
	ct1 = 0
	while nn % 2 == 0:
		nn //= 2
		ct1 += 1
	if nn == 1:
		if ct1 == ct:
			print(ct)
		elif ct1 < ct:
			print(ct - ct1 + ct)
		else:
			print(-1)
	else:
		print(-1)


for i in range(int(input())):
	t = int(input())
	T = t
	if t % 2 == 0 or t % 3 == 0:
		bi = 0
		tr = 0
		while t % 2 == 0:
			bi += 1
			t = t // 2
		while t % 3 == 0:
			tr += 1
			t = t // 3
		if t == 1 and bi >= 0 and (tr >= bi):
			print(2 * tr - bi)
		else:
			print(-1)
	elif t == 1:
		print(0)
	else:
		print(-1)


a = int(input())
for i in range(a):
	b = int(input())
	cod = 0
	cot = 0
	while b % 2 == 0:
		b = b // 2
		cod += 1
	while b % 3 == 0:
		b = b // 3
		cot += 1
	if b != 1:
		print(-1)
	elif cod > cot:
		print(-1)
	else:
		print(cot + cot - cod)


import sys, math, os
from io import BytesIO, IOBase
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
INF = 10 ** 9
mod = 998244353
for t in range(int(data())):
	n = int(data())
	a1 = a2 = 0
	while n % 3 == 0:
		a1 += 1
		n //= 3
	while n % 2 == 0:
		a2 += 1
		n //= 2
	if n != 1 or a2 > a1:
		out(-1)
	else:
		out(a1 - a2 + a1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ans = 0
	while 10 ** 14 > n > 1:
		if n % 6 == 0:
			n //= 6
		else:
			n <<= 1
		ans += 1
	print(ans if n == 1 else -1)


q = int(input())
for num in range(0, q):
	i = 0
	a = int(input())
	while a > 1:
		if a / 3 - a // 3 > 0:
			print(-1)
			break
		elif a / 2 == a // 2:
			a = a / 6
			i = i + 1
		else:
			a = a / 3
			i = i + 2
	else:
		print(i)


def main():
	n = int(input())
	i = 0
	k = 0
	while True:
		if n == 1:
			return i
		i += 1
		if n / 6 == n // 6:
			n = n // 6
			k -= 1
		else:
			n = n * 2
			k += 1
		if k == 10:
			return -1
t = int(input())
for i in range(t):
	print(main())


import sys

def solve(x):
	if x == 1:
		return 0
	x1 = int(x)
	d = 0
	t = 0
	while x1 % 2 == 0:
		x1 //= 2
		d += 1
	while x1 % 3 == 0:
		x1 //= 3
		t += 1
	if x1 == 1 and d <= t:
		return 2 * t - d
	else:
		return -1
for _ in range(int(sys.stdin.readline().strip())):
	n = int(sys.stdin.readline().strip())
	sys.stdout.write(str(solve(n)) + '\n')


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		ch0 = 0
		ch1 = 0
		while n % 2 == 0:
			ch0 += 1
			n //= 2
		while n % 3 == 0:
			ch1 += 1
			n //= 3
		if n != 1 or ch1 < ch0:
			print(-1)
		else:
			print(2 * ch1 - ch0)


for _ in range(int(input())):
	default_n = int(input())
	n = default_n
	if n == 1:
		print(0)
		continue
	if n % 6 != 0 and n * 2 % 6 != 0:
		print(-1)
		continue
	attempts = 0
	broken = 0
	while n != 1:
		while n % 6 != 0:
			n *= 2
			attempts += 1
			if attempts > 5 and n >= default_n:
				print(-1)
				broken = 1
				break
		if broken:
			break
		while n % 6 == 0:
			n //= 6
			attempts += 1
			if attempts > 5 and n >= default_n:
				print(-1)
				broken = 1
				break
		if broken:
			break
	if broken == 0:
		print(attempts)


T = int(input())

def div3(num):
	num = str(num)
	l = [int(i) for i in num]
	s = sum(l)
	if s % 3 == 0:
		return True
	else:
		return False
while T > 0:
	n = int(input())
	step = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			step += 1
		elif div3(n):
			n *= 2
			step += 1
		else:
			step = -1
			break
	print(step)
	T = T - 1


def solve(lt):
	steps = 0
	i = 0
	while lt % 3 == 0:
		i += 1
		lt /= 3
	j = 0
	while lt % 2 == 0:
		j += 1
		lt /= 2
	if lt != 1:
		return -1
	elif j > i:
		return -1
	else:
		steps = 2 * i - j
	return steps

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))
n = inp()
in_lst = []
for i in range(n):
	in_lst.append(inp())
for i in range(n):
	print(solve(in_lst[i]))


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	f2 = f3 = ans = 0
	while n != 1:
		if n % 2 == 0:
			n //= 2
			f2 += 1
		elif n % 3 == 0:
			n //= 3
			f3 += 1
		else:
			ans = -1
			break
	if ans == -1 or f2 > f3:
		print(-1)
		continue
	ans = f2 + (f3 - f2) * 2
	print(ans)


for u in range(int(input())):
	x = int(input())
	f = False
	ans = 0
	while x > 1:
		if x % 6 == 0:
			x = x // 6
		elif x % 6 != 0 and x % 6 != 2:
			x = x * 2
		else:
			f = True
			break
		ans += 1
	if f:
		print(-1)
	else:
		print(ans)


def read_int() -> int:
	return int(input())

def read_ints():
	return map(int, input().split(' '))

def test_case():
	original_number = read_int()
	number = original_number
	steps = 0
	while number != 1:
		if number % 6 == 0:
			number /= 6
			steps += 1
		else:
			number *= 2
			steps += 1
			if number > 2 * original_number:
				print(-1)
				return -1
	else:
		print(steps)
tests = read_int()
for _ in range(tests):
	test_case()


t = int(input())
n = []
for i in range(t):
	num = int(input())
	n.append(num)
count = 0

def powin(num, k):
	global count
	if num % k == 0:
		num = int(num / k)
		count += 1
		powin(num, k)
	return count
for i in range(t):
	count = 0
	p2 = powin(n[i], 2)
	count = 0
	p3 = powin(n[i], 3)
	if n[i] == 1:
		print(0)
	elif n[i] / (2 ** p2 * 3 ** p3) == 1 and p2 <= p3:
		print(2 * p3 - p2)
	else:
		print(-1)


cases = int(input())
answers = []
for i in range(cases):
	n = int(input())
	num = 0
	while n != 1:
		if n % 3 != 0 and n != 1:
			num = -1
			answers.append(num)
			break
		elif n == 1:
			answers.append(num)
			break
		elif n % 6 != 0:
			n *= 2
			num += 1
		else:
			n //= 6
			num += 1
	else:
		answers.append(num)
for i in answers:
	print(i)


t = int(input())

def f(n):
	cnt = 0
	while True:
		if n == 1:
			return cnt
		if n % 6 == 0:
			n //= 6
			cnt += 1
		elif n % 3 == 0:
			n *= 2
			cnt += 1
		else:
			return -1
for cases in range(t):
	n = int(input())
	print(f(n))


t = int(input())
for i in range(t):
	n = int(input())
	N1 = 0
	N2 = 0
	while n % 3 == 0:
		n = n // 3
		N1 += 1
	while n % 2 == 0:
		n = n // 2
		N2 += 1
	print(-1 if n != 1 or N2 > N1 else N1 - N2 + N1)


T = int(input())
while T > 0:
	n = int(input())
	res = 0
	form = True
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			res += 1
		elif n % 3 == 0:
			n = n // 3
			res += 2
		else:
			form = False
			break
	if form is False:
		print(-1)
	else:
		print(res)
	T -= 1


def list_input():
	return list(map(int, input().split()))

def multiple_input():
	return map(int, input().split())

def factor(x):
	(count2, count3) = (0, 0)
	temp = x
	while x % 2 == 0:
		x /= 2
		count2 += 1
	while x % 3 == 0:
		x /= 3
		count3 += 1
	if 2 ** count2 * 3 ** count3 != temp:
		return -1
	elif count2 < count3:
		return count3 - count2 + count3
	elif count2 == count3:
		return count3
	else:
		return -1
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		print(factor(n))


for t in range(int(input())):
	n = int(input())
	(tw, th) = (0, 0)
	while not n & 1:
		tw = tw + 1
		n = n >> 1
	while n % 3 == 0:
		th = th + 1
		n = n // 3
	if n != 1 or tw > th:
		print('-1')
	else:
		m = max(tw, th)
		ans = m + (m - tw)
		print(ans)


import math
import sys
import collections
from collections import defaultdict
from sys import stdin, stdout
MOD = 10 ** 9 + 7

def take_root(num):
	return math.ceil(math.sqrt(num))

def sieve():
	limit = 86028121
	isprime = [1] * 86028122
	isprime[0] = 0
	isprime[1] = 0
	for i in range(2, take_root(limit + 1)):
		if isprime[i] == 1:
			for j in range(i * i, limit + 1, i):
				isprime[j] = 0
	primes = []
	for i in range(limit + 1):
		if isprime[i] == True:
			primes.append(i)
	return primes

def minput():
	return map(int, input().split())

def iinput():
	return int(input())

def strinput():
	return input()

def linput():
	return list(map(int, input().split()))
for _ in range(iinput()):
	n = iinput()
	if n == 1:
		print(0)
	else:
		temp1 = 0
		while n > 0 and n % 6 == 0:
			n = n // 6
			temp1 += 1
		temp2 = 0
		while n > 0 and n % 3 == 0:
			n = n // 3
			temp2 += 1
		if n == 1:
			print(temp1 + 2 * temp2)
		else:
			print(-1)


t = int(input())
for tit in range(t):
	n = int(input())
	can_do = True
	moves = 0
	while n > 1:
		if n % 3 != 0:
			can_do = False
			break
		if n % 6 != 0:
			n *= 2
			n //= 6
			moves += 2
		else:
			n //= 6
			moves += 1
	if can_do:
		print(moves)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	(temp1, temp2) = (n, n)
	count3 = 0
	count2 = 0
	while True:
		if temp1 % 3 == 0:
			count3 += 1
			temp1 = temp1 // 3
		else:
			break
	while True:
		if temp2 % 2 == 0:
			count2 += 1
			temp2 = temp2 // 2
		else:
			break
	if 3 ** count3 * 2 ** count2 == n and count3 >= count2:
		print(count3 + count3 - count2)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0 and n % 6 != 0:
		print(-1)
	else:
		ans = 0
		while n % 6 == 0 or n % 3 == 0:
			if n % 3 == 0 and n % 6 != 0:
				ans += 1
				n *= 2
			n //= 6
			ans += 1
		print(ans if n == 1 else -1)


in_text = []
out_text = []
range_123 = int(input())
for i in range(0, range_123):
	in_text.append(int(input()))
for i in range(0, range_123):
	n = in_text[i]
	n_two = n
	n_three = n
	two = 0
	three = 0
	while n_two % 2 == 0:
		two = two + 1
		n_two = n_two // 2
	while n_three % 3 == 0:
		three = three + 1
		n_three = n_three // 3
	if two > three or 2 ** two * 3 ** three != n:
		print('-1')
	else:
		print(three - two + three)


from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def inin():
	return int(input())

def stin():
	return input()

def spin():
	return map(int, stin().split())

def lin():
	return list(map(int, stin().split()))

def matrix(n):
	return [list(map(int, input().split())) for i in range(n)]

def count2Dmatrix(i, list):
	return sum((c.count(i) for c in list))

def modinv(n, p):
	return pow(n, p - 2, p)

def GCD(x, y):
	x = abs(x)
	y = abs(y)
	if min(x, y) == 0:
		return max(x, y)
	while y:
		(x, y) = (y, x % y)
	return x

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
prime = []

def SieveOfEratosthenes(n):
	global prime
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	f = []
	for p in range(2, n):
		if prime[p]:
			f.append(p)
	return f
q = []

def dfs(n, d, v, c):
	global q
	v[n] = 1
	x = d[n]
	q.append(n)
	j = c
	for i in x:
		if i not in v:
			f = dfs(i, d, v, c + 1)
			j = max(j, f)
	return j
for _ in range(inin()):
	n = inin()
	c = 0
	while True:
		if n == 1:
			break
		elif n % 6 == 0:
			pass
		elif n * 2 % 6 == 0:
			pass
		else:
			c = -1
			break
		if n % 6 == 0:
			n = n // 6
			c += 1
		else:
			n *= 2
			c += 1
	print(c)


t = int(input())
for i in range(t):
	a = int(input())
	c = 0
	if a == 1:
		print('0')
	while a != 1 and a != -1:
		while a % 6 != 0:
			if a % 2 == 0 and a % 3 != 0:
				a = -1
				print(a)
				break
			a = a * 2
			c = c + 1
		while a % 6 == 0:
			a = a // 6
			c = c + 1
		if a == 1:
			print(c)


n = []
t = int(input())
for _ in range(t):
	n.append(int(input()))
for test in range(t):
	ans = 0
	x = n[test]
	while x > 1:
		if x % 6 == 0 or x % 3 == 0:
			if x % 6 == 0:
				x /= 6
				ans += 1
			else:
				x /= 3
				ans += 2
		else:
			ans = -1
			break
	print(ans)


T = int(input())
dic = {}

def is_2power(K):
	if K == 2:
		return True
	temp = 2
	while True:
		temp *= 2
		if temp == K:
			return True
		if temp > K:
			return False

def Fun(num, moves, Mult):
	if num in dic:
		return dic[num]
	temp = num
	while True:
		if Mult > 10:
			dic[num] = -1
			return -1
		if temp == 1:
			dic[num] = moves
			return moves
		if temp == 0 or is_2power(temp):
			dic[num] = -1
			return -1
		if temp % 6 == 0:
			temp /= 6
			Mult = 0
			moves += 1
		else:
			temp *= 2
			Mult += 1
			moves += 1
for i in range(T):
	n = int(input())
	print(Fun(n, 0, 0))


for t in range(int(input())):
	n = int(input())
	(t, s) = (0, -1)
	for i in range(20):
		for j in range(20):
			k = 2 ** i * 3 ** j
			if n == k:
				t = i
				s = j
				break
	if t <= s:
		print(s - t + s)
	else:
		print(-1)


def get_moves(n):
	if n == 1:
		return 0
	if n % 3 != 0:
		return -1
	co3 = 0
	k = n
	while k % 3 == 0:
		k = k // 3
		co3 += 1
	co2 = 0
	while k % 2 == 0:
		k = k // 2
		co2 += 1
	if k != 1:
		return -1
	if co2 > co3:
		return -1
	else:
		return co3 + co3 - co2
t = int(input())
for _ in range(t):
	n = int(input())
	print(get_moves(n))


n = int(input())
for i in range(n):
	counter = 0
	n = int(input())
	while not n == 1:
		if n % 6 == 0:
			n /= 6
			counter += 1
		else:
			n *= 2
			counter += 1
		if not n % 3 == 0 and (not n == 1):
			print(-1)
			break
	if n == 1:
		print(counter)


t = int(input())
for j in range(t):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		s2 = s3 = 0
		while n % 2 == 0:
			n //= 2
			s2 += 1
		while n % 3 == 0:
			n //= 3
			s3 += 1
		if n == 1 and s2 <= s3:
			print(2 * s3 - s2)
		else:
			print(-1)


for _ in range(int(input())):
	n = int(input())
	m = n
	c1 = 0
	c2 = 0
	ans = 0
	if n == 1:
		print(0)
		continue
	else:
		while n % 6 == 0:
			n = n // 6
			ans += 1
		while n % 3 == 0:
			n = n // 3
			c2 += 1
		while n % 2 == 0:
			n = n // 2
			c1 += 1
	if n != 1:
		print(-1)
	elif c1 > 0:
		print(-1)
	else:
		print(2 * c2 + ans)


import sys
t = int(input())
nums = []
for x in range(t):
	nums.append(int(input()))
for n in nums:
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
		continue
	else:
		b = n
		count_3 = 0
		while b % 3 == 0:
			count_3 += 1
			b /= 3
		b = n
		count_2 = 0
		while b % 2 == 0:
			count_2 += 1
			b /= 2
		if count_3 < count_2:
			print(-1)
			continue
		elif 2 ** count_2 * 3 ** count_3 != n:
			print(-1)
			continue
		print(2 * count_3 - count_2)


def math(n):
	p = 0
	q = 0
	while True:
		if n % 2 == 0:
			n = n / 2
			p = p + 1
		elif n % 3 == 0:
			n = n / 3
			q = q + 1
		else:
			break
	if n == 1 and p <= q:
		t = q - p + q
	else:
		t = -1
	return t

def main():
	arr = []
	length = int(input())
	for i in range(0, length):
		x = int(input())
		arr.append(x)
	for n in arr:
		t = math(n)
		print(t)
main()


for i in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	while 1:
		if n % 2 == 0:
			n = n / 2
			two += 1
		elif n % 3 == 0:
			n = n / 3
			three += 1
		else:
			break
	if n != 1:
		print(-1)
		continue
	elif three >= two:
		print(three * 2 - two)
	else:
		print(-1)


def solve():
	n = int(input())
	two = 0
	three = 0
	while n % 3 == 0 or n % 2 == 0:
		if n % 3 == 0:
			n = n / 3
			three += 1
		else:
			n = n / 2
			two += 1
	if n != 1 or two > three:
		print(-1)
	else:
		print(2 * three - two)
t = int(input())
while t > 0:
	solve()
	t -= 1


t = int(input())
l = list()
while 0 < t:
	i = int(input())
	l.append(i)
	t -= 1
for num in l:
	count = 0
	while num > 1:
		if num % 6 == 0:
			num = num / 6
			count = count + 1
		elif num == 1:
			break
		elif num < 1:
			count = -1
			break
		elif num % 3 == 0:
			num = num * 2
			count = count + 1
		else:
			count = -1
			break
	print(count)


for _ in range(int(input())):
	n = int(input())
	r = 0
	if n == 1:
		print('0')
	else:
		flag = 0
		while n != 1:
			if n % 6 == 0:
				n = n // 6
				r = r + 1
			else:
				a = n * 2
				if a % 6 == 0:
					n = n * 2
					r = r + 1
				else:
					print('-1')
					flag = 1
					break
		if flag != 1:
			print(r)


def generatePrimeFactors(n):
	(cnt2, cnt3) = (0, 0)
	while n % 2 == 0:
		n = n // 2
		cnt2 += 1
	while n % 3 == 0:
		n = n // 3
		cnt3 += 1
	if n != 1:
		return -1
	else:
		return {2: cnt2, 3: cnt3}
for _ in range(int(input())):
	n = int(input())
	dic = generatePrimeFactors(n)
	if dic == -1:
		print(-1)
		continue
	if dic[2] > dic[3]:
		print(-1)
		continue
	ans = dic[3] - dic[2]
	ans += dic[3]
	print(ans)


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		n = n / 2
		two += 1
	while n % 3 == 0:
		n = n / 3
		three += 1
	if n == 1:
		if two <= three:
			ans = min(two, three)
			two -= ans
			three -= ans
			ans += three * 2
			print(ans)
		else:
			print('-1')
	else:
		print('-1')


import math
for w in range(int(input())):
	n = int(input())
	x = 0
	if n == 1:
		print(0)
	else:
		if n % 6 != 0:
			x += 1
			n *= 2
		while n % 6 == 0:
			while n % 6 == 0:
				x += 1
				n = n / 6
			if n * 2 % 6 == 0:
				x += 1
				n *= 2
		print(x if n == 1 else -1)


t = input()
t = int(t)
while t > 0:
	x = input()
	x = int(x)
	po2 = 0
	po3 = 0
	while x % 2 == 0:
		po2 = po2 + 1
		x = x / 2
	while x % 3 == 0:
		po3 = po3 + 1
		x = x / 3
	ans = 0
	if x == 1:
		if po2 <= po3:
			ans = 2 * po3 - po2
		else:
			ans = -1
		print(ans)
	else:
		print('-1')
	t = t - 1


for __ in range(int(input())):
	(n, m) = (int(input()), 0)
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			m += 1
		elif n % 2 != 0:
			n *= 2
			m += 1
		else:
			n = 0
			m = -1
	print(m)


def solve(n):
	if n == 1:
		return 0
	k3 = 0
	while n % 3 == 0:
		k3 += 1
		n //= 3
	k2 = 0
	while n % 2 == 0:
		k2 += 1
		n //= 2
	if n != 1:
		return -1
	if k3 < k2:
		return -1
	return 2 * k3 - k2
t = int(input())
for _ in range(t):
	n = int(input())
	print(solve(n))


tc = int(input())
for ctr in range(tc):
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		n //= 2
		two += 1
	while n % 3 == 0:
		n //= 3
		three += 1
	if n == 1 and two <= three:
		print(2 * three - two)
	else:
		print('-1')


T = int(input())
for t in range(T):
	N = int(input())
	c = 0
	flag = 1
	while flag:
		if N == 1:
			break
		if N % 3 != 0:
			flag = 0
			break
		if N % 6 != 0:
			N *= 2
			c += 1
		N //= 6
		c += 1
	print(c if flag else -1)


t = int(input())
for _ in range(t):
	n = int(input())
	ans = 0
	if n == 1:
		print(0)
		continue
	while True:
		if n % 6 == 0:
			ans = ans + 1
			n = n // 6
			continue
		elif n % 3 == 0:
			ans = ans + 2
			n = n // 3
			continue
		else:
			break
	if n == 1:
		print(ans)
		continue
	else:
		print(-1)


import operator as op
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from functools import reduce
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def def_value():
	return 0
t = inp()
for _ in range(t):
	res = 0
	n = inp()
	while n > 1:
		if n % 6 == 0:
			n //= 6
		elif n % 3 == 0:
			n *= 2
		else:
			res = -1
			break
		res += 1
	print(res)


for _ in range(int(input())):
	x = int(input())
	t1 = 0
	t2 = 0
	while x % 2 == 0:
		x = x // 2
		t1 = t1 + 1
	while x % 3 == 0:
		x = x // 3
		t2 = t2 + 1
	if t1 > t2 or x != 1:
		print(-1)
	else:
		print(t2 - t1 + t2)


n = int(input())
res = ''
for x in range(n):
	t = int(input())
	c2 = 0
	c3 = 0
	while t % 2 == 0:
		t //= 2
		c2 += 1
	while t % 3 == 0:
		t //= 3
		c3 += 1
	if t == 1 and c2 <= c3:
		res += str(2 * c3 - c2) + '\n'
	else:
		res += '-1\n'
res = res.strip('\n')
print(res)


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	count = [0, 0]
	token = 1
	while token:
		token = 0
		if not n % 2:
			n //= 2
			count[0] += 1
			token = 1
		if not n % 3:
			n //= 3
			count[1] += 1
			token = 1
	if not n == 1 or count[1] < count[0]:
		print(-1)
	else:
		print(count[1] - count[0] + max(count))


for i in range(int(input())):
	n = int(input())
	(a, g) = (n, n)
	(a1, b1) = (0, 0)
	while a % 2 == 0:
		a = a // 2
		a1 += 1
	while g % 3 == 0:
		g = g // 3
		b1 += 1
	if b1 < a1:
		print(-1)
	elif 2 ** a1 * 3 ** b1 == n:
		print(b1 - a1 + b1)
	else:
		print(-1)


def f2(n):
	result = []
	i = 2
	while i ** 2 <= n:
		c = 0
		while n % i == 0:
			c += 1
			n //= i
		result.append([i, c])
		i += 1
	if n != 1:
		result.append([n, 1])
	return result

def f(n):
	if n == 1:
		return 0
	n_2 = 0
	n_3 = 0
	while n % 2 == 0:
		n_2 += 1
		n //= 2
	while n % 3 == 0:
		n_3 += 1
		n //= 3
	if n > 1:
		return -1
	if n_2 > n_3:
		return -1
	op_6 = n_3
	op_2 = n_3 - n_2
	return op_6 + op_2
for i in range(int(input())):
	print(f(int(input())))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def findth(x, m):
	a = 0
	while x % m == 0:
		a += 1
		x /= m
	return a

def chk(x):
	while x % 3 == 0:
		x /= 3
	while x % 2 == 0:
		x /= 2
	if x != 1:
		return 0
	return 1

def main():
	t = inp()
	for l in range(t):
		n = inp()
		t = findth(n, 2)
		h = findth(n, 3)
		if n == 1:
			print(0)
		elif h == 0 or t > h or chk(n) == 0:
			print(-1)
		else:
			print(2 * h - t)
main()


import math
from decimal import *
getcontext().prec = 30
t = int(input())
while t:
	t -= 1
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3:
		print(-1)
		continue
	if n == 6 or n == 3:
		print(6 // n)
		continue
	if n % 9:
		print(-1)
		continue
	x = 3
	six = 1
	while n % x == 0:
		x *= 3
		six *= 6
	if six % n:
		print(-1)
		continue
	ans = math.log(six // n, 2) + math.log(six, 6)
	print(int(ans))


from math import log, pow
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		num = n
		steps = 0
		flag = True
		while num > 0 and num != 1:
			if num % 6 == 0:
				steps += 1
				num //= 6
			elif num % 3 != 0:
				break
			else:
				num *= 2
				steps += 1
		if num != 1:
			print(-1)
		else:
			print(steps)


cas = int(input())

def solve():
	o = n = int(input())
	step = 0
	while o > 1:
		if o % 3 != 0:
			step = -1
			break
		if o % 6 == 0:
			o /= 6
		else:
			o *= 2
		step += 1
	print(step)
while cas > 0:
	cas -= 1
	solve()


import sys
import math
import collections
import heapq
import queue
import itertools
import functools
import operator
import time
readline = sys.stdin.readline
IPS = lambda : readline().rstrip()
IP = lambda : int(readline().rstrip())
MP = lambda : map(int, readline().split())
LS = lambda : list(map(int, readline().split()))

def solve():
	for _ in range(IP()):
		n = IP()
		(cnt, fg) = (0, 0)
		if n == 1:
			print(0)
			continue
		while n >= 6:
			if n % 6 == 0:
				n = int(n / 6)
				fg = 0
				cnt += 1
			elif fg & 1:
				break
			else:
				n *= 2
				fg = 1
				cnt += 1
		if n == 3:
			print(cnt + 2)
		elif n == 1:
			print(cnt)
		else:
			print(-1)
solve()


import math
t = int(input())
for _ in range(t):
	n = int(input())
	co2 = 0
	co3 = 0
	while n % 3 == 0:
		n = n // 3
		co3 += 1
	while n % 2 == 0:
		n = n // 2
		co2 += 1
	if co2 > co3:
		print(-1)
		continue
	if n == 1:
		print(co3 + max(0, co3 - co2))
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		res = 0
	elif n % 2 == 0 and n % 6 != 0:
		res = -1
	else:
		res = 0
		while n != 1:
			if n % 6 == 0:
				n = n // 6
			elif n % 6 == 2:
				res = -1
				break
			else:
				n = n * 2
			res += 1
	print(res)


for i in range(int(input())):
	(n, c3, c2) = (int(input()), 0, 0)
	while True:
		if not n % 3:
			c3 += 1
			n = n // 3
		elif not n % 2:
			c2 += 1
			n = n // 2
		else:
			break
	if n == 1 and c3 >= c2:
		print(c3 + c3 - c2)
	else:
		print(-1)


number_of_tests = int(input())
for test in range(number_of_tests):
	number_of_steps = 0
	n = int(input())
	while n != 1:
		if n % 6 == 0:
			n = n / 6
			number_of_steps += 1
		elif n % 3 == 0:
			n = n * 2
			number_of_steps += 1
		else:
			number_of_steps = -1
			n = 1
	print(number_of_steps)


c = int(input(''))
for i in range(0, c):
	b = int(input(''))
	ans = 0
	while b > 1:
		if b % 6 == 0:
			b = b // 6
			ans = ans + 1
		elif b % 2 != 0:
			b = b * 2
			ans = ans + 1
		else:
			b = 0
			ans = -1
	print(ans)


T = int(input().rstrip())

def solve(k):
	counter = 0
	x = k
	while True:
		if x == 1:
			return counter
		if x % 3 != 0:
			return -1
		if x % 6 == 0:
			x = x / 6
		else:
			x = 2 * x
		counter += 1
for test in range(T):
	x = int(input().rstrip())
	ans = solve(x)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	v = set()
	ct = 0
	ok = True
	while n != 1:
		if n % 6 == 0:
			n //= 6
			ct += 1
		elif n % 3 == 0:
			n *= 2
			ct += 1
		else:
			ok = False
			break
	print(ct if ok else -1)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(inputi())
	c3 = 0
	i = 3
	while i <= n and n % i == 0:
		i *= 3
		c3 += 1
	c2 = 0
	j = 2
	while j <= n and n % j == 0:
		j *= 2
		c2 += 1
	if i * j // 6 != n or c2 > c3:
		print(-1)
	else:
		print(c3 * 2 - c2)

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


import math
t = int(input())
for i in range(t):
	n = int(input())
	counter = 0
	b = True
	while n != 1:
		if n % 3 != 0:
			b = False
			break
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		counter += 1
	if b:
		print(counter)
	else:
		print(-1)


item = int(input())
for x in range(item):
	num = int(input())
	count = 0
	chk = 0
	while num > 1:
		if num % 6 == 0:
			num = num / 6
			count = count + 1
			chk = 0
		elif chk == 0:
			num *= 2
			chk = 1
			count = count + 1
		else:
			break
	if num < 1 or chk == 1:
		print(-1)
	else:
		print(count)


cnt = int(input())
for _ in range(cnt):
	sayi = int(input())
	count = 0
	if sayi >= 3:
		while sayi != 1:
			if sayi % 3 != 0:
				count = -1
				break
			elif sayi % 2 == 0:
				sayi //= 6
				count += 1
			else:
				sayi *= 2
				sayi //= 6
				count += 2
	elif sayi == 2:
		count = -1
	print(count)


import os
is_dev = 'vscode' in os.environ
if is_dev:
	inF = open('in.txt', 'r')
	outF = open('out.txt', 'w')

def ins(r):
	return list(map(int, r.split(' ')))

def inputT():
	if is_dev:
		return inF.readline()
	else:
		return input()

def printT(data=''):
	if is_dev:
		return outF.write(str(data) + '\n')
	else:
		return print(data)
from collections import Counter
import math

def primeFactors(n):
	while n % 6 == 0:
		yield 3
		yield 2
		n = n // 6
	while n % 2 == 0:
		yield 2
		n = n // 2
	while n % 3 == 0:
		yield 3
		n = n // 3
	if n > 1:
		raise ValueError('Not possible')
for _ in range(int(inputT())):
	n = int(inputT())
	if n == 1:
		printT(0)
		continue
	try:
		pf_list = list(primeFactors(n))
	except ValueError:
		printT(-1)
		continue
	pf_c = Counter(pf_list)
	if 3 not in pf_c:
		pf_c[3] = 0
	if 2 not in pf_c:
		pf_c[2] = 0
	if set(pf_c.keys()) == {3, 2} and pf_c[2] <= pf_c[3]:
		printT((pf_c[3] - pf_c[2]) * 2 + pf_c[2])
	else:
		printT(-1)
if is_dev:
	outF.close()
	print(open('out.txt', 'r').read() == open('outactual.txt', 'r').read())


t = int(input())
for e in range(t):
	x = int(input())
	cont = 0
	error = 0
	achou = False
	while True:
		if error == 2:
			break
		entrou = False
		while x % 6 == 0:
			entrou = True
			x /= 6
			cont += 1
		if x == 1:
			achou = True
			break
		if entrou:
			error = 0
		else:
			error += 1
		x *= 2
		cont += 1
	if achou:
		print(cont)
	else:
		print(-1)


def f(n, a):
	count = 0
	while n % a == 0:
		n = n // a
		count += 1
	return count

def func(n):
	a = f(n, 2)
	b = f(n, 3)
	if 2 ** a * 3 ** b == n:
		if a > b:
			return -1
		else:
			return b - a + b
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(func(n))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	ct = 0
	ans = 0
	while True:
		if n % 6 == 0:
			ans += 1
			n = n // 6
		else:
			break
	while True:
		if n % 3 == 0:
			n = n // 3
			ct += 1
		elif n == 1:
			flag = True
			break
		else:
			flag = False
			break
	if flag:
		ans += 2 * ct
	else:
		ans = -1
	print(ans)


import random

def mod_mul(a, b, n):
	result = 0
	while b > 0:
		if b & 1 > 0:
			result = (result + a) % n
		a = (a + a) % n
		b = b >> 1
	return result

def mod_exp(a, b, n):
	result = 1
	while b > 0:
		if b & 1 > 0:
			result = mod_mul(result, a, n)
		a = mod_mul(a, a, n)
		b = b >> 1
	return result

def MillerRabinPrimeCheck(n):
	if n in {2, 3, 5, 7, 11}:
		return True
	elif n == 1 or n % 2 == 0 or n % 3 == 0 or (n % 5 == 0) or (n % 7 == 0) or (n % 11 == 0):
		return False
	(k, u) = (0, n - 1)
	while not u & 1 > 0:
		k += 1
		u = u >> 1
	random.seed(0)
	s = 5
	for i in range(s):
		x = random.randint(2, n - 1)
		if x % n == 0:
			continue
		x = mod_exp(x, u, n)
		pre = x
		for j in range(k):
			x = mod_mul(x, x, n)
			if x == 1 and pre != 1 and (pre != n - 1):
				return False
			pre = x
		if x != 1:
			return False
		return True
t = int(input())

def sov(n):
	if n == 1:
		return 0
	cnt3 = 0
	cnt2 = 0
	while n % 2 == 0:
		n //= 2
		cnt2 += 1
	while n % 3 == 0:
		n //= 3
		cnt3 += 1
	if n == 1:
		if cnt2 <= cnt3:
			return cnt3 * 2 - cnt2
		else:
			return -1
	else:
		return -1
for _ in range(t):
	n = int(input())
	print(sov(n))


T_ON = 1
DEBUG_ON = 1
MOD = 998244353

def solve():
	n = read_int()
	two = three = six = 0
	while n > 1 and n % 6 == 0:
		six += 1
		n = n // 6
	while n > 1 and n % 3 == 0:
		three += 1
		n = n // 3
	if n > 1:
		print(-1)
		return
	print(six + three * 2)

def main():
	T = read_int() if T_ON else 1
	for i in range(T):
		solve()

def dbg(*xargs):
	if DEBUG_ON:
		print(*xargs)
from collections import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def read_int():
	return int(input())

def read_ints():
	return list(map(int, input().split()))

def print_nums(nums):
	print(' '.join(map(str, nums)))

def YES():
	print('YES')

def Yes():
	print('Yes')

def NO():
	print('NO')

def No():
	print('No')

def fib(n):
	(a, b) = (0, 1)
	for _ in range(n):
		(a, b) = (b, a + b)
	return a

def fib_ns(n):
	assert n >= 1
	f = [0 for _ in range(n + 1)]
	f[0] = 0
	f[1] = 1
	for i in range(2, n + 1):
		f[i] = f[i - 1] + f[i - 2]
	return f

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b, a % b)

def xgcd(a, b):
	(x0, x1, y0, y1) = (0, 1, 1, 0)
	while a != 0:
		((q, a), b) = (divmod(b, a), a)
		(y0, y1) = (y1, y0 - q * y1)
		(x0, x1) = (x1, x0 - q * x1)
	return (b, x0, y0)

def modinv(a, m):
	(g, x, _) = xgcd(a, m)
	if g != 1:
		raise Exception('gcd(a, m) != 1')
	return x % m

def mod_add(x, y):
	x += y
	while x >= MOD:
		x -= MOD
	while x < 0:
		x += MOD
	return x

def mod_mul(x, y):
	return x * y % MOD

def mod_pow(x, y):
	if y == 0:
		return 1
	if y % 2:
		return mod_mul(x, mod_pow(x, y - 1))
	p = mod_pow(x, y // 2)
	return mod_mul(p, p)

def mod_inv(y):
	return mod_pow(y, MOD - 2)

def mod_div(x, y):
	return mod_mul(x, mod_inv(y))

def is_prime(n):
	if n == 1:
		return False
	for i in range(2, int(n ** 0.5) + 1):
		if n % i:
			return False
	return True

def gen_primes(n):
	P = [True for _ in range(n + 1)]
	P[0] = P[1] = False
	for i in range(int(n ** 0.5) + 1):
		if P[i]:
			for j in range(2 * i, n + 1, i):
				P[j] = False
	return P
main()


for iota in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	three = 0
	while n % 3 == 0:
		n /= 3
		three += 1
	two = 0
	if three == 0:
		print(-1)
		continue
	while n % 2 == 0:
		n /= 2
		two += 1
	if n != 1:
		print(-1)
		continue
	if three < two:
		print(-1)
		continue
	diff = three - two
	print(three + diff)


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		ctr2 = 0
		ctr3 = 0
		while n % 2 == 0 or n % 3 == 0:
			if n % 2 == 0:
				n /= 2
				ctr2 += 1
			if n % 3 == 0:
				n /= 3
				ctr3 += 1
		if n > 1:
			print(-1)
		elif ctr2 > ctr3:
			print(-1)
		else:
			print(2 * ctr3 - ctr2)


def recur(n, acum):
	if n == 1:
		return acum
	if n % 6 == 0:
		acum += 1
		return recur(n // 6, acum)
	if n * 2 % 6 != 0:
		return -1
	else:
		acum += 1
		return recur(n * 2, acum)
t = int(input())
for i in range(t):
	a = int(input())
	print(recur(a, 0))


def count(n):
	c = 0
	while n > 0:
		if n % 3 == 0:
			n = n // 3
			c += 1
		elif n == 1:
			return 2 * c
		else:
			break
	if p(n, c) > 0:
		return 2 * c - p(n, c)
	else:
		return -1

def p(n, c):
	t = 0
	while n > 0:
		if n % 2 == 0:
			n = n // 2
			t += 1
		elif n == 1:
			if c < t:
				return -1
			else:
				return t
		else:
			return -1
for i in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 == 0:
		m = count(n)
		print(m)
	else:
		print(-1)


from math import log

def intn(flt):
	k = int(flt)
	if flt - k < 0.5:
		return k
	else:
		return k + 1
for m in range(0, int(input())):
	i = int(input())
	g = 0
	while i % 6 == 0:
		i = i // 6
		g = g + 1
	if abs(log(i, 3) - intn(log(i, 3))) < 1e-06:
		print(intn(g + 2 * log(i, 3)))
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	cnt1 = 0
	cnt2 = 0
	while True:
		if n % 2 == 0:
			n = n // 2
			cnt1 += 1
		elif n % 3 == 0:
			n = n // 3
			cnt2 += 1
		else:
			break
	if n == 1:
		if cnt2 >= cnt1:
			a = cnt2 - cnt1
			print(a + cnt2)
		else:
			print(-1)
	else:
		print(-1)


TC = int(input())

def twothree(l):
	ans = l
	(two, three, twocount, threecount) = (True, True, 0, 0)
	while ans != 1:
		(two, three) = (True, True)
		if ans % 2 == 0:
			ans = ans / 2
			twocount += 1
			two = False
		if ans % 3 == 0:
			ans = ans / 3
			threecount += 1
			three = False
		if two and three:
			break
	if ans != 1:
		return (-1, -1)
	else:
		return (twocount, threecount)

def solution(l):
	if l == 1:
		return 0
	else:
		(two, three) = twothree(l)
		if two == -1:
			return -1
		elif two > three:
			return -1
		else:
			return three - two + three
for _ in range(TC):
	l = int(input())
	print(solution(l))


def cal(k):
	if k == 1:
		return 0
	tw = 0
	th = 0
	while k % 2 == 0:
		tw += 1
		k = k // 2
	while k % 3 == 0:
		th += 1
		k = k // 3
	if k != 1 or tw > th:
		return -1
	return th - tw + th
n = int(input())
for i in range(n):
	k = int(input())
	print(cal(k))


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math
from bisect import bisect, bisect_right, bisect_left

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = get_int()
	c = 0
	f = 1
	while n > 1:
		if n % 6 == 0:
			n = n // 6
		elif n % 3 == 0:
			n = n * 2
		else:
			f = 0
			break
		c += 1
	return c if f else -1

def main():
	print(solve())
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


for iii in range(int(input())):
	n = int(input())
	dvoek = 0
	troek = 0
	tmp = n
	while tmp % 2 == 0:
		tmp = tmp // 2
		dvoek += 1
	while tmp % 3 == 0:
		tmp = tmp // 3
		troek += 1
	if tmp != 1 or dvoek > troek:
		print(-1)
	else:
		print(troek * 2 - dvoek)


t = int(input())
while t > 0:
	breaked = False
	step = 0
	n = int(input())
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			step += 1
		elif n % 3 == 0:
			n = n * 2
			step += 1
		else:
			print(-1)
			breaked = True
			break
	if not breaked:
		print(step)
	t -= 1


t = 1
t = int(input())
for _ in range(t):
	n = int(input())
	m = n
	if n == 1:
		print(0)
		continue
	ans = 0
	cnt3 = 0
	cnt2 = 0
	while n % 3 == 0:
		cnt3 += 1
		n = n // 3
		m = m // 3
	while m % 2 == 0:
		cnt2 += 1
		m = m // 2
	if cnt2 > cnt3 or m != 1:
		print(-1)
	else:
		print(2 * cnt3 - cnt2)


t = int(input())
for _ in range(t):
	n = int(input())
	(_2, _3) = (0, 0)
	while n % 2 == 0:
		_2 += 1
		n /= 2
	while n % 3 == 0:
		_3 += 1
		n /= 3
	if _2 > _3 or n > 1:
		print(-1)
	else:
		print(2 * _3 - _2)


from sys import stdin
input = stdin.readline

def main():
	test = int(input())
	for _ in range(test):
		n = int(input())
		m = n
		c = 0
		while n > 1:
			if n % 3 == 0:
				c += 1
				n = n // 3
			else:
				break
		ans = c
		while n > 1:
			if c == 0:
				break
			if n % 2 == 0:
				c -= 1
				n = n // 2
			else:
				break
		if n == 1:
			print(ans + c)
		else:
			print(-1)
main()


from sys import stdin

def inp():
	return stdin.readline().strip()
t = int(inp())
for _ in range(t):
	n = int(inp())
	if n == 1:
		print(0)
	elif n == 3:
		print(2)
	else:
		c = 0
		flag = False
		while True:
			if n == 1:
				break
			if n % 6 == 0:
				n /= 6
			elif n % 3 == 0:
				n *= 2
			else:
				print(-1)
				flag = True
				break
			c += 1
		if flag:
			continue
		else:
			print(c)


def findMoves(n):
	cnt2 = 0
	cnt3 = 0
	while n % 2 == 0:
		n /= 2
		cnt2 += 1
	while n % 3 == 0:
		n /= 3
		cnt3 += 1
	if n == 1 and cnt2 <= cnt3:
		print(2 * cnt3 - cnt2)
	else:
		print(-1)
tc = int(input())
for _ in range(0, tc):
	n = int(input())
	findMoves(n)


t = int(input())
while t > 0:
	n = int(input())
	count2 = 0
	count3 = 0
	while n % 2 == 0:
		n = n / 2
		count2 = count2 + 1
	while n % 3 == 0:
		n = n / 3
		count3 = count3 + 1
	if n != 1:
		print(-1)
	elif count2 > count3:
		print(-1)
	elif count2 == count3:
		print(count2)
	else:
		print(2 * count3 - count2)
	t = t - 1


t = int(input())
for test in range(t):
	n = int(input())
	op = 0
	flag = 0
	while n != 1:
		op += 1
		if n % 6 == 0:
			n /= 6
		elif n % 3 != 0:
			flag = 1
			break
		else:
			n *= 2
	if flag:
		print(-1)
	else:
		print(op)


def prime_factors(n):
	(twos, threes) = (0, 0)
	while n % 2 == 0:
		twos += 1
		n //= 2
	while n % 3 == 0:
		threes += 1
		n //= 3
	if threes < twos or n != 1:
		print(-1)
	elif threes == twos:
		print(threes)
	else:
		print(2 * threes - twos)
for _ in range(int(input())):
	prime_factors(int(input()))


def prime23(n):
	(x, y) = (0, 0)
	while n != 1:
		if n % 2 == 0:
			n /= 2
			x += 1
		elif n % 3 == 0:
			n /= 3
			y += 1
		else:
			return (-1, -1)
	return (x, y)
for i in range(int(input())):
	z = int(input())
	(a, b) = (prime23(z)[0], prime23(z)[1])
	if a == -1 and b == -1:
		print(-1)
	elif a > b:
		print(-1)
	else:
		print(2 * b - a)


def fact(n):
	a = n
	(d2, d3) = (0, 0)
	while a % 2 == 0:
		d2 += 1
		a /= 2
	while a % 3 == 0:
		d3 += 1
		a /= 3
	if a != 1 or d2 > d3:
		return -1
	elif d2 < d3 and a == 1:
		return d3 - d2 + d3
	elif d2 == d3 and a == 1:
		return d2
t = int(input())
for i in range(t):
	a = int(input())
	print(fact(a))


from sys import stdin, stdout
INF = 100000000000.0

def get_int():
	return int(stdin.readline().strip())

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_array():
	return list(map(int, stdin.readline().strip().split()))

def get_string():
	return stdin.readline().strip()
for _ in range(int(stdin.readline())):
	n = get_int()
	cnt = 0
	ans = 0
	while n > 1:
		ans += 1
		if n % 6 == 0:
			n = n // 6
			cnt = 0
		elif cnt < 2:
			cnt += 1
			n = n * 2
		else:
			print(-1)
			break
	else:
		print(ans)


def main():
	t = int(input())
	for i in range(t):
		x = int(input())
		ans = 0
		while x % 6 == 0:
			x = x // 6
			ans += 1
		while x % 3 == 0:
			x = x // 3
			ans += 2
		if x == 1:
			print(ans)
		else:
			print(-1)
main()


def solve():
	n = int(input())
	num3 = 0
	num2 = 0
	ans = 0
	if n == 1:
		print(0)
		return
	while n > 1:
		notdiv = False
		if n % 3 == 0:
			n //= 3
			num3 += 1
			notdiv = True
		if n % 2 == 0:
			n //= 2
			num2 += 1
			notdiv = True
		if not notdiv:
			print(-1)
			return
	if num3 < num2:
		print(-1)
		return
	while num2 < num3:
		n *= 2
		num2 += 1
		ans += 1
	ans += num3
	print(ans)
t = int(input())
for _ in range(t):
	solve()


t = int(input())

def two_three(n):
	two = 0
	three = 0
	while n != 1:
		if n % 2 == 0:
			n /= 2
			two += 1
		if n % 3 == 0:
			n /= 3
			three += 1
		elif n % 2 != 0:
			return (1, 0)
	return (two, three)
while t:
	n = int(input())
	r = 0
	(two, three) = two_three(n)
	if two > three:
		print(-1)
	else:
		print(three * 2 - two)
	t -= 1


t = int(input())
while t:
	n = int(input())
	(pow2, pow3) = (0, 0)
	while not n % 2:
		n /= 2
		pow2 += 1
	while not n % 3:
		n /= 3
		pow3 += 1
	if pow3 < pow2 or n != 1:
		print(-1)
	else:
		print(2 * pow3 - pow2)
	(pow2, pow3) = (0, 0)
	t -= 1


import io
import os
from collections import Counter, defaultdict, deque

def solve(N):
	pow3 = 0
	while N % 3 == 0:
		pow3 += 1
		N //= 3
	pow2 = 0
	while N % 2 == 0:
		pow2 += 1
		N //= 2
	if N != 1 or pow3 < pow2:
		return -1
	return pow3 - pow2 + pow3
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	ans = solve(N)
	print(ans)


num = int(input())

def judge(n):
	if n == 1:
		return 0
	if n % 3 != 0:
		return -1
	n_3 = cal_fac_num_for_m(n, 3)
	n_2 = cal_fac_num_for_m(n, 2)
	if n != 2 ** n_2 * 3 ** n_3:
		return -1
	if n_2 > n_3:
		return -1
	else:
		return 2 * n_3 - n_2

def cal_fac_num_for_m(n, m):
	flag = 0
	while n % m == 0:
		flag += 1
		n = n / m
	return flag
list_test = []
for i in range(num):
	test_n = int(input())
	list_test.append(test_n)
for i in list_test:
	print(judge(i))


t = int(input())
for tt in range(0, t):
	n = int(input())
	c = 0
	s = 0
	if n == 1:
		print(0)
	else:
		while n != 1:
			if n % 6 == 0:
				n = round(n / 6)
				c = 0
				s = s + 1
			elif n % 3 == 0:
				c = 0
				n = n * 2
				s = s + 1
			elif n % 3 != 0:
				c = 1
				break
		if c == 0:
			print(s)
		elif c == 1:
			print(-1)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def exp(n, k):
	c = 0
	while n % k == 0:
		n = n // k
		c += 1
	return (c, n)
t = int(input())
for _ in range(t):
	n = int(input())
	(twos, n) = exp(n, 2)
	(threes, n) = exp(n, 3)
	if n != 1 or twos > threes:
		print(-1)
	else:
		print(2 * threes - twos)


for _ in range(int(input())):
	y = input()
	x = [int(x) for x in y]
	n = int(y)
	count = 0
	while n > 1:
		if n % 6 != 0 and 2 * n % 6 != 0:
			count = -1
			break
		elif n % 6 == 0:
			n = n / 6
			count += 1
		elif 2 * n % 6 == 0:
			n = 2 * n
			n = n / 6
			count += 2
	print(count)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, Counter
import math

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
t = intIn()
while t > 0:
	t -= 1
	n = intIn()
	ans = 0
	while n != 1:
		f = 0
		if n % 6 == 0:
			n //= 6
			ans += 1
			f = 1
			continue
		if 2 * n % 6 == 0:
			n = n * 2
			ans += 1
			f = 1
		if ans == 0 or f == 0:
			ans = -1
			break
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	a = b = 0
	while n % 2 == 0:
		a += 1
		n = n // 2
	while n % 3 == 0:
		b += 1
		n = n // 3
	if a > b or n != 1:
		print(-1)
	else:
		print(b + (b - a))


T = int(input())
for i in range(T):
	x = int(input())
	v = x * 10
	count = 0
	while x > 1:
		if x % 6 == 0:
			x = x // 6
		elif x > v:
			break
		else:
			x = x * 2
		count += 1
	if x == 1:
		print(count)
	else:
		print(-1)


testc = int(input())
while testc > 0:
	n = int(input())
	count = 0
	count2 = 0
	count3 = 0
	while n % 6 == 0:
		n = n / 6
		count += 1
	while n % 2 == 0:
		n = n / 2
		count2 += 1
	while n % 3 == 0:
		n = n / 3
		count3 += 1
	testc -= 1
	fact1 = min(count2, count3)
	fact2 = max(count2, count3)
	if n > 1 or count2 > count3:
		print(-1)
		continue
	count += 2 * fact2 - fact1
	print(count)


b = int(input())
ls = list()
for x in range(0, b):
	a = int(input())
	count = 0
	while True:
		if a == 1:
			break
		if a % 6 == 0:
			a = a / 6
			count = count + 1
		elif count > 36:
			count = -1
			break
		else:
			a = a * 2
			count = count + 1
	ls.append(count)
for words in ls:
	print(words)


def isPower(x, y):
	if x == 1:
		return y == 1
	pow = 1
	count = 0
	while pow < y:
		pow = pow * x
		count += 1
	if pow == y:
		return count
	else:
		return -1

def isdiv(n):
	count = 0
	while n > 1:
		if n % 6 != 0:
			po = isPower(3, n)
			if po == -1:
				return -1
			else:
				return count + 2 * po
		n = n / 6
		count += 1
	return count
t = int(input())
for i in range(t):
	n = int(input())
	if n == 0:
		print(-1)
		continue
	if n % 2 != 0:
		x = isPower(3, n)
		if x == -1:
			print(-1)
		else:
			print(x * 2)
	else:
		c1 = isdiv(n)
		if c1 == -1:
			print(-1)
		else:
			print(c1)


t = int(input())
while t:
	n = int(input())
	n1 = n
	n2 = n
	k1 = 0
	k2 = 0
	while n1 / 3 == int(n1 / 3):
		n1 = n1 // 3
		k1 = k1 + 1
	while n2 / 2 == int(n2 / 2):
		n2 = n2 // 2
		k2 = k2 + 1
	if n == 1:
		print('0')
	elif 2 ** k2 * 3 ** k1 == n:
		if k1 > k2:
			print(k1 + k1 - k2)
		if k1 == k2:
			print(k1)
		if k1 < k2:
			print('-1')
	else:
		print('-1')
	t = t - 1


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def main():
	try:
		n = I()
		j = 0
		x = 0
		while n % 3 == 0:
			n = n // 3
			j += 1
		while n % 2 == 0:
			n = n // 2
			x += 1
		if n != 1:
			print(-1)
		elif j >= x:
			j += j - x
			print(j)
		else:
			print(-1)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


n = int(input())
for i in range(n):
	x = int(input())
	x1 = x
	flag = 0
	count = 0
	while x > 1:
		if x % 3 == 0:
			if x % 6 == 0:
				count += 1
				x = x // 6
			else:
				count += 2
				x = x // 3
		else:
			flag = 1
			break
	if flag == 0:
		print(count)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		count = 0
		k = n
		ans = 0
		while k % 3 == 0:
			k = k // 3
			count += 1
		ans += count * 2
		if k == 1:
			print(ans)
		else:
			cc = 0
			while k % 2 == 0:
				k = k // 2
				cc += 1
			if cc > count or k != 1:
				print(-1)
			else:
				print(ans - cc)


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = int(input())
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	n = inp()
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		threes = 0
		while n % 3 == 0:
			threes += 1
			n = n // 3
		twos = 0
		while n % 2 == 0:
			twos += 1
			n = n // 2
		if n != 1 or twos > threes:
			print(-1)
		else:
			print(threes - twos + threes)


def div_n_mul(num):
	if num == 1:
		return 0
	if num % 3 != 0:
		return -1061109567
	if num % 6 == 0:
		return 1 + div_n_mul(num // 6)
	else:
		return 1 + div_n_mul(num * 2)
N = int(input())
while N:
	n = int(input())
	result = div_n_mul(n)
	if result < 0:
		result = -1
	print(result)
	N -= 1


import math as m
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		count = 0
		while n % 3 == 0:
			if n % 6 == 0:
				n = n // 6
				count += 1
			else:
				n = n // 3
				count += 2
		if n == 1:
			print(count)
		else:
			print(-1)


N = int(input())
i = 0
while i < N:
	n = int(input())
	count = 0
	while n != 1:
		if n % 6 == 0:
			n = n / 6
		elif n % 3 == 0:
			n = n * 2
		else:
			count = -1
			break
		count += 1
	print(count)
	i += 1


import math

def process():
	n = int(input())
	(six, three) = (0, 0)
	while n % 6 == 0:
		n //= 6
		six += 1
	while n % 3 == 0:
		n //= 3
		three += 1
	if n == 1:
		print(2 * int(three) + six)
	else:
		print(-1)
for i in range(int(input())):
	process()


def divide(k, n):
	e = 0
	while k % n == 0:
		e += 1
		k = k // n
	return (k, e)

def main(k):
	(k, e_2) = divide(k, 2)
	(k, e_3) = divide(k, 3)
	if k != 1:
		ans = -1
	elif e_2 > e_3:
		ans = -1
	else:
		ans = e_3 + (e_3 - e_2)
	return ans
t = int(input())
for i in range(t):
	ans = main(int(input()))
	print(ans)


cases = int(input())
for x in range(cases):
	test = int(input())
	result = 0
	factorList = [0, 0]
	while test > 1:
		if test % 2 == 0:
			factorList[0] += 1
			test //= 2
		elif test % 3 == 0:
			factorList[1] += 1
			test //= 3
		else:
			result = -1
			break
	if result != -1:
		if factorList[0] > factorList[1]:
			result = -1
		else:
			result = 2 * factorList[1] - factorList[0]
	print(result)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def solve():
	n = int(inp())
	t = n
	ans = 0
	while t > 1:
		if t % 6 == 0:
			t = t // 6
			ans += 1
		elif t % 3 != 0:
			ans = -1
			break
		else:
			t *= 2
			ans += 1
	print(ans)
testcase(int(inp()))


n = input()
n = int(n)
c = 0
while n > 0:
	n2 = input()
	n2 = int(n2)
	while n2 > 1:
		if n2 % 3 != 0:
			c = -1
			break
		if n2 % 6 == 0:
			n2 /= 6
			c += 1
		else:
			n2 *= 2
			c += 1
	print(c)
	c = 0
	n -= 1


for _ in range(int(input())):
	num = int(input())
	if num == 1:
		print(0)
	else:
		count_2 = 0
		count_3 = 0
		while num % 2 == 0:
			num //= 2
			count_2 += 1
		while num % 3 == 0:
			num //= 3
			count_3 += 1
		if num > 1 or count_2 > count_3:
			print(-1)
		else:
			ans = 2 * count_3 - count_2
			print(ans)


import sys
import math
from sys import stdin, stdout
from collections import deque, defaultdict
from math import ceil, floor, inf, sqrt, factorial, gcd, log2
from copy import deepcopy
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007

def factor(n):
	arr = [0] * 2
	while n % 2 == 0:
		arr[0] += 1
		n //= 2
	while n % 3 == 0:
		arr[1] += 1
		n //= 3
	if n > 1:
		arr.append(n)
	return arr
for _ in range(ii1()):
	n = ii1()
	if n == 1:
		print(0)
		continue
	if n == 3:
		print(2)
		continue
	if bin(n).count('1') == 1:
		print(-1)
		continue
	arr = factor(n)
	if len(arr) == 2 and (arr[0] > 0 or arr[1] > 0):
		if arr[0] == arr[1]:
			print(arr[0])
		elif arr[0] > arr[1]:
			print(-1)
		else:
			print(arr[1] + arr[1] - arr[0])
	else:
		print(-1)


def minstepsBU(n):
	k = 0
	while n != 1:
		if n % 3 != 0:
			k = -1
			break
		if n % 6 == 0 and n % 6 != 9:
			n = n // 6
			k += 1
		elif n % 6 == 9 or n * 2 % 6 == 0:
			n = n * 2
			k += 1
	return k
for t in range(int(input())):
	n = int(input())
	print(minstepsBU(n))


def process():
	n = int(input())
	twos = 0
	trees = 0
	while n % 2 == 0:
		n /= 2
		twos += 1
	while n % 3 == 0:
		n /= 3
		trees += 1
	if n != 1:
		print(-1)
	elif twos > trees:
		print(-1)
	else:
		multiplications = trees - twos
		print(multiplications * 2 + twos)
t = int(input())
for _ in range(t):
	process()


import math
t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		two = 0
		thr = 0
		while n % math.pow(2, two) == 0:
			two += 1
		while n % math.pow(3, thr) == 0:
			thr += 1
		if two > 0:
			two -= 1
		if thr > 0:
			thr -= 1
		if n / (int(math.pow(3, thr)) * int(math.pow(2, two))) != 1.0:
			print(-1)
		elif two <= thr:
			print(thr - two + thr)
		else:
			print(-1)


for s in [*open(0)][1:]:
	n = int(s)
	i = 0
	while n % 3 < 1:
		i += 1
		n //= 3
	s = bin(n)[::-1]
	j = s.find('1')
	print((2 * i - j, -1)[i < j or '1' in s[j + 1:]])


def solve():
	x = int(input())
	ans = 0
	while True:
		if x % 6 == 0:
			ans += 1
			x //= 6
		elif x % 3 == 0:
			ans += 2
			x //= 3
		else:
			break
	if x == 1:
		print(ans)
	else:
		print(-1)
[solve() for i in range(int(input()))]


t = int(input())
for tc in range(t):
	n = int(input())
	save = n
	flag = 0
	count = 0
	while n != 1:
		while n % 6 == 0:
			n = n / 6
			count += 1
			if n == 1:
				print(count)
				flag = 1
				break
		while n % 6 != 0 and flag != 1:
			n *= 2
			count += 1
			if n > 4 * save:
				print('-1')
				flag = 1
				break
			if n == 1:
				print(count)
				flag = 1
				break
		if flag == 1:
			break
	if n == 1 and flag == 0:
		print(count)


def factor(x):
	temp = x
	t2 = 0
	t3 = 0
	while (x / 3).is_integer():
		t3 += 1
		x /= 3
	while (x / 2).is_integer():
		t2 += 1
		x /= 2
	return (x, t2, t3)
T = int(input())
for t in range(T):
	x = int(input())
	(a, b, c) = factor(x)
	if not a == 1 or b > c:
		print(-1)
	else:
		print(c + (c - b))


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
from heapq import heapify, heappop, heappush, heappushpop, heapreplace

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline().rstrip()

def lsi():
	return list(si())
res = ['YES', 'NO']
p = [1, 6, 36, 216, 1296, 7776, 46656, 279936, 1679616, 10077696, 60466176, 362797056, 2176782336, 13060694016, 78364164096, 470184984576, 2821109907456, 16926659444736, 101559956668416, 609359740010496, 3656158440062976, 21936950640377856, 131621703842267136, 789730223053602816, 4738381338321616896, 28430288029929701376, 170581728179578208256]
test_case = ii()
while test_case:
	test_case -= 1
	n = ii()
	c = 0
	i = 0
	while i < 27:
		if p[i] == n:
			break
		elif p[i] > n:
			n += n
			c += 1
		else:
			i += 1
	print(c + i if i < 27 else -1)


for _ in range(int(input())):
	n = int(input())
	no_of_2 = 0
	no_of_3 = 0
	while n % 3 == 0:
		n //= 3
		no_of_3 += 1
	while n % 2 == 0:
		n //= 2
		no_of_2 += 1
	if no_of_2 > no_of_3 or n != 1:
		print(-1)
	else:
		print(no_of_3 - no_of_2 + no_of_3)


t = int(input())
for T in range(t):
	n = int(input())
	n1 = n
	count = 0
	if n == 1:
		print(0)
		continue
	if n == 3:
		print(2)
		continue
	(c3, c2, c6) = (0, 0, 0)
	while n % 3 == 0:
		c3 += 1
		n //= 3
	n = n1
	while n % 2 == 0:
		c2 += 1
		n //= 2
	n = n1
	if c2 > c3:
		print(-1)
		continue
	ans = c3 - c2
	n *= pow(2, ans)
	c6 = 0
	while n % 6 == 0:
		n //= 6
		c6 += 1
	if n == 1:
		print(ans + c6)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())
	twos = 0
	threes = 0
	if n != 1:
		old_value = n
		while old_value % 3 == 0:
			new_value = old_value // 3
			threes += 1
			old_value = new_value
		while old_value % 2 == 0:
			new_value = old_value // 2
			twos += 1
			old_value = new_value
		if old_value == 1:
			if twos <= threes:
				ans = 2 * threes - twos
			else:
				ans = -1
		else:
			ans = -1
	else:
		ans = 0
	print(ans)


input_length = int(input())
while input_length != 0:
	numbernow = int(input())
	input_length -= 1
	numbercopy = numbernow
	if numbernow == 1:
		print(0)
		continue
	g = 0
	f = 0
	if numbercopy % 3 == 0:
		while numbercopy % 3 == 0:
			numbercopy = numbercopy // 3
			g += 1
		if numbercopy % 2 == 0:
			while numbercopy % 2 == 0:
				numbercopy = numbercopy // 2
				f += 1
		if numbercopy == 1:
			if g >= f:
				print(2 * g - f)
			else:
				print(-1)
		else:
			print(-1)
	else:
		print(-1)


def minMoves(n):
	res = 0
	while n > 1:
		if n % 6 == 0:
			(n, res) = (n // 6, res + 1)
		elif n % 2 == 0:
			(n, res) = (0, -1)
		else:
			(n, res) = (n * 2, res + 1)
	return res
t = int(input())
for i in range(t):
	n = int(input())
	print(minMoves(n))


t = int(input())
for _ in range(t):
	n = int(input())
	count2 = 0
	while n % 2 == 0:
		count2 += 1
		n = n // 2
	count3 = 0
	while n % 3 == 0:
		count3 += 1
		n = n // 3
	if n != 1 or count2 > count3:
		print(-1)
		continue
	print(count3 + count3 - count2)


test = int(input())
test_arr = []
import math

def factors(n):
	arr = {}
	flag = 0
	while n % 2 == 0:
		if str(2) in arr:
			arr['2'] = arr['2'] + 1
		else:
			arr['2'] = 1
		n = n // 2
	while n % 3 == 0:
		if str(3) in arr:
			arr[str(3)] = arr[str(3)] + 1
		else:
			arr[str(3)] = 1
		n = n // 3
	if n > 2:
		flag = 1
	return (arr, flag)
for _ in range(test):
	n = int(input())
	sol = factors(n)
	arr = sol[0]
	flag = sol[1]
	num = -1
	(count2, count3) = (0, 0)
	for (k, v) in arr.items():
		if str(2) == k:
			count2 = arr[str(2)]
		if str(3) == k:
			count3 = arr[str(3)]
	if count3 < count2:
		flag = 1
	if flag == 0:
		num = count3 - count2 + count3
	test_arr.append(num)
for t in test_arr:
	print(t)


def minStepsTo1DP(n):
	if n == 1:
		return 0
	ans2 = 9999999999
	ans3 = 9999999999
	if n % 3 == 0 and n % 6 != 0:
		ans3 = minStepsTo1DP(n * 2)
	if n % 6 == 0:
		ans2 = minStepsTo1DP(n // 6)
	return 1 + min(ans2, ans3)
t = int(input())
while t:
	n = int(input())
	ans = minStepsTo1DP(n)
	if ans == 10000000000:
		print(-1)
	else:
		print(ans)
	t -= 1


import os
from sys import stdin

def solve(tc):
	n = int(stdin.readline().strip())
	cnt = 0
	while True:
		if n == 1:
			break
		if n % 6 == 2 or n % 6 == 4:
			print(-1)
			return
		cnt += 1
		if n % 6 == 0:
			n //= 6
			continue
		else:
			n *= 2
	print(cnt)
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


t = int(input())
while t > 0:
	(ct, f) = (0, 0)
	n = int(input())
	while n != 1:
		if n % 6 == 0:
			n = n / 6
			ct = ct + 1
		elif n % 3 == 0:
			n = n * 2
			ct = ct + 1
		else:
			f = 1
			break
	if f == 1:
		print('-1')
	else:
		print(ct)
	t = t - 1


import sys
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	two = 0
	three = 0
	while n % 2 == 0:
		two += 1
		n = n // 2
	while n % 3 == 0:
		three += 1
		n = n // 3
	if n != 1 or two > three:
		print(-1)
	else:
		print(2 * three - two)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	can2 = 0
	can3 = 0
	while n % 2 == 0 and n > 1:
		n //= 2
		can2 += 1
	while n % 3 == 0 and n > 1:
		n //= 3
		can3 += 1
	if n != 1 or can2 > can3:
		print(-1)
	else:
		print(2 * can3 - can2)


for _ in range(int(input())):
	n = int(input())
	ans = 0
	if n == 1:
		print(0)
		continue
	if n == 3:
		print(2)
		continue
	if n < 6:
		print(-1)
		continue
	p = q = 0
	while n % 2 == 0:
		n = n // 2
		p += 1
	while n % 3 == 0:
		n = n // 3
		q += 1
	if n != 1 or p > q:
		print(-1)
	else:
		print(2 * q - p)


import sys
input = sys.stdin.readline

def calc(n):
	i = 0
	while n != 1:
		if n % 6 == 0:
			n = n / 6
			i += 1
		elif n % 3 == 0:
			n = n / 3
			i += 2
		else:
			return -1
	return i

def program():
	t = int(input())
	i = 0
	while i < t:
		n = int(input())
		print(calc(n))
		i += 1
program()


n = int(input())
a = []

def only_three_and_two(x):
	k3 = 0
	k2 = 0
	while x % 3 == 0:
		x //= 3
		k3 += 1
	while x % 2 == 0:
		x //= 2
		k2 += 1
	if x != 1 or k2 > k3:
		return -1
	divisions_need = k2
	k3 = k3 - k2
	divisions_need += k3 * 2
	return divisions_need
b = []
for _ in range(n):
	i = int(input())
	a.append(i)
	b.append(only_three_and_two(i))
for elem in b:
	print(elem)


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def main(x):
	cnt = 0
	while x != 1:
		if x % 6 == 0:
			x //= 6
		elif x % 3 == 0:
			x *= 2
		else:
			print(-1)
			return
		cnt += 1
	print(cnt)
N = int(input())
for i in range(N):
	x = int(input())
	main(x)


t = int(input())
for i in range(t):
	n = int(input())
	tst = n
	s = 0
	while tst > 5 and tst < 4 * n + 2:
		if tst % 6 == 0:
			tst = tst // 6
		else:
			tst = tst * 2
		s += 1
	if tst in {2, 4, 5} or tst > 6:
		s = -1
	elif tst == 3:
		s += 2
	print(s)


import math
import sys
import itertools
import itertools

def sa(Type=int):
	return [Type(x) for x in input().split()]

def solve(t):
	n = int(input())
	cc = 0
	while n > 1:
		if n % 6 == 0:
			n /= 6
		elif n % 3 == 0:
			n *= 2
		else:
			cc = -1
			break
		cc += 1
	print(cc)
t = int(input())
for i in range(t):
	solve(i + 1)


def Foo():
	n = int(input())
	m = n
	q_2 = 0
	while True:
		if m % 2 == 0:
			m = m // 2
			q_2 += 1
		else:
			break
	q_3 = 0
	while True:
		if m % 3 == 0:
			m = m // 3
			q_3 += 1
		else:
			break
	if m != 1 or q_2 > q_3:
		print(-1)
		return
	print(q_3 - q_2 + q_3)
	return

def main():
	t = int(input())
	for i in range(t):
		Foo()
main()


t = int(input())
for t1 in range(t):
	n = int(input())
	x = 0
	z = 0
	while n % 2 == 0 or n % 3 == 0:
		if n % 2 == 0:
			n /= 2
			x += 1
		else:
			n /= 3
			z += 1
	if n != 1:
		print(-1)
	elif x <= z:
		print(z - x + z)
	else:
		print(-1)


def sum(num):
	num = str(num)
	su = 0
	for i in range(len(num)):
		su = su + int(num[i])
	return su
n = int(input())
count = 0
for i in range(n):
	num = int(input())
	while num >= 1:
		if num == 1:
			print(count)
			count = 0
			break
		elif num == 2:
			print('-1')
			count = 0
			break
		elif num % 2 == 0 and num % 3 == 0:
			num = num // 6
			count += 1
		elif num % 6 != 0 and num % 4 == 0:
			print('-1')
			count = 0
			break
		else:
			num = num * 2
			count += 1


for _ in range(int(input())):
	n = int(input())
	temp = 0
	while not n % 3:
		temp += 1
		if n % 2:
			n *= 2
		else:
			n /= 6
	print([-1, temp][n == 1])


for i in range(int(input())):
	n = int(input())
	v = n
	c = 0
	while n % 6 == 0 or n % 6 == 3:
		if n % 6 == 0:
			n = n // 6
			c += 1
		else:
			n = n * 2
			c += 1
	if v == 1:
		print(0)
	elif n != 1:
		print(-1)
	else:
		print(c)


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
from collections import defaultdict as vector
from collections import deque as que
inin = lambda : int(input())
inar = lambda : list(map(int, input().split()))
from math import log
Testcase = inin()
for _ in range(Testcase):
	n = inin()
	c2 = 0
	c3 = 0
	while n % 3 == 0:
		n //= 3
		c3 += 1
	while n % 2 == 0:
		n //= 2
		c2 += 1
	if n == 1:
		if c2 > c3:
			print(-1)
		else:
			print(c3 + c3 - c2)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	(n,) = map(int, input().split())
	x = n
	s = 0
	while x != 1 and x % 3 == 0:
		if x % 6 == 0:
			x //= 6
			s += 1
		else:
			x *= 2
			s += 1
	if x == 1:
		print(s)
	else:
		print(-1)


t = int(input())
while t:
	n = int(input())
	m = 0
	f = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			m += 1
		else:
			n = 2 * n
			if n % 6 == 0:
				n = n // 6
				m += 2
			else:
				f = 1
				break
	if f == 0:
		print(m)
	else:
		print(-1)
	t = t - 1


for _ in range(int(input())):
	m = int(input())
	if m == 1:
		print(0)
		continue
	c3 = c2 = 0
	while not m % 3:
		m //= 3
		c3 += 1
	while not m % 2:
		m //= 2
		c2 += 1
	if m != 1 or c2 > c3:
		print(-1)
	else:
		print(2 * c3 - c2)


t = int(input())
for _ in range(t):
	n = int(input())
	count_2 = 0
	count_3 = 0
	while n % 2 == 0:
		n //= 2
		count_2 += 1
	while n % 3 == 0:
		n //= 3
		count_3 += 1
	if n != 1:
		print(-1)
	elif count_2 > count_3:
		print(-1)
	else:
		x = count_3 - count_2 + count_3
		print(x)


inputCnt = int(input())
for _ in range(inputCnt):
	x = int(input())
	(cntTwos, cntThrees) = (0, 0)
	while x % 2 == 0:
		cntTwos += 1
		x /= 2
	while x % 3 == 0:
		cntThrees += 1
		x /= 3
	if cntThrees >= cntTwos and x == 1:
		print(2 * cntThrees - cntTwos)
	else:
		print('-1')


def pow2(n):
	count = 0
	while n % 2 == 0:
		n = n / 2
		count += 1
	return count

def pow3(n):
	count = 0
	while n % 3 == 0:
		n = n / 3
		count += 1
	return count
for _ in range(int(input())):
	n = int(input())
	x = pow3(n)
	y = pow2(n)
	if n == 1:
		print(0)
		continue
	if x == 0:
		print(-1)
		continue
	if x < y:
		print(-1)
		continue
	if x >= y:
		n = n * pow(2, x - y)
		n = n / pow(6, x)
		if n == 1:
			print(2 * x - y)
		else:
			print(-1)


def call():
	n = int(input())
	coN = 0
	coN2 = 0
	while n % 2 == 0:
		n /= 2
		coN += 1
	while n % 3 == 0:
		n /= 3
		coN2 += 1
	if n == 1 and coN <= coN2:
		print(2 * coN2 - coN)
	else:
		print(-1)
for _ in range(int(input())):
	call()


for _ in range(int(input())):
	n = int(input())
	flag = True
	count = 0
	if n % 6 == 0 or n * 2 % 6 == 0:
		while n != 1:
			if n % 6 == 0:
				n = n // 6
				flag = True
			elif flag:
				n = n * 2
				flag = False
			else:
				print('-1')
				break
			count += 1
		if flag:
			print(count)
	elif n == 1:
		print('0')
	else:
		print('-1')


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def solve():
	n = I()
	c = 0
	a = []
	while n % 2 == 0:
		n = n // 2
		c += 1
	d = 0
	while n % 3 == 0:
		n = n // 3
		d += 1
	if c > d:
		print(-1)
	elif n != 1:
		print(-1)
	else:
		print(d - c + d)
for _ in range(I()):
	solve()


import math, string, itertools, operator, fractions, heapq, collections, re, array, bisect, sys, functools

def solve(line):
	n = int(line)
	a = [0, 0]
	while n > 1:
		if n % 2 == 0:
			n = n // 2
			a[0] += 1
		elif n % 3 == 0:
			n = n // 3
			a[1] += 1
		else:
			return -1
	if a[0] > a[1]:
		return -1
	return a[1] * 2 - a[0]
T = int(sys.stdin.readline())
t = 0
while True:
	line = sys.stdin.readline().rstrip()
	if not line:
		break
	print(solve(line))
	t += 1


from sys import *
t = int(stdin.readline())
while t > 0:
	n = int(stdin.readline())
	fg = n
	c2 = 0
	c3 = 0
	if n == 1:
		ans = 0
	elif n % 2 != 0 and n % 3 != 0:
		ans = -1
	else:
		if n % 2 == 0:
			while n % 2 == 0:
				c2 += 1
				n = n / 2
		if n % 3 == 0:
			while n % 3 == 0:
				c3 += 1
				n = n / 3
		if n == 1:
			if c3 == c2:
				ans = c2
			elif c2 > c3:
				ans = -1
			elif c3 > c2:
				ans = c3 - c2 + c3
		else:
			ans = -1
	print(ans)
	t -= 1


for t in range(int(input())):
	n = int(input())
	cnt = 0
	flag = 0
	for i in range(200):
		if n == 1:
			print(cnt)
			break
		elif n % 6 == 0:
			n = n / 6
		else:
			n = n * 2
		cnt += 1
		if n > 1e+19:
			flag = 1
			break
	if flag == 1:
		print(-1)


import sys
I = sys.stdin.readline
pr = sys.stdout.write

def main():
	for _ in range(int(I())):
		n = int(I())
		c2 = c3 = 0
		while not n & 1:
			c2 += 1
			n //= 2
		while n % 3 == 0:
			c3 += 1
			n //= 3
		if c2 <= c3 and n == 1:
			pr(f'{2 * c3 - c2}\n')
		else:
			pr('-1\n')
main()


for _ in range(int(input())):
	n = int(input())
	dic = {3: 0, 2: 0}
	while n > 1:
		if n % 3 == 0:
			n //= 3
			dic[3] += 1
		else:
			break
	while n > 1:
		if n % 2 == 0:
			n //= 2
			dic[2] += 1
		else:
			break
	if n != 1:
		print(-1)
	elif dic[2] > dic[3]:
		print(-1)
	else:
		print(dic[3] + (dic[3] - dic[2]))


import math

def primeFactors(n, d):
	d = {2: 0, 3: 0}
	while n % 6 == 0:
		d[2] += 1
		d[3] += 1
		n = n // 6
	while n % 3 == 0:
		d[3] += 1
		n = n // 3
	while n % 2 == 0:
		d[2] += 1
		n = n // 2
	if n != 1:
		return (d, -1)
	return (d, 1)
for _ in range(int(input())):
	x = primeFactors(int(input()), {})
	if x[1] == -1:
		print(-1)
	else:
		d = x[0]
		if d[2] > d[3]:
			print(-1)
		elif d[2] == d[3]:
			print(d[2])
		else:
			print(d[3] + d[3] - d[2])


t = int(input())
for case in range(t):
	n = int(input())
	moves = 0
	while n != 1:
		if n % 2 == 1:
			n *= 2
			moves += 1
		elif n % 6 == 0:
			n //= 6
			moves += 1
		else:
			break
	else:
		print(moves)
		continue
	print(-1)


import bisect
import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	for _ in range(iin()):
		n = iin()
		if n == 1:
			print(0)
		elif n % 3 != 0 and n % 6 != 0:
			print(-1)
		else:
			count = 0
			while n % 6 == 0:
				n = n // 6
				count += 1
			if 1162261467 % n != 0:
				print(-1)
			else:
				print(2 * round(math.log(n, 3)) + count)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
from math import log
fast_reader = sys.stdin.readline
fast_writer = sys.stdout.write

def input():
	return fast_reader().strip()

def print(*argv):
	fast_writer(' '.join((str(i) for i in argv)))
	fast_writer('\n')
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		temp = n
		c3 = 0
		while temp % 3 == 0:
			temp = temp // 3
			c3 += 1
		c2 = 0
		while temp % 2 == 0:
			temp = temp // 2
			c2 += 1
		if temp != 1 or c3 < c2:
			print(-1)
		else:
			print(c3 + c3 - c2)


t = int(input())
for i in range(t):
	n = int(input())
	n2 = n
	n3 = n
	count = 0
	count2 = 0
	count3 = 0
	while n2 % 2 == 0:
		count2 += 1
		n2 /= 2
	while n3 % 3 == 0:
		count3 += 1
		n3 /= 3
	if n / 2 ** count2 / 3 ** count3 != 1:
		count = -1
	elif count2 == 0 and count3 == 0 and (n != 1):
		count = -1
	elif count2 <= count3:
		count = count3 * 2 - count2
	else:
		count = -1
	print(count)


from sys import stdin, exit
input = stdin.readline

def i():
	return input()

def ii():
	return int(input())

def iis():
	return map(int, input().split())

def liis():
	return list(map(int, input().split()))

def print_array(a):
	print(' '.join(map(str, a)))
t = ii()
for _ in range(t):
	n = ii()
	count = 0
	while n != 1 and n % 3 == 0:
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		count += 1
	if n == 1:
		print(count)
	else:
		print(-1)


t = int(input())
k = t
a = list()
r = 0
while k > 0:
	s = int(input())
	while s % 6 == 0:
		s = s / 6
		r = r + 1
	while s % 3 == 0:
		s = s / 3
		r = r + 2
	if s == 1:
		a.append(r)
		k = k - 1
		r = 0
	else:
		a.append('-1')
		k = k - 1
		r = 0
for p in a:
	print(p)


T = int(input())
for t in range(T):
	n = int(input())
	if n == 1:
		print(0)
		continue
	cnt2 = 0
	cnt3 = 0
	flg = True
	while n != 1:
		if n % 2 == 0:
			cnt2 += 1
			n = n / 2
		elif n % 3 == 0:
			cnt3 += 1
			n = n / 3
		else:
			print(-1)
			flg = False
			break
	if flg:
		if cnt2 > cnt3:
			print(-1)
		else:
			print(cnt3 - cnt2 + cnt3)


t = int(input())

def bor(n):
	ct = 0
	while 1:
		if n == 1:
			break
		if n < 1:
			return -1
		if n % 6 == 0:
			n //= 6
		else:
			n *= 2
		ct += 1
	return ct
for _ in range(t):
	n = int(input())
	flag = 0
	ct = 0
	if n == 1:
		print(0)
		continue
	else:
		while n != 1:
			if n % 6 == 0:
				n //= 6
				ct += 1
			elif n % 3 == 0:
				n *= 2
				ct += 1
			else:
				flag = 1
				break
		if flag == 1:
			print(-1)
		else:
			print(ct)


t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	z = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			c = c + 1
		elif n * 2 % 6 == 0:
			n = n * 2
			c = c + 1
		else:
			z = -1
			break
	if z == 0:
		print(c)
	else:
		print(-1)


import sys

def I():
	return list(map(int, input().split()))

def sieve(n):
	a = [1] * n
	for i in range(2, n):
		if a[i]:
			for j in range(i * i, n, i):
				a[j] = 0
	return a
names = {}
done = set()
for __ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	c2 = 0
	c3 = 0
	while n % 2 == 0:
		c2 += 1
		n //= 2
	while n % 3 == 0:
		c3 += 1
		n //= 3
	if n > 1 or c2 > c3:
		print(-1)
	else:
		print(c3 - c2 + c3)


def solve():
	n = int(input())
	two = 0
	three = 0
	while n % 3 == 0:
		n = n / 3
		three += 1
	while n % 2 == 0:
		n = n / 2
		two += 1
	if n != 1:
		print(-1)
		return
	if two > three:
		print(-1)
		return
	else:
		diff = three - two
		print(three + diff)
t = int(input())
for test in range(t):
	solve()


t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	deg2 = 0
	deg3 = 0
	while n % 2 == 0:
		n = n // 2
		deg2 = deg2 + 1
	while n % 3 == 0:
		n = n // 3
		deg3 = deg3 + 1
	if deg2 > deg3 or n != 1:
		ans.append(-1)
	else:
		ans.append(2 * deg3 - deg2)
for i in ans:
	print(i)


ans = 0

def solve(n):
	global ans
	if n == 1 or n == 0:
		return
	if n % 3 != 0:
		ans = -1
		return
	if n % 2 != 0:
		ans += 1
		solve(n * 2)
		return
	ans += 1
	solve(n // 6)
for t in range(int(input())):
	ans = 0
	solve(int(input()))
	print(ans)


t = int(input(''))
strr = []
for i in range(t):
	inp = int(input(''))
	strr.append(inp)
for i in range(t):
	n = strr[i]
	if n == 1:
		print(0)
		continue
	if n == 3:
		print(2)
		continue
	cnt2 = 0
	cnt3 = 0
	flag = 1
	while flag == 1 and n != 1:
		if n % 2 == 0:
			cnt2 = cnt2 + 1
			n = n / 2
		if n % 3 == 0:
			cnt3 = cnt3 + 1
			n = n / 3
		else:
			flag = 0
	if flag == 0:
		print(-1)
		continue
	if cnt3 == 0:
		print(-1)
	elif cnt3 < cnt2:
		print(-1)
	else:
		print(cnt3 + (cnt3 - cnt2))


for i in range(int(input())):
	number = int(input())
	counter = 0
	counter_infinity = 0
	while number > 2 and counter_infinity < 2:
		residual_6 = number % 6
		if residual_6 == 0:
			number = number / 6
			counter = counter + 1
			counter_infinity = 0
		else:
			number = number * 2
			counter = counter + 1
			counter_infinity = counter_infinity + 1
	if number == 1:
		print(counter)
	else:
		print(-1)


t = int(input())
while t:
	n = int(input())
	(odd, even) = (0, 0)
	while n % 2 == 0:
		even += 1
		n = n // 2
	while n % 3 == 0:
		odd += 1
		n = n // 3
	if n == 1:
		if even > odd:
			print(-1)
		else:
			x = abs(odd - even)
			print(max(odd, even) + x)
	else:
		print(-1)
	t -= 1


for test_i in range(int(input())):
	n = int(input())
	(num2, num3) = (0, 0)
	while n != 1:
		if n % 2:
			break
		n //= 2
		num2 += 1
	while n != 1:
		if n % 3:
			break
		n //= 3
		num3 += 1
	print(-1 if n != 1 or num2 > num3 else 2 * num3 - num2)


def checker(n):
	(tmp, k) = (n, 0)
	while tmp % 6 == 0:
		k += 1
		tmp //= 6
	while tmp % 3 == 0:
		tmp //= 3
		k += 2
	return -1 if tmp > 1 else k
for _ in range(int(input())):
	print(checker(int(input())))


import math

def f(n):
	if n == 1:
		return 0
	d = n
	x = 0
	while d % 3 == 0:
		d = d // 3
		x += 1
	if x == 0:
		return -1
	d = n
	k = 0
	while d % 2 == 0:
		d = d // 2
		k += 1
	if n == 3 ** x * 2 ** k and x - k >= 0:
		return 2 * x - k
	else:
		return -1
ret = []
for i in range(int(input())):
	ret.append(f(int(input())))
for i in range(len(ret)):
	print(ret[i])


import sys
t = int(input())

def inp():
	return sys.stdin.readline().strip()
for _ in range(t):
	n = int(inp())
	steps = 0
	flag = 0
	if n == 1:
		print(0)
		continue
	while n != 1:
		if n % 3 != 0:
			print('-1')
			flag = 0
			break
		else:
			steps = steps + 1
			flag = 1
			if n % 2 == 0:
				n = n // 6
			else:
				n = n * 2
	if flag == 1:
		print(steps)


t = int(input())
import math
for i in range(t):
	n = int(input())
	three = 0
	two = 0
	up = math.ceil(math.log(n, 2))
	if n == 1:
		print(0)
		continue
	for j in range(1, up):
		if n % 2 ** j == 0:
			two = two + 1
		if n % 3 ** j == 0:
			three = three + 1
	if two > three:
		print(-1)
		continue
	if three == 0:
		print(-1)
		continue
	if n * 2 ** (three - two) / (2 ** three * 3 ** three) != 1:
		print(-1)
		continue
	else:
		print(three - two + three)


tests = int(input())
for _ in range(tests):
	n = int(input())
	cnt0 = 0
	while True:
		if n & 1 == 0:
			n >>= 1
			cnt0 += 1
		else:
			break
	cnt1 = 0
	while n > 1:
		if n % 3 == 0:
			cnt1 += 1
			n //= 3
		else:
			print(-1)
			break
	else:
		print(2 * cnt1 - cnt0 if cnt1 >= cnt0 else -1)


t = int(input())
ans = []
for i in range(t):
	n = int(input())
	three = 0
	two = 0
	while True:
		if n % 3 == 0:
			n = n // 3
			three += 1
		elif n % 2 == 0:
			n = n // 2
			two += 1
		else:
			break
	if n == 1:
		if three >= two:
			ans.append(three + three - two)
		else:
			ans.append(-1)
	else:
		ans.append(-1)
for i in ans:
	print(i)


import math
t = int(input())
for g in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print('-1')
		continue
	moves = 0
	while n % 3 == 0:
		if n % 6 != 0:
			n = n * 2
			moves += 1
		else:
			n = n // 6
			moves += 1
	if n == 1:
		print(moves)
	else:
		print('-1')


t = int(input())
while t != 0:
	n = int(input())
	a = [0, 0]
	for i in range(2, 4):
		while n % i == 0:
			n //= i
			a[i - 2] += 1
	if n != 1:
		print('-1')
	elif a[0] < a[1]:
		print(a[1] * 2 - a[0])
	elif a[0] == a[1]:
		print(a[0])
	else:
		print('-1')
	t -= 1


k = int(input())
while k:
	n = int(input())
	cnt2 = 0
	cnt3 = 0
	while n % 2 == 0:
		n /= 2
		cnt2 += 1
	while n % 3 == 0:
		n /= 3
		cnt3 += 1
	if n == 1 and cnt2 <= cnt3:
		print(cnt3 - cnt2 + cnt3)
	else:
		print(-1)
	k -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	i = 0
	temp = n
	while True:
		if temp % 6 == 0:
			i += 1
			temp = temp / 6
		elif temp % 3 == 0:
			temp = temp * 2
			i += 1
		elif temp == 1:
			print(i)
			break
		else:
			print(-1)
			break


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')

def solve(n):
	k2 = 0
	while n % 2 == 0:
		k2 += 1
		n /= 2
	k3 = 0
	while n % 3 == 0:
		k3 += 1
		n /= 3
	if n > 1:
		return -1
	if k2 > k3:
		return -1
	return k3 - k2 + k3

def main():
	for _ in range(ri()):
		n = ri()
		wi(solve(n))
main()


def isgood(n):
	twoct = 0
	threect = 0
	while n % 2 == 0:
		twoct += 1
		n /= 2
	while n % 3 == 0:
		threect += 1
		n /= 3
	return [twoct, threect, n == 1]
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	L = isgood(n)
	if not L[2]:
		ans.append(-1)
	elif L[0] > L[1]:
		ans.append(-1)
	else:
		ans.append(2 * L[1] - L[0])
for i in ans:
	print(i)


import io
import os
from sys import stdout, stdin
input = stdin.readline
for t in range(int(input())):
	n = int(input())
	count = 0
	flag = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			flag = 0
		else:
			n = n * 2
			flag += 1
		if flag == 2:
			count = -1
			break
		count += 1
	print(count)


import math

def solve():
	n = int(input())
	(p2, p3) = (0, 0)
	while n % 2 == 0:
		p2 += 1
		n //= 2
	while n % 3 == 0:
		p3 += 1
		n //= 3
	if p3 < p2:
		print(-1)
	elif n != 1:
		print(-1)
	else:
		print(p3 - p2 + p3)
t = 1
t = int(input())
for _ in range(0, t):
	solve()


for _ in range(int(input())):
	x = int(input())
	c1 = 0
	c2 = 0
	while x > 1 and (x % 2 == 0 or x % 3 == 0):
		if x % 2 == 0:
			c1 += 1
			x = x // 2
		if x % 3 == 0:
			c2 += 1
			x = x // 3
	if x == 1:
		if c2 < c1:
			print(-1)
		else:
			print(c2 - c1 + c2)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	ans = 0
	f = 1
	while True:
		while n % 6 == 0:
			n //= 6
			ans += 1
		if n == 1:
			break
		if n * 2 % 6 != 0:
			f = 0
			break
		n *= 2
		ans += 1
	if f == 0:
		ans = -1
	print(ans)


import math
b = int(input())
for i in range(b):
	v = int(input())
	c = 0
	while v != 1:
		if v % 6 == 0:
			v = v // 6
		elif v % 3 == 0:
			v = v * 2
		else:
			c = -1
			break
		c = c + 1
	print(c)


def solve(n):
	twos = 0
	threes = 0
	while n % 2 == 0:
		twos += 1
		n //= 2
	while n % 3 == 0:
		threes += 1
		n //= 3
	if n > 1:
		return -1
	if twos > threes:
		return -1
	return 2 * threes - twos
t = int(input())
for i_t in range(t):
	n = int(input())
	print(solve(n))


n = int(input())
l = []
while n:
	data = int(input())
	res = 0
	if data == 1:
		l.append(0)
	else:
		while True:
			if data % 6 == 0:
				data = data / 6
			else:
				data = data * 2
			res = res + 1
			if data > 1000000000:
				l.append(-1)
				break
			if data == 1:
				l.append(res)
				break
	n = n - 1
for i in l:
	print(i)


for i in range(int(input())):
	n = int(input())
	fact_2 = 0
	fact_3 = 0
	while n % 2 == 0 and n != 1:
		fact_2 += 1
		n = n // 2
	while n % 3 == 0 and n != 1:
		fact_3 += 1
		n = n // 3
	if n == 1:
		if fact_3 >= fact_2:
			cnt = fact_3 - fact_2
			cnt = cnt + fact_3
			print(cnt)
		else:
			print(-1)
	else:
		print(-1)


T = int(input())
while T != 0:
	n = int(input())
	div2 = 0
	div3 = 0
	while n % 2 == 0:
		n = n // 2
		div2 += 1
	while n % 3 == 0:
		n = n // 3
		div3 += 1
	if n != 1:
		print(-1)
		T -= 1
		continue
	if div2 > div3:
		print(-1)
	else:
		ans = 2 * div3 - div2
		print(ans)
	T -= 1


def one(num):
	count = 0
	while num != 1:
		if num % 6 == 0:
			num //= 6
			count += 1
		else:
			num *= 2
			count += 1
		if count > 100:
			return -1
	return count
t = int(input())
for t_itr in range(t):
	num1 = int(input())
	result = one(num1)
	print(result)


n = int(input())
for x in range(n):
	a = int(input())
	counter = 0
	while a % 6 == 0:
		a = a // 6
		counter += 1
	while a % 3 == 0:
		a = a // 3
		counter += 2
	if a == 1:
		print(counter)
	else:
		print(-1)


def solve():
	n = int(input())
	ans = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			ans += 1
		elif n % 3 == 0:
			n //= 3
			ans += 2
		else:
			ans = -1
			break
	print(ans)
t = int(input())
while t > 0:
	solve()
	t -= 1


def solve():
	x = int(input())
	counter = 0
	while x >= 1:
		if x == 1:
			print(counter)
			return
		if x % 6 == 0:
			x = x / 6
			counter += 1
		elif x % 3 == 0:
			x = x / 3
			counter += 2
		else:
			print(-1)
			return
t = int(input())
for _ in range(t):
	solve()


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
sys.setrecursionlimit(10 ** 2)

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def ri():
	return int(f.readline().strip())

def ria():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def rs():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque
import math
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m

def choose(n, k):
	if n < k:
		assert false
	return mult(factorial(n), modinv(mult(factorial(k), factorial(n - k)))) % NUMBER
from collections import deque, defaultdict
import heapq
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def dfs(g, timeIn, timeOut, depths, parents):
	cnt = 0
	stack = [[1, 0, 0, 0]]
	while stack:
		(v, neig_i, parent, depth) = stack[-1]
		parents[v] = parent
		depths[v] = depth
		if neig_i == 0:
			timeIn[v] = cnt
			cnt += 1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i += 1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u, 0, v, depth + 1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1
cnt = 0

@bootstrap
def dfs(v, adj, timeIn, timeOut, depths, parents, parent=0, depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt += 1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u, adj, timeIn, timeOut, depths, parents, v, depth + 1)
	timeOut[v] = cnt
	cnt += 1
	yield

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def get_num_2_5(n):
	twos = 0
	fives = 0
	while n > 0 and n % 2 == 0:
		n //= 2
		twos += 1
	while n > 0 and n % 5 == 0:
		n //= 5
		fives += 1
	return (twos, fives)

def solution(n):
	threes = 0
	twos = 0
	while n % 3 == 0:
		n //= 3
		threes += 1
	while n % 2 == 0:
		n //= 2
		twos += 1
	if n > 1 or twos > threes:
		return -1
	return 2 * threes - twos

def main():
	T = 1
	T = ri()
	for i in range(T):
		n = ri()
		x = solution(n)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


for test in range(int(input())):
	n = int(input())
	ans = 0
	while n != 1:
		if n % 6 == 0:
			ans += 1
			n = n / 6
		else:
			n *= 2
			ans += 1
		if n % 3 != 0 and n != 1:
			ans = -1
			n = 1
	print(ans)


T = int(input())
for i in range(T):
	N = int(input())
	moves = 0
	while N > 1:
		if N % 6 == 0:
			N /= 6
			moves += 1
		elif N % 3 == 0:
			N /= 3
			moves += 2
		else:
			moves = -1
			break
	print(moves)


from sys import stdin, stdout
import math

def primeFactors(n, arr, flgg):
	while n % 2 == 0:
		arr.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		if i != 3:
			flgg = 1
			break
		while n % i == 0:
			arr.append(i)
			n = n // i
	if n > 2:
		arr.append(n)
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	arr = []
	xx = 0
	primeFactors(n, arr, xx)
	tw = 0
	th = 0
	flg = 0
	for i in arr:
		if i == 2:
			tw += 1
		elif i == 3:
			th += 1
		else:
			flg = 1
	if not flg and tw <= th:
		print(th - tw + th)
	else:
		print(-1)


def splitup(n):
	twos = 0
	threes = 0
	while n % 2 == 0:
		n = n // 2
		twos += 1
	while n % 3 == 0:
		n = n // 3
		threes += 1
	if n == 1:
		return [twos, threes]
	return [-1, -1]

def f(n):
	(a, b) = splitup(n)
	if a == -1:
		return -1
	if a > b:
		return -1
	return 2 * b - a
numberofcases = int(input())
for _ in range(numberofcases):
	print(f(int(input())))


t = int(input())
l = []
for i in range(t):
	l.append(int(input()))
for i in range(t):
	n = 0
	if l[i] == 1:
		print(0)
	elif l[i] % 3 != 0:
		print(-1)
	else:
		while True:
			if l[i] % 6 == 0:
				l[i] /= 6
				n += 1
			if l[i] % 6 != 0 and l[i] % 3 == 0:
				l[i] *= 2
				n += 1
			if l[i] == 1:
				print(n)
				break
			if l[i] % 3 != 0:
				print(-1)
				break


for i in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	c = 0
	f = 0
	while n > 1:
		if n % 3 != 0:
			f = 1
			break
		elif n % 2 != 0 and n % 3 == 0:
			n = n * 2
			c += 1
		elif n % 2 == 0 and n % 3 == 0:
			n = n // 6
			c += 1
	if c == 0 or f == 1:
		print(-1)
	else:
		print(c)


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		c = 0
		if n == 1:
			print(0)
			continue
		if n < 6:
			n *= 2
			c += 1
		while n != 1 and (n % 6 == 0 or n * 2 % 6 == 0):
			while n % 6 == 0:
				n = n // 6
				c += 1
			if n == 1:
				break
			n = n * 2
			c += 1
		if n == 1:
			print(c)
		else:
			print(-1)
main()


def main():
	(n,) = map(int, input().split(' '))
	p2 = 0
	p3 = 0
	while n % 2 == 0:
		p2 += 1
		n //= 2
	while n % 3 == 0:
		p3 += 1
		n //= 3
	if n != 1 or p2 > p3:
		print(-1)
	else:
		print(p3 + (p3 - p2))
for _ in range(int(input())):
	main()


def sol2():
	for t in range(int(input())):
		n = int(input())
		ans = 0
		while n > 1:
			if n % 6 == 0:
				ans += 1
				n = n // 6
			elif n % 3 == 0 and n % 2 != 0:
				ans += 1
				n *= 2
			else:
				ans = -1
				break
		print(ans)
sol2()


t = input()
for i in range(int(t)):
	n = int(input())
	m = n
	a = 0
	b = 0
	while True:
		if n % 2 == 0:
			n = n / 2
			a = a + 1
		else:
			break
	n = m
	while True:
		if n % 3 == 0:
			n = n / 3
			b = b + 1
		else:
			break
	if pow(2, a) * pow(3, b) == m and a <= b:
		print(2 * b - a)
	else:
		print(-1)


t = int(input())

def recur(n, target, steps, seen):
	if n == target:
		return steps
	if n in seen:
		return -1
	if n < target:
		seen.add(n)
		return recur(n * 6, target, steps + 1, seen)
	if n > target:
		if n & 1:
			return -1
		else:
			seen.add(n)
			return recur(n // 2, target, steps + 1, seen)
for _ in range(t):
	target = int(input())
	print(recur(1, target, 0, set()))


n = int(input())
for i in range(n):
	p = int(input())
	count1 = 0
	count2 = 0
	flag = True
	while p != 1:
		if p % 2 == 0:
			p = p // 2
			count1 += 1
		elif p % 3 == 0:
			p = p // 3
			count2 += 1
		else:
			flag = False
			break
	if count2 >= count1 and flag == True:
		print(count2 * 2 - count1)
	else:
		print(-1)


from sys import stdin
input = lambda : stdin.readline().strip()
char = [chr(i) for i in range(97, 123)]
CHAR = [chr(i) for i in range(65, 91)]
mp = lambda : list(map(int, input().split()))
INT = lambda : int(input())
rn = lambda : range(INT())
from math import ceil, sqrt, factorial, gcd
for _ in rn():
	n = INT()
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		ans = 0
		while n > 1:
			if n % 6 == 0:
				ans += 1
				n = n // 6
			elif n % 3 == 0:
				ans += 2
				n = n // 3
			else:
				break
		if n == 1:
			print(ans)
		else:
			print(-1)


t = int(input())
for i in range(t):
	z = int(input())
	p2c = 0
	p3c = 0
	ans = 0
	while z != 1:
		if z % 2 == 0:
			p2c = p2c + 1
			z = z // 2
		elif z % 3 == 0:
			p3c = p3c + 1
			z = z // 3
		else:
			ans = -1
			break
	if ans == -1:
		print(ans)
	elif p3c >= p2c:
		print(2 * p3c - p2c)
	else:
		print(-1)


for _ in range(int(input())):
	a = int(input())
	(k, count) = (0, 0)
	while a > 1:
		if a % 6 == 0:
			a = a // 6
			k += 1
		elif a * 2 % 6 == 0:
			a *= 2
			k += 1
		else:
			count = 1
			break
	if count == 1:
		print('-1')
	else:
		print(k)


from collections import defaultdict
tcs = int(input())
for tc in range(tcs):
	n = int(input())
	ans = 0
	d = defaultdict(int)
	div = 2
	cant = False
	while True:
		if n == 1:
			break
		if n % div == 0:
			n //= div
			d[div] += 1
		else:
			div += 1
		if div > 6:
			cant = True
			break
	if d[2] > d[3] or len(d) > 2 or cant:
		print(-1)
	else:
		print(d[3] + d[3] - d[2])


from sys import stdin, stdout
import math
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	(t2, t3) = (0, 0)
	while n > 1 and n % 2 == 0:
		n //= 2
		t2 += 1
	while n > 1 and n % 3 == 0:
		n //= 3
		t3 += 1
	if n > 1 or t2 > t3:
		print(-1)
	else:
		print(t3 + (t3 - t2))


a = int(input())
for i in range(a):
	n = int(input())
	count = 0
	while n % 6 == 0:
		n = n // 6
		count += 1
	if n == 1:
		print(count)
		continue
	else:
		while n % 3 == 0:
			n = n // 3
			count += 2
		if n == 1:
			print(count)
		else:
			print(-1)


try:
	t = int(input())
	while t:
		n = int(input())
		ans = 0
		while n > 1:
			if n % 6 == 0:
				n = n / 6
			elif n * 2 % 6 == 0:
				n = n * 2
			else:
				break
			ans += 1
		if n == 1:
			print(ans)
		else:
			print(-1)
		t -= 1
except:
	pass


for i in range(int(input())):
	n = int(input())
	count = 0
	flag = True
	while True:
		if n == 1:
			break
		elif n == 3:
			count += 2
			break
		elif n == 2 or n == 4 or n == 5:
			print(-1)
			flag = False
			break
		elif n % 6 == 0:
			n = n / 6
			count += 1
		elif n * 2 % 6 == 0:
			n = n * 2 / 6
			count += 2
		else:
			flag = False
			print(-1)
			break
	if flag == True:
		print(count)


for _ in range(int(input())):
	n = int(input())
	kek = dict()
	kek[2] = 0
	kek[3] = 0
	i = 2
	while i < 4 and n > 1:
		if n % i == 0:
			n //= i
			kek[i] += 1
		else:
			i += 1
	if n != 1:
		print(-1)
	elif kek[2] > kek[3]:
		print(-1)
	else:
		ans = kek[3]
		ans += kek[3] - kek[2]
		print(ans)


def mb(n):
	cn2 = 0
	cn3 = 0
	while n % 2 == 0:
		cn2 += 1
		n = n / 2
	while n % 3 == 0:
		cn3 += 1
		n = n / 3
	if n == 1 and cn2 <= cn3:
		return 2 * cn3 - cn2
	else:
		return '-1'
t = int(input())
for _ in range(t):
	n = int(input())
	print(mb(n))


for i in [*open(0)][1:]:
	i = int(i)
	a = b = 0
	while i % 2 == 0:
		i /= 2
		a += 1
	while i % 3 == 0:
		i /= 3
		b += 1
	print((2 * b - a, -1)[a > b or i != 1])


t = int(input())
for i in range(t):
	n = int(input())
	moves = 0
	prev = n
	k = 0
	while n >= 1:
		if n == 1:
			break
		if n % 6 == 0:
			n //= 6
			moves += 1
		else:
			n *= 2
			moves += 1
		if moves >= 100:
			moves = -1
			break
	print(moves)


ans = ''
for _ in range(int(input())):
	n = int(input())
	cnt3 = 0
	while n % 3 == 0:
		cnt3 += 1
		n //= 3
	cnt2 = 0
	while n % 2 == 0:
		cnt2 += 1
		n //= 2
	if cnt2 > cnt3 or n > 1:
		ans += str(-1) + '\n'
	else:
		x = cnt3 - cnt2
		ans += str(x + cnt3) + '\n'
print(ans)


T = int(input())
for t in range(T):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
		continue
	power2 = power3 = 0
	while n % 2 == 0:
		n //= 2
		power2 += 1
	while n % 3 == 0:
		n //= 3
		power3 += 1
	if n != 1 or power2 > power3:
		print(-1)
		continue
	print(2 * power3 - power2)


for _ in range(int(input())):
	n = int(input())
	c = 0
	neg = 0
	while n != 1:
		if n % 6 != 0 and 2 * n % 6 != 0:
			neg = -1
			break
		if n % 6 == 0:
			n /= 6
			c += 1
		else:
			n = n * 2
			c += 1
	if neg == 0:
		print(c)
	else:
		print(-1)


t = int(input())
for case in range(t):
	num = int(input())
	exp2 = 0
	exp3 = 0
	while num % 2 == 0:
		num = num // 2
		exp2 += 1
	while num % 3 == 0:
		num = num // 3
		exp3 += 1
	if num != 1 or exp2 > exp3:
		print(-1)
	else:
		print(2 * exp3 - exp2)


t = int(input())
for i in range(0, t):
	val = int(input())
	if val % 2 == 0 and val % 6 != 0:
		print(-1)
	elif val == 1:
		print(0)
	else:
		count = 0
		while val > 1:
			if val % 6 == 0 and val != 2:
				val = val / 6
			elif val == 2:
				count = -1
				break
			elif val % 3 == 0:
				val = val * 2
			else:
				count = -1
				break
			count = count + 1
		print(count)


n = int(input())
for i in range(n):
	t = int(input())
	_2 = _3 = 0
	while t % 2 == 0:
		t //= 2
		_2 += 1
	while t % 3 == 0:
		t //= 3
		_3 += 1
	print(2 * _3 - _2 if _3 >= _2 and t == 1 else -1)


t = int(input())
for _ in range(t):
	n = int(input())
	c2 = 0
	c3 = 0
	while n % 2 == 0:
		c2 += 1
		n //= 2
	while n % 3 == 0:
		c3 += 1
		n //= 3
	if n != 1:
		print(-1)
	elif c2 > c3:
		print(-1)
	else:
		ans = c3 + (c3 - c2)
		print(ans)


from math import *
from collections import *
from bisect import *
import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	flag = 0
	(tw, thr) = (0, 0)
	k = n
	while k % 2 == 0:
		tw += 1
		k = k // 2
	while k % 3 == 0:
		thr += 1
		k = k // 3
	if k != 1 or tw > thr:
		print(-1)
	else:
		k = min(thr, tw)
		print(k + (thr - k) * 2)


t = int(input())
import math
while t > 0:
	t -= 1
	n = int(input())
	ct = 0
	flag = 0
	while n > 1:
		if n % 6 == 0:
			ct += 1
			n = n / 6
		elif n % 3 == 0:
			ct += 2
			n = n / 3
		else:
			flag = 1
			break
		if n == 1:
			break
	if flag == 1:
		print(-1)
	else:
		print(ct)


t = int(input())
l = []
for i in range(t):
	c = int(input())
	l.append(c)
for i in range(t):
	a = 0
	x = l[i]
	while True:
		if x % 6 == 0:
			x = x / 6
			a = a + 1
		elif x % 6 != 0 and x % 3 == 0:
			x = x / 3
			a = a + 2
		elif x == 1:
			print(a)
			break
		else:
			print(-1)
			break


import sys
from collections import defaultdict
name = sys.stdin.read()
jobGrid = []
isJob = True
row = []
grid = name.split('\n')
grid = grid[:-1]
number_of_cases = int(grid[0])
for i in range(number_of_cases):
	l = list(map(int, grid[i + 1].split()))[0]
	ans = 0
	multiply = 0
	while l != 1:
		if l % 6 != 0:
			if multiply == 1:
				break
			l *= 2
			multiply = 1
		else:
			l /= 6
			multiply = 0
		ans += 1
	if l != 1:
		print(-1)
	else:
		print(ans)


def ints():
	return list(map(int, input().split()))
import math
for _ in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	while n != 1:
		if n % 2 != 0 and n % 3 != 0:
			break
		if n % 2 == 0:
			n = n // 2
			two += 1
		if n % 3 == 0:
			n = n // 3
			three += 1
	if n != 1 or two > three:
		print(-1)
	else:
		print(three - two + three)


def main():
	for _ in range(int(input())):
		(n, r) = (int(input()), 0)
		while not n % 6:
			n //= 6
			r += 1
		while not n % 3:
			n //= 3
			r += 2
		print(r if n == 1 else -1)
main()


t = int(input())
for i in range(t):
	n = int(input())
	count_two = 0
	count_three = 0
	while n % 2 == 0:
		n = n // 2
		count_two += 1
	while n % 3 == 0:
		n = n // 3
		count_three += 1
	if n == 1:
		if count_three >= count_two:
			print(count_three + count_three - count_two)
		else:
			print(-1)
	else:
		print(-1)


def check(n: int) -> int:
	count = 0
	mbt = False
	while True:
		if n == 1:
			print(count)
			return
		if n % 6 != 0:
			if mbt:
				break
			n *= 2
			mbt = True
		else:
			n /= 6
			mbt = False
		count += 1
	if n == 1:
		print(count)
	else:
		print(-1)
t = int(input().strip())
for _ in range(t):
	check(int(input().strip()))


def twos(x):
	a = 0
	while 1:
		if x == 1:
			break
		if x % 2 == 0:
			x = x // 2
			a += 1
		else:
			break
	return a

def thres(y):
	b = 0
	while 1:
		if y == 1:
			break
		if y % 3 == 0:
			y = y // 3
			b += 1
		else:
			break
	return b
for _ in range(int(input())):
	n = int(input())
	x = twos(n)
	y = thres(n)
	if n / (2 ** x * 3 ** y) == 1:
		if x > y:
			print(-1)
		else:
			print(y + y - x)
	else:
		print(-1)


t = int(input())
for i in range(t):
	n = int(input())

	def twoThree(num):
		two = 0
		three = 0
		oth = 0
		while num % int(2 ** (two + 1)) == 0:
			two += 1
		while num % int(3 ** (three + 1)) == 0:
			three += 1
		num /= 2 ** two
		num /= 3 ** three
		return (two, three, oth if num == 1 else 8)
	(a, b, o) = twoThree(n)
	if a <= b and o != 8:
		print(2 * b - a)
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		op = 0
		while True:
			if n % 6 == 0:
				n = n // 6
				op += 1
			elif (n - 3) % 6 == 0:
				n = n * 2 // 6
				op += 2
			else:
				op = -1
				break
			if n == 1:
				break
		print(op)


import math
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
	else:
		r = {2: 0, 3: 0}
		while n % 2 == 0:
			r[2] += 1
			n = n // 2
		while n % 3 == 0:
			r[3] += 1
			n = n // 3
		if n != 1:
			print(-1)
		elif r[2] > r[3]:
			print(-1)
		elif r[2] == r[3]:
			print(r[2])
		else:
			print(r[3] - r[2] + r[3])


s = input()
t = int(s)
for i in range(t):
	s = input()
	n = int(s)
	x = 0
	y = 0
	while n % 3 == 0:
		n = n / 3
		x = x + 1
	while n % 2 == 0:
		n = n / 2
		y = y + 1
	if n != 1:
		print(-1)
		continue
	if x >= y:
		print(x - y + x)
	if y > x:
		print(-1)


n = int(input())
for i in range(n):
	a = int(input())
	(k, b) = (0, 0)
	c = a % 3
	while c == 0:
		a /= 3
		k += 1
		c = a % 3
	d = a % 2
	while d == 0:
		a /= 2
		b += 1
		d = a % 2
	if b > k or a != 1:
		print(-1)
	else:
		print(2 * k - b)


t = int(input())
for _ in range(t):
	n = int(input())
	x = 0
	y = 0
	while True:
		if n % 3 == 0:
			x = x + 1
			n = n // 3
		else:
			break
	while True:
		if n % 2 == 0:
			y = y + 1
			n = n // 2
		else:
			break
	if n != 1:
		print(-1)
	elif x < y:
		print(-1)
	else:
		print(x + (x - y))


def get_n(n):
	nn = n
	s = 0
	t = 0
	while nn % 3 == 0:
		s += 1
		nn = nn / 3
	while nn % 2 == 0:
		t += 1
		nn = nn / 2
	if nn != 1:
		return '-1'
	elif t > s:
		return '-1'
	else:
		return str(2 * s - t)
t = int(input())
X = ''
for i in range(t):
	X += get_n(int(input())) + '\n'
print(X[:-1])


t = int(input())
from math import ceil, log
for i in range(t):
	n = int(input())
	(f, c) = (0, 0)
	if n == 1:
		print(0)
	else:
		while n != 1:
			if n % 6 == 0:
				n //= 6
				c += 1
			elif n % 3 == 0:
				n //= 3
				c += 2
			else:
				f = 1
				print(-1)
				break
		if f == 0:
			print(c)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
for _ in range(int(input())):
	n = int(input())
	count = 0
	while n != 1:
		if n % 6 == 0:
			n = n // 6
			count += 1
		elif n % 3 == 0:
			n = n * 2
			count += 1
		else:
			break
	if n == 1:
		print(count)
	else:
		print(-1)


import os, sys
from collections import defaultdict, Counter, deque
from io import BytesIO, IOBase
from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
import math as mt
MAXN = 100001
spf = [0 for i in range(MAXN)]

def getFactorization(x):
	ret = list()
	while x != 1:
		ret.append(spf[x])
		x = x // spf[x]
	return ret

def main():
	for _ in range(int(input())):
		n = int(input())
		q = 0
		p = 0
		while n % 3 == 0:
			n = n // 3
			q += 1
		while n % 2 == 0:
			n = n // 2
			p += 1
		if n > 1:
			print(-1)
		elif q >= p:
			print(q - p + q)
		else:
			print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for i in range(t):
	k1 = 0
	k2 = 0
	n = int(input())
	while n % 3 == 0:
		k1 += 1
		n = n // 3
	while n % 2 == 0:
		k2 += 1
		n = n // 2
	if n != 1:
		print(-1)
	elif n % 5 == 0:
		print(-1)
	elif n % 7 == 0:
		print(-1)
	elif k1 < k2:
		print(-1)
	else:
		print(k1 * 2 - k2)


import sys
from collections import defaultdict as dd
from collections import deque
from fractions import Fraction as f
from copy import *
from bisect import *
from heapq import *
from math import *
from itertools import permutations

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')

def li():
	return [int(x) for x in input().split()]

def fi():
	return int(input())

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)

def bo(i):
	return ord(i) - ord('a')
t = fi()
while t > 0:
	t -= 1
	n = fi()
	c = d = 0
	while n % 2 == 0:
		n //= 2
		c += 1
	while n % 3 == 0:
		n //= 3
		d += 1
	if n != 1 or d < c:
		print(-1)
		continue
	print(d + (d - c))


def solve():
	n = int(input())
	pow2 = 0
	while n % 2 == 0:
		pow2 += 1
		n //= 2
	pow3 = 0
	while n % 3 == 0:
		pow3 += 1
		n //= 3
	if n > 1:
		return -1
	if pow2 > pow3:
		return -1
	return 2 * pow3 - pow2

def main():
	t = int(input())
	for i in range(t):
		print(solve())
main()


t = int(input())
li = []
for i in range(t):
	li.append(int(input()))
for i in li:
	counter = 0
	while i != 1:
		counter += 1
		if i % 3 != 0:
			counter = -1
			break
		elif i % 2 != 0:
			i *= 2
		elif i % 6 == 0:
			i /= 6
	print(counter)


t = int(input())
for i in range(t):
	n = int(input())
	temp = n
	k = 0
	while not temp % 2:
		temp = temp // 2
		k = k + 1
	temp1 = n
	k1 = 0
	while not temp1 % 3:
		temp1 = temp1 // 3
		k1 = k1 + 1
	if pow(3, k1) * pow(2, k) != n:
		print(-1)
	elif k1 >= k:
		print(2 * k1 - k)
	else:
		print(-1)


t = int(input())
while t > 0:
	n = int(input())
	k = n
	a = 0
	b = 0
	t -= 1
	while n % 2 == 0:
		n //= 2
		a += 1
	while n % 3 == 0:
		n //= 3
		b += 1
	if b - a < 0 or n != 1:
		print(-1)
		continue
	else:
		print(2 * b - a)


n = int(input())
ans = []
for i in range(n):
	a = int(input())
	two = 0
	thr = 0
	while a % 2 == 0:
		two += 1
		a //= 2
	while a % 3 == 0:
		thr += 1
		a //= 3
	if a != 1 or two > thr:
		ans.append(-1)
	else:
		ans.append(thr * 2 - two)
for i in ans:
	print(i)


def checkp3(x):
	v = 0
	while True:
		a = x % 3
		if a == 0:
			x //= 3
			v += 1
		else:
			return v

def checkp2(x):
	v = 0
	while True:
		a = x % 2
		if a == 0:
			x //= 2
			v += 1
		else:
			return v
listf = []
t = int(input())
for x in range(t):
	n = int(input())
	if n == 1:
		listf.append(0)
	else:
		r1 = checkp2(n)
		r2 = checkp3(n)
		a = 2 ** r1 * 3 ** r2
		if a == n:
			if r2 >= r1:
				s = 2 * r2 - r1
				listf.append(s)
			else:
				listf.append(-1)
		else:
			listf.append(-1)
for g in listf:
	print(g)


po = input()
p = int(po)
while p > 0:
	p = p - 1
	na = input()
	n = int(na)
	count = 0
	while n > 1:
		if n == 2:
			count = -1
		if n == 2:
			break
		if n % 3 != 0:
			count = -1
		if n % 3 != 0:
			break
		elif n % 6 == 0:
			n = n / 6
			count = count + 1
		else:
			n = n * 2
			count = count + 1
	print(count)


t = int(input())
while t > 0:
	n = int(input())
	(c2, c3) = (0, 0)
	result = 0
	while n % 2 == 0:
		n //= 2
		c2 += 1
	while n % 3 == 0:
		n //= 3
		c3 += 1
	if n != 1:
		print(-1)
	elif c2 <= c3:
		result += c3 - c2
		result += c3
		print(result)
	else:
		print(-1)
	t -= 1


def div(n, c):
	if n == 1:
		return c
	elif n % 6 == 0:
		n = n // 6
		c += 1
		return div(n, c)
	elif n % 3 == 0 and n % 6 != 0:
		n = n * 2
		c += 1
		return div(n, c)
	else:
		return -1
t = int(input())
for i in range(t):
	c = 0
	n = int(input())
	s = div(n, c)
	print(s)


t = int(input())
for _ in range(t):
	n = int(input())
	(threes, twos) = (0, 0)
	while 1:
		if n % 3 == 0:
			threes += 1
			n = n // 3
		elif n % 2 == 0:
			twos += 1
			n = n // 2
		else:
			break
	if twos > threes or n != 1:
		print(-1)
	else:
		print(threes + threes - twos)


T = int(input())

def by_six(n):
	if n % 6 == 0:
		return True
	else:
		return False

def by_three(n):
	if n % 3 == 0:
		return True
	else:
		return False

def main():
	n = int(input())
	if n == 1:
		return 0
	if n == 2:
		return -1
	count = 0
	while n > 2:
		if by_six(n):
			n = n // 6
			count = count + 1
			continue
		if by_three(n):
			n = n * 2
			n = n // 6
			count = count + 2
			continue
		else:
			return -1
	if n == 2:
		return -1
	else:
		return count
for i in range(T):
	print(main())


t = int(input())
for p in range(t):
	n = int(input())
	i = 0
	ans = True
	while True:
		if n == 1:
			break
		if n % 6 == 0:
			i += 1
			n //= 6
		elif n % 3 == 0:
			i += 2
			n //= 3
		else:
			ans = False
			break
	if ans:
		print(i)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	(k2, k3) = (0, 0)
	while n % 2 == 0:
		k2 += 1
		n //= 2
	while n % 3 == 0:
		k3 += 1
		n //= 3
	if k2 > k3 or n != 1:
		print(-1)
	else:
		print(k3 - k2 + k3)


for _t in range(int(input())):
	n = int(input())
	a = 0
	h = False
	while n > 1 and n % 3 == 0:
		h = True
		if n % 6 == 0:
			n = n // 6
		else:
			n = n * 2
		a += 1
	if n != 1:
		print(-1)
	else:
		print(a)


import math
from sys import stdin, stdout

def input():
	return stdin.readline()

def prin(x):
	return stdout.write(str(x) + '\n')
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
	else:
		(two, three) = (0, 0)
		while n % 2 == 0:
			two += 1
			n = n // 2
		while n % 3 == 0:
			three += 1
			n = n // 3
		if n - 1 != 0 or two > three:
			print(-1)
		elif three == two:
			print(two)
		else:
			x = two
			print(x + (three - two) * 2)


from math import *

def pow3(n):
	c = 0
	while n > 0:
		if n % 3 == 0:
			c += 1
			n //= 3
		else:
			break
	return c

def pow2(n):
	if n & 1 == 1:
		return 0
	c = 0
	while n > 0:
		if n % 2 == 0:
			c += 1
			n //= 2
		else:
			break
	return c

def f(n):
	p3 = pow3(n)
	n //= 3 ** p3
	p2 = pow2(n)
	if p3 < p2:
		return -1
	n //= 2 ** p2
	if n == 1:
		return p3 + p3 - p2
	return -1
t = int(input())
for _ in range(t):
	n = int(input())
	print(f(n))


cases = int(input())
power6 = []
for p in range(11):
	pos = 6 ** p
	power6.append(pos)
for i in range(cases):
	num = int(input())
	if num in power6:
		print(power6.index(num))
	else:
		count2 = 0
		while num % 2 == 0:
			num = int(num / 2)
			count2 += 1
		count3 = 0
		while num % 3 == 0:
			num = int(num / 3)
			count3 += 1
		if num != 1 or count3 == 0 or count3 < count2:
			print(-1)
		else:
			print(count3 - count2 + count3)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
		continue
	ans = 0
	flag = 0
	while n != 1:
		if n % 6 == 0:
			ans += 1
			n //= 6
		elif n % 3 == 0:
			n *= 2
			ans += 1
		elif n % 3 != 0:
			flag = 1
			break
	if flag == 1:
		ans = -1
	print(ans)


N = int(input())
for x in range(N):
	ans = int(input())
	count = 0
	count1 = 0
	while ans // 2 == ans / 2:
		ans = ans / 2
		count += 1
	while ans / 3 == ans // 3:
		ans = ans / 3
		count1 += 1
	if ans != 1:
		print(-1)
	elif count > count1:
		print(-1)
	else:
		print(min(count1, count) + 2 * (count1 - min(count1, count)))


rr = lambda : input()
rri = lambda : int(input())
rrm = lambda : list(map(int, input().split()))
INF = float('inf')

def solve(N):
	if N == 1:
		return 0
	twos = 0
	threes = 0
	while N % 2 == 0:
		N //= 2
		twos += 1
	while N % 3 == 0:
		N //= 3
		threes += 1
	if N == 1 and threes > 0 and (threes >= twos):
		return threes + threes - twos
	else:
		return -1
t = rri()
for _ in range(t):
	n = rri()
	print(solve(n))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	(cnt2, cnt3) = (0, 0)
	if n == 1:
		print(0)
		continue
	k = n
	while k > 1:
		if k % 2 == 0:
			cnt2 += 1
			k //= 2
		else:
			break
	while k > 1:
		if k % 3 == 0:
			cnt3 += 1
			k //= 3
		else:
			break
	if k != 1:
		print(-1)
		continue
	if cnt3 < cnt2:
		print(-1)
		continue
	print(cnt3 * 2 - cnt2)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

def inputnums():
	return map(int, input().split())

def inputlist():
	return list(map(int, input().split()))

def inputstring():
	return [x for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	n = inputnum()
	two = 0
	three = 0
	while n % 2 == 0 or n % 3 == 0:
		if n % 2 == 0:
			n //= 2
			two += 1
		if n % 3 == 0:
			n //= 3
			three += 1
	if n > 1 or two > three:
		print(-1)
	else:
		print(2 * three - two)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	c = 0
	while n % 6 == 0:
		c += 1
		n //= 6
	while n % 3 == 0:
		c += 2
		n //= 3
	if n == 1:
		print(c)
	else:
		print(-1)


for _ in range(int(input())):
	y = int(input())
	if y == 1:
		print(0)
	else:
		f = 1
		count = 0
		while y != 1:
			if y % 2 == 0 and y % 3 == 0:
				y = y // 6
				count += 1
			elif y % 3 == 0:
				y = y * 2
				count += 1
			else:
				print(-1)
				f = 0
				break
		if f == 1:
			print(count)


t = int(input())
for i in range(t):
	n = int(input())
	cnt = 0
	flag = 0
	flag1 = 0
	if n == 1:
		print(0)
		continue
	while 1:
		if n == 1:
			break
		if n % 6 == 0:
			n = n // 6
			if n == 1:
				break
			cnt = cnt + 1
		elif n * 2 % 6 == 0:
			n = n * 2
			cnt = cnt + 1
		else:
			print(-1)
			flag = 1
			break
	if flag == 0:
		print(cnt + 1)


from __future__ import division, print_function
import os, sys
import math
import collections
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[1]

def sort2(l):
	return sorted(l, key=getKey, reverse=True)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r))

def binary_search(arr, low, high, x):
	if high >= low:
		mid = (high + low) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binary_search(arr, low, mid - 1, x)
		else:
			return binary_search(arr, mid + 1, high, x)
	else:
		return -1

def ceil(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def padded_bin_with_complement(x):
	if x < 0:
		return bin(2 ** 16 - abs(x))[2:].zfill(16)
	else:
		return bin(x)[2:].zfill(16)

def binaryToDecimal(binary):
	binary1 = binary
	(decimal, i, n) = (0, 0, 0)
	while binary != 0:
		dec = binary % 10
		decimal = decimal + dec * pow(2, i)
		binary = binary // 10
		i += 1
	print(decimal)

def CountFrequency(arr):
	return collections.Counter(arr)

def pos(a):
	b = [0] * len(a)
	c = sorted(a)
	for i in range(len(a)):
		for j in range(len(a)):
			if c[j] == a[i]:
				b[i] = j
				break
	return b

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n

def commonn(a, b, n):
	c = []
	for i in range(n):
		if a[i] == b[i]:
			c.append('-1')
		else:
			c.append(b[i])
	return c

def primeFactors(n):
	j = []
	while n % 2 == 0:
		j.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			j.append(int(n))
			n = n / i
	if n > 2:
		j.append(int(n))
	return j

def main():
	for _ in range(ii()):
		n = ii()
		cou = 0
		f = 0
		for i in range(n):
			if n == 2 or n == 1:
				if n == 2:
					f = 1
				break
			if n % 6 == 0:
				n = n // 6
			elif n % 6 == 3:
				n = n * 2
			elif n % 6 != 3 or n % 6 != 0:
				f = 1
				break
			cou += 1
		if f:
			print(-1)
		else:
			print(cou)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())
for h in range(t):
	n = int(input())
	res = 0
	e = False
	while n != 1:
		if n % 6 == 0:
			n /= 6
			res += 1
		elif n * 2 % 6 == 0:
			n *= 2
			res += 1
		else:
			e = True
			break
	if e:
		print('-1')
	else:
		print(res)


test = int(input())
for i in range(test):
	n = int(input())
	c = 0
	s = 0
	while n > 1:
		if n % 6 == 0:
			n //= 6
			s = 0
		elif s == 0:
			n *= 2
			s = 1
		else:
			print(-1)
			break
		c += 1
	if n == 1:
		print(c)


import sys

def fac(n):
	if n == 1:
		return 0
	elif n == 2:
		return -1
	elif n == 3:
		return 2
	arr = [0, 0]
	while n % 2 == 0:
		arr[0] += 1
		n //= 2
	while n % 3 == 0:
		arr[1] += 1
		n //= 3
	if n > 1:
		return -1
	elif arr[0] > arr[1]:
		return -1
	return arr[1] + (arr[1] - arr[0])
t = int(sys.stdin.readline())
ans_arr = []
for x in range(t):
	n = int(sys.stdin.readline())
	ans_arr.append(str(fac(n)))
print('\n'.join(ans_arr))


t = int(input())
for i in range(t):
	two = 0
	three = 0
	n = int(input())
	if n == 1:
		print(0)
		continue
	while True:
		if n % 2 == 0:
			two += 1
			n = n // 2
		else:
			break
	while True:
		if n % 3 == 0:
			three += 1
			n = n // 3
		else:
			break
	if two == 0 and three == 0 or two > three or n != 1:
		print(-1)
	else:
		print(three * 2 - two)


n = int(input())
for i in range(n):
	p = int(input())
	count = 0
	while p != 1 and p % 3 == 0:
		if p % 6 == 0:
			p = p // 6
			count += 1
		else:
			p = p * 2
			count += 1
	if p != 1:
		print(-1)
	else:
		print(count)


for _ in range(int(input())):
	n = int(input())
	two = 0
	three = 0
	flag = True
	while n > 1:
		if n % 2 == 0:
			two += 1
			n = n // 2
		elif n % 3 == 0:
			three += 1
			n = n // 3
		else:
			flag = False
			break
	if flag:
		z = three - two
		if z < 0:
			print(-1)
		else:
			print(z + three)
	else:
		print(-1)


t = int(input())
while t:
	n = int(input())
	c = 0
	while n != 1:
		if n % 3 != 0:
			c = -1
			break
		if n % 6 == 0:
			n /= 6
			c += 1
			continue
		if n % 3 == 0:
			n *= 2
			c += 1
			continue
	print(c)
	t -= 1


def solve():
	n = int(input())
	if n == 1:
		return 0
	ans = 0
	while n > 1:
		r = n % 6
		if r:
			if n * 2 % 6 != 0:
				return -1
			else:
				ans += 1
				n *= 2
		n = n // 6
		ans += 1
	return ans

def main():
	t = int(input())
	while t:
		print(solve())
		t -= 1
main()


t = int(input())

def pow_3(n):
	c = 0
	while type(n) == int:
		if n % 3 == 0:
			n = n // 3
			c += 1
		else:
			n = n / 3
	return c

def pow_2(n):
	c = 0
	while type(n) == int:
		if n % 2 == 0:
			n = n // 2
			c += 1
		else:
			n = n / 2
	return c
for _ in range(t):
	n = int(input())
	c1 = pow_3(n)
	c2 = pow_2(n)
	if c1 < c2:
		print('-1')
	else:
		d = c1 - c2
		if n == 3 ** c1 * 2 ** c2:
			print(d + c1)
		else:
			print('-1')


t = int(input())
while t > 0:
	n = int(input())
	count = 0
	flag = 0
	while n > 1:
		if n % 6 == 0:
			n = int(n / 6)
			count += 1
		else:
			n *= 2
			if n % 6 != 0:
				flag = 1
				break
			else:
				n = int(n / 6)
				count += 2
	if flag == 0:
		print(count)
	else:
		print(-1)
	t -= 1


a = int(input())
for i in range(a):
	x = int(input())
	c1 = 0
	c2 = 0
	while x % 2 == 0:
		x = x / 2
		c1 = c1 + 1
	while x % 3 == 0:
		x = x / 3
		c2 = c2 + 1
	if x == 1 and c1 <= c2:
		print(2 * c2 - c1)
	else:
		print(-1)


a = int(input())
i = 0
while a > i:
	n = int(input())
	tec = 1
	ind = 1
	kol = 0
	if n == 1:
		print(0)
	elif n % 3 != 0:
		print(-1)
	else:
		while ind == 1:
			if n % 6 == 0:
				n = n // 6
				kol += 1
			elif n % 3 == 0:
				n = n // 3
				kol += 2
			elif n == 1:
				print(kol)
				ind = 0
			else:
				ind = 0
				print(-1)
	i += 1


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return len(set(factors))

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def printp(p):
	for i in p:
		print(i)
MOD = 10 ** 9 + 7
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	flag = True
	ans = 0
	while flag:
		if n == 1:
			break
		elif n % 6 == 0:
			n = n // 6
			ans += 1
		elif n % 3 == 0:
			n = n * 2
			ans += 1
		else:
			flag = False
			break
	if flag == False:
		print(-1)
	else:
		print(ans)


t = int(input())
for i in range(t):
	x = [int(i) for i in input().split(' ')][0]
	n = 0
	while True:
		if x == 1:
			print(n)
			break
		if x % 3 != 0:
			print(-1)
			break
		if x % 6 == 0:
			x /= 6
		else:
			x *= 2
		n += 1


tests = int(input())
for i in range(tests):
	n = int(input())
	if n == 1:
		print(0)
		continue
	if n % 3 != 0:
		print(-1)
		continue
	exp = n
	c3 = 0
	c2 = 0
	while exp % 3 == 0:
		exp //= 3
		c3 += 1
	while exp % 2 == 0:
		exp //= 2
		c2 += 1
	if exp != 1:
		print(-1)
	elif c2 > c3:
		print(-1)
	else:
		print(c3 - c2 + c3)


List = []
T = int(input())
for _ in range(T):
	N = int(input())
	if N == 1:
		List.append(0)
	elif N != 1:
		Count = 0
		while N != 1:
			if N % 6 == 0 and N != 1:
				N /= 6
				Count += 1
			elif N % 6 != 0 and N != 1:
				N *= 2
				Count += 1
				if N % 6 == 0:
					N /= 6
					Count += 1
				else:
					N *= 2
					List.append(-1)
					break
			if N == 1:
				List.append(Count)
for Content in List:
	print(Content)


import sys
input = sys.stdin.buffer.readline
t = int(input())
while t:
	t -= 1
	n = int(input())
	c6 = 0
	c3 = 0
	while n % 6 == 0:
		n = n / 6
		c6 += 1
	while n % 3 == 0:
		n = n / 3
		c3 += 1
	if n == 1:
		print(c6 + 2 * c3)
	else:
		print(-1)


n = int(input())
while n:
	n -= 1
	(q1, q2) = (0, 0)
	a = int(input())
	while a % 3 == 0:
		a //= 3
		q1 += 1
	while a % 2 == 0:
		a //= 2
		q2 += 1
	if a != 1:
		print(-1)
	elif q1 < q2:
		print(-1)
	else:
		print(2 * q1 - q2)


test = int(input())
while test:
	test -= 1
	n = int(input())
	count = 0
	if n < 6 and n != 1:
		if n == 3:
			print(2)
		else:
			print(-1)
	else:
		while n % 6 == 0:
			n = n / 6
			count += 1
		while n % 3 == 0:
			n = n / 3
			count += 2
		if n == 1:
			print(count)
		else:
			print(-1)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	ans = 0
	while N % 6 == 0:
		N //= 6
		ans += 1
	while N % 2 == 1 and N % 3 == 0:
		N *= 2
		N //= 6
		ans += 2
	if N == 1:
		print(ans)
	else:
		print(-1)


import math

def dtb(n):
	return bin(n).replace('0b', '')

def btd(n):
	return int(n, 2)
t = int(input())
for k in range(t):
	n = int(input())
	a = []
	flag = True
	(count1, count2) = (0, 0)
	if n == 1:
		print(0)
		continue
	while n != 1:
		if n % 2 == 0:
			count1 += 1
			n = n // 2
		elif n % 3 == 0:
			count2 += 1
			n = n // 3
		else:
			flag = False
			break
	if flag == False:
		print('-1')
	elif count2 < count1:
		print(-1)
	elif count1 == count2:
		print(count1)
	elif count2 > count1:
		ans = count2 - count1
		ans = ans + count2
		print(ans)


query = int(input())
for _ in range(query):
	n = int(input())
	step = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			step += 1
		elif n % 3 == 0:
			n //= 3
			step += 2
		else:
			break
	if n != 1:
		print(-1)
	else:
		print(step)


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(0)
		continue
	n_3 = n_2 = 0
	while n % 2 == 0:
		n_2 += 1
		n /= 2
	while n % 3 == 0:
		n_3 += 1
		n /= 3
	if n == 1 and n_3 >= n_2:
		print(2 * n_3 - n_2)
	else:
		print(-1)


import sys
from bisect import bisect_left
from collections import defaultdict
input = lambda : sys.stdin.readline().rstrip()
R = lambda : map(int, input().split())
(T,) = R()
for _ in range(T):
	(n,) = R()
	c2 = 0
	c3 = 0
	while n % 2 == 0:
		c2 += 1
		n /= 2
	while n % 3 == 0:
		c3 += 1
		n /= 3
	if n != 1 or c3 < c2:
		print(-1)
	else:
		print(2 * c3 - c2)


counter = int(input())
i = 0
data = []
while i < counter:
	data.append(int(input()))
	i += 1
for i in data:
	if i == 1:
		print('0')
		continue
	if i == 2:
		print('-1')
		continue
	counter = 0
	if i % 6 != 0 and i % 3 != 0:
		print('-1')
		continue
	while True:
		if i % 6 != 0 and i % 3 == 0:
			i *= 2
			counter += 1
			continue
		elif i % 6 == 0:
			i = i / 6
			counter += 1
			continue
		elif i == 1:
			print(counter)
			break
		elif i % 6 != 0 and i % 3 != 0:
			print('-1')
			break


for _ in range(int(input())):
	n = int(input())
	p2 = 0
	p3 = 0
	while n % 2 == 0 or n % 3 == 0:
		if n % 2 == 0:
			n //= 2
			p2 += 1
		if n % 3 == 0:
			n //= 3
			p3 += 1
	if n != 1 or p2 > p3:
		print(-1)
		continue
	ans = p3 - p2 + p3
	print(ans)


t = int(input())
while t:
	t = t - 1
	x = int(input())
	to = 0
	th = 0
	step = 0
	while (x % 2 == 0 or x % 3 == 0) and x:
		if x % 2 == 0:
			to = to + 1
			x = x / 2
		if x % 3 == 0:
			th = th + 1
			x = x / 3
	if x != 1:
		print(-1)
	else:
		v = min(to, th)
		step = step + v
		to = to - v
		th = th - v
		if to:
			print(-1)
		else:
			step = step + th * 2
			print(step)


number = int(input())
for iTask in range(number):
	n = int(input())
	if n == 1:
		print(0)
	elif n == 2:
		print(-1)
	elif n == 3:
		print(2)
	else:
		pow3 = 0
		divisible = n % 3
		while divisible == 0 and n > 2:
			n = int(n / 3)
			pow3 += 1
			divisible = n % 3
		pow2 = 0
		divisible = n % 2
		while divisible == 0 and n > 1:
			n = int(n / 2)
			pow2 += 1
			divisible = n % 2
		if n == 1 and pow3 >= pow2:
			print(pow3 - pow2 + pow3)
		else:
			print(-1)


def main():
	t = int(input())
	for g in range(t):
		count_2 = 0
		count_3 = 0
		n = int(input())
		while n % 2 == 0:
			n //= 2
			count_2 += 1
		while n % 3 == 0:
			n //= 3
			count_3 += 1
		if n > 1:
			print(-1)
		elif count_3 < count_2:
			print(-1)
		else:
			print(count_2 + (count_3 - count_2) * 2)
main()


a = int(input())
for i in range(0, a):
	n = int(input())
	tow = 0
	b = n
	three = 0
	while n >= 1:
		if n % 3 == 0:
			three = three + 1
			n = n // 3
		elif n % 2 == 0:
			tow = tow + 1
			n = n // 2
		else:
			break
	if n == 1:
		if b == 1:
			print(0)
		elif three >= 1:
			if tow > three:
				print(-1)
			else:
				print(tow + 2 * (three - tow))
		else:
			print(-1)
	else:
		print(-1)


def sieve(n):
	p2 = 0
	p3 = 0
	while n > 1 and n % 2 == 0:
		n = n // 2
		p2 += 1
	while n > 1 and n % 3 == 0:
		n = n // 3
		p3 += 1
	return (p2, p3, n)
test = int(input())
for _ in range(test):
	n = int(input())
	if n == 1:
		print(0)
	elif n == 2:
		print(-1)
	else:
		(p2, p3, n) = sieve(n)
		if n > 1 or p2 > p3:
			print(-1)
		else:
			print(p3 + (p3 - p2))


t = int(input())
x = []
for i in range(t):
	xt = int(input())
	x.append(xt)
for i in range(len(x)):
	m = x[i]
	count = 0
	while True:
		if m == 1:
			print(count)
			break
		if m % 2 == 0 and m % 6 != 0:
			print('-1')
			break
		elif m % 6 == 0:
			m = m / 6
		else:
			m = m * 2
		count += 1


for i in range(int(input())):
	n = int(input())
	x2 = 0
	x3 = 0
	while n % 2 == 0:
		x2 += 1
		n = n // 2
	while n % 3 == 0:
		n = n // 3
		x3 += 1
	if n != 1:
		print(-1)
	elif x2 > x3:
		print(-1)
	else:
		print(2 * x3 - x2)


for _ in range(int(input())):
	x = int(input())
	flag = False
	counter = 0
	while True:
		if x == 1:
			flag = True
			break
		if x % 6 == 0:
			x = x // 6
			counter += 1
		elif x % 2 == 0:
			break
		else:
			x *= 2
			counter += 1
	print('-1' if flag == False else counter)


import math
for _ in range(int(input())):
	n = int(input())
	count = 0
	temp = n
	two = 0
	while temp % 2 == 0:
		two += 1
		temp //= 2
	temp = n
	three = 0
	while temp % 3 == 0:
		three += 1
		temp //= 3
	if n == 1:
		print(0)
		continue
	if three < two or 2 ** two * 3 ** three != n:
		print(-1)
		continue
	count += three
	count += three - two
	print(count)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	c = 0
	while n != 1:
		if n % 6 == 0:
			n //= 6
			c += 1
		elif n % 3 == 0:
			n //= 3
			c += 2
		else:
			break
	if 1 == n:
		print(c)
	else:
		print(-1)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
for _ in range(int(input())):
	n = int(input())
	ans = 0
	flag = 0
	while n != 1:
		if n % 6 == 2:
			flag = 1
			print(-1)
			break
		if n % 6 == 0:
			n //= 6
			ans += 1
		else:
			n *= 2
			ans += 1
	if not flag:
		print(ans)


from math import *
from collections import deque
from copy import deepcopy
import sys

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def multi():
	return map(int, input().split())

def strmulti():
	return map(str, inp().split())

def lis():
	return list(map(int, inp().split()))

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def stringlis():
	return list(map(str, inp().split()))

def out(var):
	sys.stdout.write(str(var))

def printlist(a):
	print(' '.join((str(a[i]) for i in range(len(a)))))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
t = int(input())
for j in range(t):
	n = int(input())
	num = 0
	num2 = 0
	while n % 2 == 0:
		n /= 2
		num += 1
	while n % 3 == 0:
		n /= 3
		num2 += 1
	if num <= num2 and n == 1:
		print(2 * num2 - num)
	else:
		print(-1)


from sys import stdin, stdout
import math
input = stdin.readline

def print(x='', y='', end='\n'):
	if y == '':
		stdout.write(str(x) + end)
	else:
		stdout.write(str(x) + ' ' + str(y) + end)

def solve():
	n = int(input())
	if n == 1:
		print(0)
		return
	(cnt3, cnt2) = (0, 0)
	p = n
	c = 0
	while n % 6 == 0:
		n //= 6
		c += 1
	if n == 1:
		print(c)
		return
	p = n
	while n % 3 == 0:
		n //= 3
		cnt3 += 1
	if cnt3 == 0:
		print(-1)
		return
	x = 2 ** cnt3 * p
	if 6 ** int(math.log(x, 6)) != x:
		print(-1)
		return
	print(cnt3 * 2 + c)
tt = 1
tt = int(input())
for __ in range(tt):
	solve()


t = int(input())
for p in range(0, t):
	m = 0
	n = int(input())
	while n != 1 or n != 0:
		if n % 6 == 0:
			n //= 6
			m += 1
		elif n * 2 % 6 == 0:
			n *= 2
			m += 1
		else:
			break
	if n == 1:
		print(m)
	else:
		print(-1)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def valid(row, col, rows, cols, rcross, lcross):
	return rows[row] == 0 and cols[col] == 0 and (rcross[col + row] == 0) and (lcross[col - row] == 0)

def div(n):
	tmp = []
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			tmp.append(i)
			if i != n // i:
				tmp.append(n // i)
	return tmp

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def main():
	ans = ''
	for _ in range(ii()):
		n = ii()
		tmp = n
		cnt2 = 0
		while tmp % 2 == 0:
			cnt2 += 1
			tmp //= 2
		cnt3 = 0
		while tmp % 3 == 0:
			cnt3 += 1
			tmp //= 3
		if 3 ** cnt3 * 2 ** cnt2 == n:
			if cnt3 >= cnt2:
				x = cnt3 - cnt2 + cnt3
				ans += str(x) + '\n'
			else:
				ans += '-1\n'
		else:
			ans += '-1\n'
	print(ans)
main()


def check(n):
	count = 0
	limit = 0
	while n > 1:
		if n % 6 == 0:
			count += 1
			n = n / 6
		elif n * 2 % 6 == 0:
			n = n * 2 / 6
			count += 2
		else:
			n = n * 2
			count += 1
			limit += 1
		if limit > 50:
			break
	if n == 1:
		return count
	else:
		return '-1'
for _ in range(int(input())):
	n = int(input())
	print(check(n))


n_lines = int(input())
for _ in range(n_lines):
	line = input()
	line = line.rstrip('\n').split(' ')
	line = [int(s) for s in line]
	x = line[0]
	y = x
	a = 0
	while y % 3 == 0:
		y = y // 3
		a += 1
	y = x
	b = 0
	while y % 2 == 0:
		y = y // 2
		b += 1
	if x / (3 ** a * 2 ** b) == 1 and a >= b:
		print(2 * a - b)
	else:
		print(-1)


testcases = int(input())
for i in range(testcases):
	n = int(input())
	if n == 1:
		print(0)
	else:
		done = False
		count = 0
		while n != 1:
			if n % 3 != 0:
				print(-1)
				done = True
				break
			else:
				if n % 6 == 0:
					n = n / 6
				else:
					n = n * 2
				count += 1
		if not done:
			print(count)


t = int(input())
for i in range(t):
	n = int(input())
	N = n
	a = 0
	while N % 2 == 0:
		N //= 2
		a += 1
	b = 0
	while N % 3 == 0:
		N //= 3
		b += 1
	if N == 1 and b >= a:
		ans = 0
		while n > 1:
			if n % 6 == 0:
				n //= 6
			else:
				n *= 2
			ans += 1
	else:
		ans = -1
	print(ans)


import os
import sys
from io import BytesIO, IOBase
from bisect import bisect_left, bisect_right
import threading
from collections import Counter, defaultdict
arr = []

def main():
	for _ in range(int(input())):
		num = int(input())
		mv = 0
		while num % 3 == 0:
			if num % 6 == 0:
				num //= 6
			else:
				num *= 2
			mv += 1
		if num == 1:
			print(mv)
		else:
			print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for i in ' ' * int(input()):
	n = int(input())
	s = 0
	while n > 1:
		if n % 6 != 0:
			n *= 2
			s += 1
		if n % 6 != 0:
			s = -1
			break
		else:
			n //= 6
			s += 1
	print(s)


for i in range(int(input())):
	n = int(input())
	k = 0
	f = 0
	k2 = 0
	while n != 1:
		if n % 3 == 0:
			n = n / 3
			k = k + 1
		elif n % 2 == 0:
			n = n / 2
			k2 = k2 + 1
		else:
			f = 1
			break
	if f == 1:
		print(-1)
	else:
		k1 = k
		if k1 >= k2:
			print(k1 + k1 - k2)
		else:
			print(-1)


for _ in range(int(input())):
	num = int(input())
	(n1, n2) = (0, 0)
	while True:
		if num % 3 != 0:
			break
		num = num // 3
		n2 += 1
	while not num & 1:
		num = num // 2
		n1 += 1
	if n2 < n1 or num != 1:
		print('-1')
	else:
		print(n2 + (n2 - n1))


for testcase in range(int(input())):
	n = int(input())
	count = 0
	f = 1
	while n != 1:
		if n % 6 == 0:
			count += 1
			n //= 6
		else:
			n = n * 2
			count += 1
			if n % 6 == 0:
				count += 1
				n //= 6
			else:
				print(-1)
				f = 0
				break
	if f:
		print(count)


import math

def prime(n):
	a = 0
	b = 0
	while n % 2 == 0:
		a += 1
		n = n // 2
	while n % 3 == 0:
		b += 1
		n = n // 3
	if a > b:
		return (0, -1)
	if n > 2:
		return (0, -1)
	return (a, b)
t = int(input())
for _ in range(t):
	x = int(input())
	(c, d) = prime(x)
	if c == 0 and d == -1:
		print(-1)
	else:
		print(d - c + d)


t = int(input())
for i in range(t):
	n = int(input())
	count = 0
	flag = 0
	if n == 1:
		print(0)
	if n == 2 or n == 4 or n == 5:
		print(-1)
	if n == 6:
		print(1)
	if n == 3:
		print(2)
	if n > 6:
		j = 0
		k = 0
		p = n
		while n % 3 == 0:
			n = n / 3
			j += 1
		while n % 2 == 0:
			n = n / 2
			k += 1
		r = 2 ** k * 3 ** j
		if j == 0:
			print(-1)
		elif j >= k:
			if r == p:
				print(2 * j - k)
			elif r != n:
				print(-1)
		elif j < k:
			print(-1)


times = int(input())
for i in range(times):
	x = 0
	y = 0
	number = int(input())
	while number % 3 == 0:
		number = number // 3
		x += 1
	while number % 2 == 0:
		number = number // 2
		y += 1
	if y > x or number != 1:
		print(-1)
	else:
		print(2 * x - y)


import sys
input = sys.stdin.readline
read = lambda : map(int, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(councils, a, k):
	summ = 0
	for x in a:
		summ += min(x, councils)
	return summ // councils >= k
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
from itertools import permutations
import math
import bisect as bis
import random
import sys

def solve():
	n = input().rstrip()
	m = list(map(int, list(n)))
	n = int(n)
	if n == 1:
		print(0)
		return
	two = 0
	three = 0
	while n % 2 == 0:
		n //= 2
		two += 1
	while n % 3 == 0:
		n //= 3
		three += 1
	if two > three:
		print(-1)
		return
	if n != 1:
		print(-1)
		return
	if two == three:
		print(two)
		return
	print(abs(two - three) + max(two, three))
for _ in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	tmp = n
	c1 = 0
	c2 = 0
	while n > 1 and n % 2 == 0:
		c1 += 1
		n //= 2
	while n > 1 and n % 3 == 0:
		c2 += 1
		n //= 3
	if n == 1 and c1 <= c2:
		count = 0
		while c1 > 0 and c2 > 0:
			count += 1
			c1 -= 1
			c2 -= 1
		count += c2 * 2
		print(int(count))
	else:
		print(-1)


T = int(input())
i = 0
while i < T:
	N = int(input())
	C2 = 0
	C3 = 0
	while N != 1:
		Div2 = True
		Div3 = True
		if N % 3 == 0:
			N = N // 3
			C3 = C3 + 1
		else:
			Div3 = False
		if N % 2 == 0:
			N = N // 2
			C2 = C2 + 1
		else:
			Div2 = False
		if Div2 == False and Div3 == False:
			break
	if N == 1:
		if C2 > C3:
			print(-1)
		else:
			print(C3 - C2 + C3)
	else:
		print(-1)
	i = i + 1


def main():
	t = int(input())
	while t:
		n = int(input())
		count = 0
		while n <= 1000000000 and n != 1:
			if n % 6 == 0:
				n = n // 6
			else:
				n = n * 2
			count += 1
		if n == 1:
			print(count)
		else:
			print(-1)
		t -= 1
main()


from sys import stdin
t = int(stdin.readline().strip())
for i in range(t):
	n = int(stdin.readline().strip())
	num2 = 0
	while n % 2 == 0:
		num2 += 1
		n /= 2
	num3 = 0
	while n % 3 == 0:
		num3 += 1
		n /= 3
	if n != 1 or num2 > num3:
		print(-1)
	else:
		print(num3 + (num3 - num2))


t = int(input())
output = []
for i in range(t):
	n = int(input())
	cnt2 = 0
	while n % 2 == 0:
		cnt2 += 1
		n /= 2
	cnt3 = 0
	while n % 3 == 0:
		cnt3 += 1
		n /= 3
	if n == 1 and cnt3 >= cnt2:
		output.append(2 * cnt3 - cnt2)
	else:
		output.append(-1)
for o in output:
	print(o)


def game(n):
	ans = 0
	while n != 1:
		if n == 1:
			return ans
		if n % 3 != 0:
			return -1
		if n % 6 == 0:
			n = n // 6
			ans += 1
		else:
			n = n * 2
			ans += 1
	return ans
t = int(input())
while t:
	print(game(int(input())))
	t = t - 1


c = int(input())
a = []
for i in range(c):
	c = int(input())
	a.append(c)
ans = []
for i in a:
	n2 = n3 = 0
	n = i
	while n % 2 == 0 or n % 3 == 0:
		if n % 2 == 0:
			n = n / 2
			n2 = n2 + 1
		else:
			n = n / 3
			n3 = n3 + 1
	if n == 1:
		if n2 <= n3:
			ans.append(n3 + (n3 - n2))
		else:
			ans.append(-1)
	else:
		ans.append(-1)
for i in ans:
	print(i)


def multi(test):
	n = int(input())
	count = 0
	flag = 0
	while n > 1:
		if n % 6 == 0:
			count += 1
			n = n // 6
			flag = 0
		else:
			count += 1
			n = n * 2
			flag += 1
			if flag > 5:
				flag = 0
				return -1
	return count
t = int(input())
l = []
for i in range(t):
	res = multi(i)
	l.append(res)
for i in l:
	print(i)


def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def mi():
	return map(int, input().split())

def main():
	for i in range(ii()):
		x = ii()
		if x == 1:
			print(0)
			continue
		if x % 3 != 0 or x == 0:
			print(-1)
			continue
		k = 0
		check = 0
		while True:
			if x == 1:
				print(k)
				break
			if x % 6 == 0:
				x = x // 6
				k += 1
				continue
			if x % 6 != 0 and x % 3 == 0:
				x = x * 2
				k = k + 1
				continue
			if x % 6 != 0 and x % 3 != 0:
				print(-1)
				break
main()


list1 = []
b = input()
for i in range(int(b)):
	c = input()
	d = int(c)
	f = 0
	if d == 1:
		list1.append(0)
		continue
	while d % 3 == 0:
		d = d / 3
		f = f + 1
	import math
	e = math.log2(d)
	if e % 1 == 0 and f >= e and (f != 0):
		g = f * 2 - e
		list1.append(int(g))
		continue
	if f == 0:
		list1.append(-1)
		continue
	else:
		list1.append(-1)
		continue
for t in list1:
	print(t)


from sys import stdin, stdout
q = int(input())
for i in range(q):
	n = int(input())
	cnt = 0
	flag = 0
	while n > 1:
		if n % 6 == 0:
			n = n // 6
			cnt += 1
		elif n % 3 == 0:
			n = 2 * n // 6
			cnt += 2
		else:
			print(-1)
			flag = 1
			break
	if flag == 0:
		print(cnt)


import getopt
import sys
import math
onlyCase = -1
verbose = False
try:
	(opts, args) = getopt.getopt(sys.argv[1:], 'vc:', ['case='])
except getopt.GetoptError as err:
	print(str(err))
	sys.exit(2)
for (o, a) in opts:
	if o in ('-c', '--case'):
		onlyCase = int(a)
	elif o == '-v':
		verbose = True
	else:
		sys.exit()
ncases = int(input())
for c in range(1, ncases + 1):
	n = int(input())
	if onlyCase != -1 and onlyCase != c:
		continue
	d3 = 0
	d2 = 0
	x = n
	while x % 3 == 0:
		x = x // 3
		d3 += 1
	while x % 2 == 0:
		x = x // 2
		d2 += 1
	if verbose:
		print('%d by 3: %d by 2 %d (left: %d)' % (n, d3, d2, x))
	if d2 > d3 or x != 1:
		print('-1')
	else:
		print('%d' % (2 * d3 - d2))