Polycarp plays "Game 23". Initially he has a number $n$ and his goal is to transform it to $m$. In one move, he can multiply $n$ by $2$ or multiply $n$ by $3$. He can perform any number of moves.

Print the number of moves needed to transform $n$ to $m$. Print -1 if it is impossible to do so.

It is easy to prove that any way to transform $n$ to $m$ contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).


-----Input-----

The only line of the input contains two integers $n$ and $m$ ($1 \le n \le m \le 5\cdot10^8$).


-----Output-----

Print the number of moves to transform $n$ to $m$, or -1 if there is no solution.


-----Examples-----
Input
120 51840

Output
7

Input
42 42

Output
0

Input
48 72

Output
-1



-----Note-----

In the first example, the possible sequence of moves is: $120 \rightarrow 240 \rightarrow 720 \rightarrow 1440 \rightarrow 4320 \rightarrow 12960 \rightarrow 25920 \rightarrow 51840.$ The are $7$ steps in total.

In the second example, no moves are needed. Thus, the answer is $0$.

In the third example, it is impossible to transform $48$ to $72$.

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Implementation


Solutions:
(n, m) = list(map(int, input().split()))
if m % n > 0:
	print(-1)
else:
	res = 0
	a = m // n
	while a % 2 == 0:
		a //= 2
		res += 1
	while a % 3 == 0:
		a //= 3
		res += 1
	if a == 1:
		print(res)
	else:
		print(-1)


(n, m) = map(int, input().split(' '))
res = 0
for d in [2, 3]:
	while m / d % n == 0:
		res += 1
		m /= d
if m == n:
	print(res)
else:
	print(-1)


def main():
	(n, m) = map(int, input().split(' '))
	if m % n > 0:
		print(-1)
		return
	a = m // n
	transforms = 0
	while a > 1:
		if a % 2 == 0:
			a //= 2
		elif a % 3 == 0:
			a //= 3
		else:
			print(-1)
			return
		transforms += 1
	print(transforms)
main()


from sys import setcheckinterval, stdin
setcheckinterval(1000)
iin = lambda : int(stdin.readline())
lin = lambda : list(map(int, stdin.readline().split()))
(n, k) = lin()
ch = 0

def factors(a):
	fact = []
	if a % 2 == 0:
		ch = 0
		while a % 2 == 0:
			ch += 1
			a //= 2
		fact.append([2, ch])
	i = 3
	while i * i <= a:
		if a % i == 0:
			ch = 0
			while a % i == 0:
				ch += 1
				a //= i
			fact.append([i, ch])
		i += 2
	if a > 1:
		fact.append([a, 1])
	return fact
if k % n != 0:
	print(-1)
	exit()
k //= n
a1 = factors(k)
for (i, j) in a1:
	if i == 2 or i == 3:
		ch += j
	else:
		print(-1)
		exit()
print(ch)


(n, m) = list(map(int, input().split()))
count = 0
a = m // n
if m % n == 0:
	while a != 1:
		if a % 3 == 0:
			a //= 3
			count += 1
		elif a % 2 == 0:
			a //= 2
			count += 1
		else:
			count = -1
			break
	print(count)
else:
	print(-1)


(n, m) = map(int, input().split())
cnt = 0
a = m // n
while a > 1 and a % 2 == 0:
	a //= 2
	cnt += 1
while a > 1 and a % 3 == 0:
	a //= 3
	cnt += 1
if a != 1 or m < n or m % n != 0:
	print(-1)
else:
	print(cnt)


(numu, numd) = map(int, input().split())
cud = 0
cut = 0
cdd = 0
cdt = 0
while numu / 2 == numu // 2 and numu != 1:
	numu = numu / 2
	cud += 1
while numu / 3 == numu // 3 and numu != 1:
	numu = numu / 3
	cut += 1
while numd / 2 == numd // 2 and numd != 1:
	numd = numd / 2
	cdd += 1
while numd / 3 == numd // 3 and numd != 1:
	numd = numd / 3
	cdt += 1
if numu == numd and (cud > cdd or cut > cdt):
	print('-1')
elif numu == numd:
	print(cdd + cdt - cud - cut)
else:
	print('-1')


(n, m) = map(int, input().split())
k = 0
if m % n == 0:
	chislo = m // n
	while chislo % 2 == 0:
		k += 1
		chislo //= 2
	while chislo % 3 == 0:
		k += 1
		chislo //= 3
	if chislo != 1:
		print(-1)
	else:
		print(k)
else:
	print(-1)


import math
(n, m) = map(int, input().split())
a = 0
x = m / n
while True:
	if x % 2 == 0:
		a += 1
		x = x / 2
	elif x % 3 == 0:
		a += 1
		x = x / 3
	elif x == 1.0:
		print(a)
		break
	else:
		print('-1')
		break


(x, y) = map(int, input().split())
c = 0
f = 0
if y % x != 0:
	f = 1
else:
	d = y // x
	while d % 2 == 0:
		d //= 2
		c += 1
	while d % 3 == 0:
		d //= 3
		c += 1
	if d != 1:
		f = 1
if f == 1:
	print('-1')
else:
	print(c)


import math

def primeFactors(n):
	a = []
	while n % 2 == 0:
		a.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			a.append(i)
			n = n / i
	if n > 2:
		a.append(n)
	return a
(a, b) = map(int, input().split())
k = b / a
c = 0
if k % 1 == 0:
	y = primeFactors(k)
	for i in y:
		if i == 2 or i == 3:
			c = c + 1
	if c == len(y):
		print(c)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
cnt = 0
num = m / n
while 1:
	if num != int(num):
		print(-1)
		break
	elif num == 1:
		print(cnt)
		break
	elif num % 2 == 0:
		num /= 2
		cnt += 1
	else:
		num /= 3
		cnt += 1


def main():
	(a, b) = [int(v) for v in input().split()]
	if b % a != 0:
		print(-1)
		return
	c = 0
	d = b // a
	while d % 3 == 0:
		d //= 3
		c += 1
	while d % 2 == 0:
		d //= 2
		c += 1
	print(c if d == 1 else '-1')
main()


(n, m) = [int(x) for x in input().split(' ')]
from math import gcd, log, log2
a = [2 ** x for x in range(40)]
b = [3 ** x for x in range(40)]
for i in range(40):
	for j in range(40):
		if n * a[i] * b[j] == m:
			print(i + j)
			exit(0)
print(-1)


[n, m] = list(map(int, input().split()))
power = m / n
count = 0
while power % 2 == 0:
	power = power / 2
	count += 1
while power % 3 == 0:
	power = power / 3
	count += 1
if power != 1:
	print(-1)
else:
	print(count)


(n, m) = [int(x) for x in input().split()]
if m % n != 0:
	print(-1)
else:
	d = m // n
	c = 0
	while d % 2 == 0:
		c += 1
		d //= 2
	while d % 3 == 0:
		c += 1
		d //= 3
	if d != 1:
		print(-1)
	else:
		print(c)


(n, m) = map(int, input().split())
for i in range(10000):
	for j in range(10000):
		nn = n * 2 ** i * 3 ** j
		if nn == m:
			print(i + j)
			exit()
		elif nn > m:
			break
print(-1)


def log3(a):
	if a == 1:
		return 0
	elif a % 3 != 0:
		return 1000000
	else:
		return 1 + log3(a // 3)
inp = input()
inp = inp.split()
for i in inp:
	i = int(i)
inp[0] = int(inp[0])
inp[1] = int(inp[1])
ans = int(1000000)
if inp[1] % inp[0] != 0:
	print(-1)
else:
	inp[1] //= inp[0]
	for i in range(0, 62):
		ans = min(ans, i + log3(inp[1]))
		if inp[1] % 2 != 0:
			break
		inp[1] //= 2
	if ans == 1000000:
		print(-1)
	else:
		print(ans)


(n, m) = map(int, input().split())
if n == m:
	print(0)
elif m % n != 0:
	print(-1)
else:
	h = m // n
	g = 0
	while h != 1:
		y = h
		if h % 2 == 0:
			h = h // 2
			g += 1
		elif h % 3 == 0:
			h = h // 3
			g += 1
		if y == h:
			g = -1
			break
	print(g)


import sys
input = sys.stdin.readline
output = sys.stdout
c = 0
(a, b) = map(int, input().split())
if a == b:
	res = 0
elif b / a != b // a:
	res = -1
else:
	div = b // a
	while 1:
		if div % 2 == 0:
			div //= 2
			c += 1
		elif div % 3 == 0:
			div //= 3
			c += 1
		else:
			break
	if div == 1:
		res = c
	else:
		res = -1
output.write(str(res) + '\n')


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

def ss():
	return [x for x in input()]

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

def mi():
	return map(int, input().split())
(a, b) = mi()
if a == b:
	print(0)
elif a > b:
	print(-1)
else:
	s = [[0 for _ in range(30)] for _ in range(30)]
	s[0][0] = a
	for i in range(1, 30):
		if s[i - 1][0] * 3 > b:
			break
		s[i][0] = s[i - 1][0] * 3
	it = i
	for x in range(it):
		for i in range(1, 30):
			if s[x][i - 1] * 2 > b:
				break
			s[x][i] = s[x][i - 1] * 2
	key = False
	for i in range(it):
		for x in range(30):
			if s[i][x] == b:
				key = True
				ans = i + x
				break
	if key:
		print(ans)
	else:
		print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	m //= n
	ans = 0
	while m % 2 == 0:
		ans += 1
		m //= 2
	while m % 3 == 0:
		ans += 1
		m //= 3
	if m != 1:
		print(-1)
	else:
		print(ans)


(x, y) = map(int, input().split())
n = y / x
c = 0
flag = 0
while n != 1:
	if n % 3 == 0:
		n /= 3
		c += 1
	elif n % 2 == 0:
		n /= 2
		c += 1
	else:
		flag = 1
		break
if flag == 1:
	print('-1')
else:
	print(c)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	num = m // n
	if num == 1:
		print(0)
	else:
		res = 0
		while num % 2 == 0:
			num //= 2
			res += 1
		while num % 3 == 0:
			num //= 3
			res += 1
		if num != 1:
			print(-1)
		else:
			print(res)


(a, b) = map(int, input().split())
if b % a == 0:
	div = b // a
	moves = 0
	while div > 1:
		if div % 2 == 0:
			div /= 2
		elif div % 3 == 0:
			div /= 3
		else:
			moves = -1
			break
		moves += 1
	print(moves)
else:
	print(-1)


c1 = 0
c2 = 0
g1 = 0
g2 = 0
cnt = 0
(a, b) = map(int, input().split())
while a % 2 == 0:
	a //= 2
	c1 += 1
while b % 2 == 0:
	b //= 2
	c2 += 1
while a % 3 == 0:
	a //= 3
	g1 += 1
while b % 3 == 0:
	b //= 3
	g2 += 1
if c1 > c2 or g1 > g2:
	print(-1)
	exit()
a *= pow(2, c1)
a *= pow(3, g1)
b *= pow(2, c2)
b *= pow(3, g2)
while c1 < c2:
	a *= 2
	cnt += 1
	c1 += 1
while g1 < g2:
	cnt += 1
	a *= 3
	g1 += 1
if a == b:
	print(cnt)
else:
	print(-1)


(n, m) = map(int, input().split(' '))
moves = 0
if m % n != 0 and m / n % 2 != 0 and (m / n % 3 != 0):
	print(-1)
	exit()
else:
	k = m / n
	while k % 3 == 0:
		k = k / 3
		moves += 1
	while k % 2 == 0:
		k = k / 2
		moves += 1
	if k == 1:
		print(moves)
		exit()
	else:
		print(-1)
		exit()


(n, m) = map(int, input().split())
if m % n == 0:
	div = m // n
	pow2 = pow3 = 0
	while div != 1:
		if div % 2 != 0 and div % 3 != 0:
			print(-1)
			break
		if div % 2 == 0:
			div //= 2
			pow2 += 1
		if div % 3 == 0:
			div //= 3
			pow3 += 1
	else:
		print(pow2 + pow3)
else:
	print(-1)


from math import gcd

def solve(n, m, k):
	if n > m:
		return -1
	if n == m:
		return k
	x = solve(n * 2, m, k + 1)
	y = solve(n * 3, m, k + 1)
	if x >= 0:
		if y >= 0:
			return min(x, y)
		return x
	elif y >= 0:
		return y
	return -1

def main():
	(n, m) = list(map(int, input().split()))
	print(solve(n, m, 0))
main()


values = list(map(int, input().split()))
min_value = values[0]
max_value = values[1]
controle = 0
flag_01 = 0
flag_02 = 0
if max_value % min_value != 0:
	print(-1)
elif max_value == min_value:
	print(0)
else:
	result = max_value / min_value
	flag_01 = 1
	while result > 2 and result != 3:
		if result % 3 == 0:
			result = result / 3
			controle += 1
		elif result % 2 == 0:
			result = result / 2
			controle += 1
		else:
			flag_02 = 1
			print(-1)
			break
if flag_01 == 1 and flag_02 == 0:
	print(controle + 1)


li = list(map(int, input().split()))
n = li[0]
m = li[1]
num = m / n
flag = 0
c = 0
d = 0
if num == 1:
	print(0)
else:
	while num != 1:
		if num % 2 != 0 and num % 3 != 0:
			flag = 1
			break
		elif num % 2 == 0:
			c += 1
			num = int(num / 2)
		elif num % 3 == 0:
			d += 1
			num = int(num / 3)
	if flag == 1:
		print(-1)
	else:
		print(c + d)


(n, m) = list(map(int, input().split()))
p = m / n
c = 0
while p % 2 == 0:
	p = p / 2
	c = c + 1
while p % 3 == 0:
	p = p / 3
	c = c + 1
if p != 1:
	print('-1')
else:
	print(c)


l = input()
a = int(l.split(' ')[0])
b = int(l.split(' ')[1])
if a == b:
	print(0)
elif b % a != 0 or b < a:
	print(-1)
else:
	div = b / a
	cnt = 0
	b = True
	while div != 1:
		if div % 2 == 0:
			div /= 2
			cnt += 1
		elif div % 3 == 0:
			div /= 3
			cnt += 1
		else:
			b = False
			print(-1)
			break
	if b:
		print(cnt)


(n, m) = list(map(int, input().split()))
if m % n == 0:
	j = m // n
	cnt = 0
	while j % 2 == 0:
		cnt += 1
		j //= 2
	while j % 3 == 0:
		cnt += 1
		j //= 3
	if j != 1:
		print(-1)
	else:
		print(cnt)
else:
	print(-1)


def solve():
	(n, m) = map(int, input().split())
	if m % n != 0:
		return -1
	t = m // n
	ans = 0
	for i in [2, 3]:
		while t % i == 0:
			t //= i
			ans += 1
	if t > 1:
		return -1
	return ans
print(solve())


def fonksiyon(sayi1, sayi2):
	if sayi2 % sayi1 != 0:
		return '-1'
	sayi3 = sayi2 / sayi1
	check = 0
	while sayi3 != 1:
		if sayi3 % 3 == 0:
			sayi3 = sayi3 / 3
			check += 1
		elif sayi3 % 2 == 0:
			sayi3 = sayi3 / 2
			check += 1
		else:
			return '-1'
	return check
(sayi1, sayi2) = input().split()
sayi1 = int(sayi1)
sayi2 = int(sayi2)
print(fonksiyon(sayi1, sayi2))


(n, m) = map(int, input().split())
total = 0
if m % n != 0:
	print(-1)
	exit()
v = m // n
while v % 2 == 0:
	total += 1
	v //= 2
while v % 3 == 0:
	total += 1
	v //= 3
if v != 1:
	print(-1)
	exit()
print(total)


(x, y) = map(int, input().split(' '))
k = 0
if y % x != 0:
	print(-1)
else:
	y = y / x
	while y != 1:
		if y % 2 == 0:
			y /= 2
			k += 1
		elif y % 3 == 0:
			y /= 3
			k += 1
		else:
			print(-1)
			break
	else:
		print(k)


(n, m) = [int(x) for x in input().split()]
if m % n != 0:
	print(-1)
else:
	ans = 0
	fact = m // n
	while fact:
		if fact == 1:
			break
		elif fact % 2 == 0:
			fact /= 2
			ans += 1
		elif fact % 3 == 0:
			fact /= 3
			ans += 1
		else:
			ans = -1
			break
	print(ans)


n = input()
a = int(n.split()[0])
b = int(n.split()[1])

def game23(a, count, b):
	if b % a != 0:
		return False
	if a == b:
		print(count)
		return True
	if game23(a * 2, count + 1, b):
		return True
	if game23(a * 3, count + 1, b):
		return True
if not game23(a, 0, b):
	print('-1')


import sys, math, queue
sys.setrecursionlimit(1000000)
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit()
m = m // n
ans = 0
while m % 2 == 0:
	m = m // 2
	ans += 1
while m % 3 == 0:
	m = m // 3
	ans += 1
if m != 1:
	print(-1)
else:
	print(ans)


num = input().split(' ')
(n, m) = (int(num[0]), int(num[1]))
if n == m:
	print(0)
elif m < n or m % n != 0:
	print(-1)
else:
	ans = 0
	k = m // n
	while k % 3 == 0:
		ans += 1
		k = k // 3
	if k != 1:
		while k % 2 == 0:
			ans += 1
			k = k // 2
	if k != 1:
		print(-1)
	else:
		print(ans)


(n, m) = map(int, input().strip().split())
if m % n != 0:
	print('-1')
	exit()
m = m // n
cnt = 0
while m % 2 == 0:
	m = m // 2
	cnt += 1
while m % 3 == 0:
	m = m // 3
	cnt += 1
if m != 1:
	print('-1')
	exit()
print(cnt)


(n, m) = map(int, input().split())
z = 0
v = 0
k = 2
if m % n != 0:
	print(-1)
else:
	z = m // n
	while k > 1:
		if z % 2 == 0:
			z = int(z / 2)
			v += 1
		elif z % 3 == 0:
			z = int(z / 3)
			v += 1
		else:
			k = 0
	if z == 1:
		print(v)
	else:
		print(-1)


[m, n] = list(map(int, input().rstrip().split()))
r = n // m
count = 0
while r % 3 == 0:
	r = r // 3
	count += 1
while r % 2 == 0:
	r = r // 2
	count += 1
if n % m == 0 and r == 1:
	print(count)
else:
	print(-1)


def fo(x):
	y = x
	z = x
	(s2, s3) = (0, 0)
	while x % 2 == 0:
		x = x // 2
		s2 += 1
	while y % 3 == 0:
		y = y // 3
		s3 += 1
	if 2 ** s2 * 3 ** s3 == z:
		k = 1
	else:
		k = 0
	arr = [s2, s3, k]
	return arr
(a, b) = list(map(int, input().split()))
if b % a == 0:
	x = b // a
	arr = fo(x)
	if arr[-1] == 0:
		print(-1)
	else:
		print(arr[0] + arr[1])
else:
	print(-1)


(n, m) = map(int, input().split())
k2 = 0
k3 = 0
kol2 = 0
kol3 = 0
n1 = n
m1 = m
while n1 % 3 == 0 or n1 % 2 == 0:
	if n1 % 2 == 0:
		n1 //= 2
		k2 += 1
	if n1 % 3 == 0:
		k3 += 1
		n1 //= 3
while m1 % 3 == 0 or m1 % 2 == 0:
	if m1 % 2 == 0:
		m1 //= 2
		kol2 += 1
	if m1 % 3 == 0:
		kol3 += 1
		m1 //= 3
if kol3 >= k3 and kol2 >= k2 and (n * 2 ** (kol2 - k2) * 3 ** (kol3 - k3) == m):
	print(kol3 - k3 + kol2 - k2)
else:
	print(-1)


from math import *
(n, m) = map(int, input().strip().split())
if m % n != 0:
	print(-1)
else:
	x = m // n
	u = gcd(x, 2 ** 40)
	v = gcd(x, 3 ** 40)
	if x // u // v != 1:
		print(-1)
	else:
		print(round(log(u) / log(2)) + round(log(v) / log(3)))


(a, b) = map(int, input().split())
if b % a != 0:
	print(-1)
	exit()
if b == a:
	print(0)
	exit()
n = b / a
moves = 0
for i in range(2, 4):
	while n % i == 0:
		moves += 1
		n = n / i
if n != 1:
	print(-1)
else:
	print(moves)


(n, m) = map(int, input().split())
tmp = m
if m % n:
	print(-1)
	exit()
else:
	m //= n
	cnt1 = 0
	while m % 2 == 0:
		m //= 2
		cnt1 += 1
	cnt2 = 0
	while m % 3 == 0:
		m //= 3
		cnt2 += 1
	if n * 2 ** cnt1 * 3 ** cnt2 == tmp:
		print(cnt1 + cnt2)
	else:
		print(-1)


(n, m) = (int(x) for x in input().split())
div = m / n
opr = 0
while div % 3 == 0:
	opr += 1
	div = div / 3
while div % 2 == 0:
	opr += 1
	div = div / 2
if m == n:
	print(0)
elif div != 1:
	print(-1)
else:
	print(opr)


(n, m) = map(int, input().split())
dif = m / n
i = 0
while dif % 2 == 0:
	dif /= 2
	i += 1
while dif % 3 == 0:
	dif /= 3
	i += 1
if dif != 1:
	print(-1)
else:
	print(i)


(n1, n2) = map(int, input().split())
counter = 0
num = int(n2 / n1)
if num != n2 / n1:
	print(-1)
elif num == 1:
	print(0)
elif num % 2 != 0 and num % 3 != 0:
	print(-1)
else:
	while num != 1:
		if num % 3 == 0:
			num /= 3
			counter += 1
		elif num % 2 == 0:
			num /= 2
			counter += 1
		else:
			print(-1)
			break
	if num == 1:
		print(counter)


(a, b) = map(int, input().split(' '))
if b % a != 0:
	print(-1)
else:
	n = 0
	num = b / a
	while num % 2 == 0:
		num /= 2
		n += 1
	while num % 3 == 0:
		num /= 3
		n += 1
	if num != 1:
		print(-1)
	else:
		print(n)


import sys
(n, m) = map(int, input().split())
if m % n:
	print(-1)
	sys.exit()
u = m // n
cnt = 0
while u % 2 == 0:
	u //= 2
	cnt += 1
while u % 3 == 0:
	u //= 3
	cnt += 1
print(cnt if u == 1 else -1)


(n, m) = map(int, input().split())
r = -1
if m % n == 0:
	m //= n
	r = 0
	for d in (3, 2):
		while m % d == 0:
			m //= d
			r += 1
print((r, -1)[m > 1])


import math

def maxPow(num, factor):
	count = 0
	while num % math.pow(factor, count) == 0:
		count += 1
	return count - 1
(n, m) = map(int, input().split())
calc = m / n
if calc < 1:
	print('-1')
else:
	fac2 = maxPow(calc, 2)
	fac3 = maxPow(calc, 3)
	if math.pow(2, fac2) * math.pow(3, fac3) == calc:
		print(fac2 + fac3)
	else:
		print('-1')


n = list(map(int, input().split()))
a = n[1] / n[0]
if int(a) == a:
	c = 0
	while a > 1:
		f1 = 0
		f2 = 0
		if a % 3 == 0:
			a = a / 3
			c = c + 1
			f1 = 1
		if a % 2 == 0:
			a = a / 2
			c = c + 1
			f2 = 1
		if f1 == 0 and f2 == 0:
			print(-1)
			break
	else:
		print(c)
else:
	print(-1)


a = input().split()
n = int(a[0])
m = int(a[1])
count = 0
k = m / n
if m == n:
	print('0')
elif m % n == 0:
	while k != 1:
		if k % 3 == 0:
			count += 1
			k /= 3
		elif k % 2 == 0:
			count += 1
			k /= 2
		else:
			k = 1
			count = -1
	print(count)
else:
	print('-1')


from sys import stdin as sin
from itertools import count
(n, m) = [int(i) for i in sin.readline().split(' ')]
if m % n != 0:
	print(-1)
	exit()
t = m / n
ans = 0
while t % 2 == 0:
	t /= 2
	ans += 1
while t % 3 == 0:
	t /= 3
	ans += 1
if t > 1:
	print(-1)
	exit()
else:
	print(ans)


r = [int(x) for x in input().split()]
m = 0
if r[1] % r[0] == 0:
	x = r[1] / r[0]
	while x % 2 == 0:
		m = m + 1
		x = x / 2
	while x % 3 == 0:
		m = m + 1
		x = x / 3
	if x == 1:
		print(m)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit(0)
r = m // n
c = 0
while r % 3 == 0:
	c += 1
	r = r // 3
while r % 2 == 0:
	c += 1
	r = r // 2
print(c) if r == 1 else print(-1)


def countEven(num):
	count = 0
	while num % 2 == 0:
		num = num / 2
		count += 1
	return [count, num]

def countThree(num):
	count = 0
	while num % 3 == 0:
		num = num / 3
		count += 1
	if num != 1:
		return False
	return [count, num]

def main():
	rawInput = input().split(' ')
	intInput = [int(x) for x in rawInput]
	inputM = intInput[1]
	inputN = intInput[0]
	devision = inputM / inputN
	step = 0
	if devision != int(devision):
		print(-1)
	elif devision == 1:
		print(0)
	else:
		countE = countEven(devision)
		step += countE[0]
		devision = countE[1]
		countT = countThree(devision)
		if countT == False:
			print(-1)
			return
		step += countT[0]
		if step == 0:
			print(-1)
		else:
			print(step)
main()


(n, m) = map(int, input().split(' '))
if m % n != 0:
	print(-1)
else:
	division = m / n
	count = 0
	while division % 2 == 0 or division % 3 == 0:
		if division % 2 == 0:
			division /= 2
			count += 1
		else:
			division /= 3
			count += 1
	if division != 1:
		print(-1)
	else:
		print(count)


from functools import reduce
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))
mo = 1000000007
(n, m) = value()
ans = 0
flag = 0
if m % n != 0:
	print(-1)
else:
	m = m // n
	while m % 2 == 0:
		m //= 2
		ans += 1
	while m % 3 == 0:
		m //= 3
		ans += 1
	if m != 1:
		ans = -1
	print(ans)


(n, m) = map(int, input().split())
count = 0
div = m / n
if m % n != 0:
	print(-1)
	exit()
while div % 2 == 0:
	count = count + 1
	div = div / 2
while div % 3 == 0:
	count = count + 1
	div = div / 3
print(count if div == 1 else -1)


(n, m) = map(int, input().split())
ans = 0
fl = 1
if m % n != 0:
	print(-1)
else:
	k = m / n
	while k > 1:
		if k % 3 == 0:
			ans += 1
			k /= 3
		elif k % 2 == 0:
			ans += 1
			k /= 2
		else:
			fl = 0
			k = 0
	if fl == 1:
		print(ans)
	else:
		print(-1)


import math
(n, m) = map(int, input().split())
if n == m:
	print('0')
elif m < n:
	print('-1')
elif m / n != int(m / n):
	print('-1')
else:
	v = int(m / n)
	cnt = 0
	if v % 2 != 0 and v % 3 != 0:
		print('-1')
	else:
		k = 0
		while v > 1:
			if v % 2 == 0:
				v = int(v / 2)
			elif v % 3 == 0:
				v = int(v / 3)
			else:
				k = 1
				print('-1')
				break
			cnt += 1
		if k == 0:
			print(cnt)


(a, b) = map(int, input().split())
if a == b:
	print(0)
	exit()
r = b // a
if b % a != 0:
	print(-1)
	exit()
t = 0
if r == 1:
	print(-1)
	exit()
while r > 1:
	if r % 3 == 0:
		r = r // 3
		t += 1
	elif r % 2 == 0:
		r = r // 2
		t += 1
	else:
		print(-1)
		exit()
print(t)


ch = input()
(a, b) = ch.split(' ')
n = int(a)
m = int(b)
if m % n == 0:
	r = m // n
	if r % 2 == 0 or r % 3 == 0 or r == 1:
		i = 2
		j = 0
		while r > 1 and (r % i == 0 or r % 3 == 0):
			while r % i == 0:
				r = r // i
				j += 1
			i = 3
		if r != 1:
			print('-1')
		else:
			print(j)
	else:
		print('-1')
else:
	print('-1')


(n, m) = map(int, input().split())
if m % n:
	print(-1)
else:
	n = m // n
	cnt = 0
	while not n % 3:
		n //= 3
		cnt += 1
	while not n % 2:
		n //= 2
		cnt += 1
	if n != 1:
		print(-1)
	else:
		print(cnt)


(n, m) = map(int, input().split())
re = m // n
mo = 0
if m % n != 0:
	print(-1)
else:
	i = 0
	if re == 0:
		print(0)
	else:
		while n > 0:
			if re == 1:
				print(i)
				break
			if re % 2 == 0:
				re = int(re / 2)
				mo += 1
			elif re % 3 == 0:
				re = int(re / 3)
				mo += 1
			elif re % 2 != 0 and re % 3 != 0:
				print(-1)
				break
			i += 1
		else:
			print(mo)


(m, n) = list(map(int, input().split()))
if n % m != 0:
	print(-1)
else:
	d = n // m
	res = 0
	k = 0
	while k == 0:
		if d % 3 == 0:
			d = d / 3
			res += 1
		elif d % 2 == 0:
			d = d / 2
			res += 1
		elif d == 1:
			print(res)
			k = 1
		else:
			print(-1)
			k = 1


(n, m) = map(int, input().split(' '))
ans = 0
for d in [2, 3]:
	while m / d % n == 0:
		m /= d
		ans += 1
	if m == n:
		print(ans)
		exit()
print(-1)


(a, b) = tuple(map(int, input().split()))
if b % a != 0:
	print(-1)
else:
	c = b // a
	num = 0
	while c % 2 == 0:
		num += 1
		c //= 2
	while c % 3 == 0:
		num += 1
		c //= 3
	if c == 1:
		print(num)
	else:
		print(-1)


def main():
	(n, m) = map(int, input().split())
	if m % n:
		print(-1)
		return
	d = m // n
	res = 0
	while d > 1 and (not d % 2):
		d //= 2
		res += 1
	while d > 1 and (not d % 3):
		d //= 3
		res += 1
	if d != 1:
		print(-1)
		return
	print(res)
	return
main()


(n, m) = map(int, input().split())
if m % n:
	print(-1)
	exit()
ans = 0
m = m // n
while m > 1:
	if m % 3 == 0:
		ans += 1
		m = m // 3
	if m % 2 == 0:
		ans += 1
		m = m // 2
	if m > 1 and m % 2 and m % 3:
		print(-1)
		exit()
print(ans)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	aux = m // n
	cont = 0
	while not aux % 2:
		aux = aux // 2
		cont += 1
	while not aux % 3:
		aux = aux // 3
		cont += 1
	if aux != 1:
		print(-1)
	else:
		print(cont)


(n, m) = list(map(int, input().split()))
(N, M) = (n, m)
k1 = 0
k2 = 0
while n % 2 == 0:
	n //= 2
	k1 += 1
while n % 3 == 0:
	n //= 3
	k2 += 1
k3 = 0
k4 = 0
while m % 2 == 0:
	m //= 2
	k3 += 1
while m % 3 == 0:
	m //= 3
	k4 += 1
if 3 ** max(0, k4 - k2) * (2 ** max(0, k3 - k1) * N) == M:
	print(k4 + k3 - k1 - k2)
else:
	print(-1)


(n, m) = [int(i) for i in input().split(' ')]
if m == n:
	print(0)
elif m / n == m // n:
	m /= n
	ans = 0
	while m % 2 == 0:
		ans += 1
		m /= 2
	while m % 3 == 0:
		ans += 1
		m /= 3
	if m == 1:
		print(ans)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
for i in range(30):
	for j in range(20):
		if n * 2 ** i * 3 ** j == m:
			print(i + j)
			exit(0)
print(-1)


tokens = input().split()
n = int(tokens[0])
m = int(tokens[1])
if m % n != 0:
	print(-1)
else:
	count = 0
	num = m // n
	while num % 2 == 0:
		num = num // 2
		count += 1
	while num % 3 == 0:
		num = num // 3
		count += 1
	if num == 1:
		print(count)
	else:
		print(-1)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
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 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 main():
	(n, m) = LI()
	if m % n != 0:
		return -1
	k = m // n
	r = 0
	while k > 1:
		if k % 2 == 0:
			k //= 2
			r += 1
		elif k % 3 == 0:
			k //= 3
			r += 1
		else:
			return -1
	if k == 1:
		return r
	return -1
print(main())


def game(n, m):
	if n == m:
		return 0
	if m % n != 0:
		return -1
	ans = 0
	val = m / n
	while val % 2 == 0:
		val = val / 2
		ans += 1
	while val % 3 == 0:
		val = val / 3
		ans += 1
	if val != 1:
		return -1
	return ans
(n, m) = list(map(int, input().split()))
print(game(n, m))


(n, m) = map(int, input().split())
k = m % n
if k != 0:
	print(-1)
else:
	k = m // n
	k2 = 0
	while k > 0 and k % 2 == 0:
		k = k // 2
		k2 += 1
	k3 = 0
	while k > 0 and k % 3 == 0:
		k = k // 3
		k3 += 1
	if n * 2 ** k2 * 3 ** k3 == m:
		print(k2 + k3)
	else:
		print(-1)


program_input = input().split(' ')
n = int(program_input[0])
m = int(program_input[1])
if m != n:
	cnt = 0
	while n != m:
		if n * 2 <= m:
			if m % (n * 2) == 0:
				n *= 2
				cnt += 1
			else:
				n *= 3
				cnt += 1
		else:
			cnt = -1
			break
	if cnt == 0:
		print(-1)
	else:
		print(cnt)
else:
	print(0)


(n, m) = map(int, input().split())
c = 0
if m % n == 0:
	m //= n
	while m > 1:
		if m % 6 == 0:
			m //= 6
			c += 2
		elif m % 3 == 0:
			m //= 3
			c += 1
		elif m % 2 == 0:
			m //= 2
			c += 1
		else:
			print(-1)
			exit()
	print(c)
else:
	print(-1)


(n, m) = input().split()
n = int(n)
m = int(m)
if m % n == 0:
	m = m / n
	j = 0
	while m % 2 == 0:
		j = j + 1
		m = m / 2
	while m % 3 == 0:
		j = j + 1
		m = m / 3
	if m == 1:
		print(j)
	else:
		print(-1)
else:
	print(-1)


def func(n, m):
	if n > m or m % n != 0:
		return -1
	temp = m // n
	c2 = c3 = 0
	while temp % 2 == 0:
		c2 += 1
		temp //= 2
	while temp % 3 == 0:
		c3 += 1
		temp //= 3
	if temp == 1:
		return c2 + c3
	else:
		return -1
(n, m) = map(int, input().split())
c = func(n, m)
print(c)


(n, m) = map(int, input().split())
N = m // n
if N * n != m:
	print('-1')
	exit()
cnt2 = 0
cnt3 = 0
while N % 2 == 0:
	cnt2 += 1
	N = N // 2
while N % 3 == 0:
	cnt3 += 1
	N = N // 3
if m == pow(2, cnt2) * pow(3, cnt3) * n:
	print(cnt2 + cnt3)
else:
	print(-1)


(n, m) = [int(x) for x in input().split(' ')]
if n == m:
	print('0')
	exit(0)
elif m % n != 0:
	print('-1')
	exit(0)
div = m / n
if div % 2 != 0 and div % 3 != 0:
	print('-1')
	exit(0)
divisors = [6, 3, 2]
ans = 0
for i in divisors:
	while div % i == 0:
		ans += 2 if i == 6 else 1
		div /= i
print(ans if div == 1 else '-1')


(n, m) = map(int, input().split())
if m % n:
	print(-1)
	exit()
else:
	s = m // n
	i = 0
	while s > 1:
		if s % 3 == 0:
			s //= 3
			i += 1
		elif s % 2 == 0:
			s //= 2
			i += 1
		else:
			print(-1)
			exit()
	print(i)


import math
(a, b) = list(map(int, input().split()))
count = 0
if b == a:
	print(0)
	exit()
elif b % a == 0:
	count = 0
	div = b // a
	while div % 2 == 0:
		div = div // 2
		count += 1
	while div % 3 == 0:
		div = div // 3
		count += 1
	if div == 1:
		print(count)
	else:
		print(-1)
else:
	print(-1)


(a, b) = input().split(' ')
a = int(a)
b = int(b)
n = b / a
counter = 0
while n % 2 == 0:
	n = n / 2
	counter += 1
while n % 3 == 0:
	n = n / 3
	counter += 1
if n == 1:
	print(int(counter))
else:
	print(-1)


(a, b) = map(int, input().split())
if b % a != 0:
	print(-1)
	exit(0)
if a == b:
	print(0)
	exit(0)
c = b // a
i = 1
while c % 2 ** i == 0:
	i += 1
j = 1
while c % 3 ** j == 0:
	j += 1
i -= 1
j -= 1
if a * 2 ** i * 3 ** j != b:
	print(-1)
	exit(0)
print(i + j)


(n, m) = [int(i) for i in input().split()]
k = 0
d = int(m / n)
if m % n != 0:
	print(-1)
elif d == 1:
	print(0)
else:
	while True:
		if d % 2 == 0:
			d /= 2
			k += 1
		elif d % 3 == 0:
			d /= 3
			k += 1
		else:
			if d == 1:
				print(k)
			else:
				print(-1)
			break


R = lambda : map(int, input().split())
(n, m) = R()
cntn2 = 0
cntn3 = 0
cntm2 = 0
cntm3 = 0
while n % 2 == 0:
	n = n / 2
	cntn2 += 1
while n % 3 == 0:
	n = n / 3
	cntn3 += 1
while m % 2 == 0:
	m = m / 2
	cntm2 += 1
while m % 3 == 0:
	m = m / 3
	cntm3 += 1
if (n == m) & (cntn2 <= cntm2) & (cntn3 <= cntm3):
	print(cntm2 - cntn2 + cntm3 - cntn3)
else:
	print(-1)


(num, target) = map(int, input().split())
if target % num != 0:
	print(-1)
else:
	div = target // num
	ans = 0
	while div != 1:
		if div % 2 == 0:
			ans += 1
			div //= 2
		if div % 3 == 0:
			ans += 1
			div //= 3
		if div % 3 != 0 and div % 2 != 0 and (div != 1):
			print(-1)
			exit()
	print(ans)


(n, m) = map(int, input().split())
if m % n == 0:
	if m == n:
		print(0)
	else:
		t = m // n
		ctr = 0
		while t % 2 == 0:
			t /= 2
			ctr += 1
		while t % 3 == 0:
			t /= 3
			ctr += 1
		print(ctr if t == 1 else -1)
else:
	print(-1)


(n, m) = map(int, input().split())
tmp = m / n
counter = 0
while tmp % 3 == 0:
	tmp = tmp / 3
	counter += 1
while tmp % 2 == 0:
	tmp = tmp / 2
	counter += 1
if tmp == 1:
	print(counter)
else:
	print(-1)


import sys
import collections
import math
import heapq
from operator import itemgetter

def getint():
	return int(input())

def getints():
	return [int(x) for x in input().split(' ')]
(n, m) = getints()
result = -1
if m % n == 0:
	result = 0
	n = m // n
	while n % 3 == 0:
		n //= 3
		result += 1
	while n % 2 == 0:
		n //= 2
		result += 1
	if n != 1:
		result = -1
print(str(result))


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest
from math import ceil, floor, log, log2, sqrt, gcd, factorial

def binNumber(n, size=1):
	return bin(n)[2:].zfill(size)

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

def ini():
	return int(input())

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

def sti():
	return str(input())
(n, m) = input().split()
n = int(n)
m = int(m)
ans = 0
while m % (2 * n) == 0:
	n = n * 2
	ans = ans + 1
while m % (3 * n) == 0:
	n = n * 3
	ans = ans + 1
if m == n:
	print(ans)
else:
	print(-1)


holi = input('')
holi = holi.split(' ')
n = int(holi[0])
m = int(holi[1])

def how_many_times_x_by_a(x, a):
	i = 0
	while 1:
		if x // a == x / a:
			x = x / a
			i = i + 1
		else:
			break
	return i
if m == 0 or n == 0:
	print(-1)
else:
	c = m / n
	by3 = how_many_times_x_by_a(c, 3)
	by2 = how_many_times_x_by_a(c, 2)
	if c != 2 ** by2 * 3 ** by3:
		print(-1)
	else:
		result = by3 + by2
		print(result)


a = list(map(float, input().split()))
res = a[1] / a[0]
result = 0
if a[1] % a[0] > 0:
	print('-1')
else:
	while res > 1:
		if res % 2 == 0:
			res = res / 2
		elif res % 3 == 0:
			res = res / 3
		else:
			print('-1')
			exit()
		result += 1
	print(result)


(a, b) = map(int, input().split())
n = 0
while a != b:
	if b / 3 % a != 0 and b / 2 % a != 0:
		n = -1
		break
	elif b / 3 % a == 0:
		n = n + 1
		b = b / 3
	elif b / 2 % a == 0:
		n = n + 1
		b = b / 2
print(n)


def move(n, step):
	if n == 3 or n == 2:
		step += 1
		return step
	if n % 2 == 0:
		n = n / 2
		step += 1
	elif n % 3 == 0:
		n = n / 3
		step += 1
	else:
		return -1
	return move(n, step)
(source, target) = map(int, input().split(' '))
step = 0
n = 0
if target % source == 0:
	n = target / source
	if n != 1:
		step = move(n, step)
else:
	step = -1
print(step)


(a, b) = map(int, input().split())
c1 = 0
f = 0
if a == b:
	print(0)
else:
	while True:
		if f == 1:
			break
		c2 = 0
		while True:
			v = a * 2 ** c1 * 3 ** c2
			if v == b:
				print(c2 + c1)
				f = 1
				break
			elif v > b:
				break
			c2 += 1
		if a * 2 ** c1 > b:
			print(-1)
			break
		c1 += 1


(n, m) = [int(x) for x in input().split(' ')]
if m % n != 0:
	print(-1)
else:
	m = m // n
	step = 0
	while m % 2 == 0:
		m = m // 2
		step += 1
	while m % 3 == 0:
		m = m // 3
		step += 1
	if m == 1:
		print(step)
	else:
		print(-1)


(a, b) = map(int, input().split())
if b % a != 0:
	print(-1)
	exit()
val = b / a
c1 = 0
c2 = 0
while val % 2 == 0:
	c1 = c1 + 1
	val = val / 2
while val % 3 == 0:
	c2 = c2 + 1
	val = val / 3
if val != 1:
	print(-1)
	exit()
print(c1 + c2)


(n, m) = map(int, input().split())
count = 0
if m == n:
	count = 0
elif m / n % 2 == 0 or m / n % 3 == 0:
	ans = m // n
	for i in range(m // n):
		if ans % 3 == 0 or ans % 2 == 0:
			count += 1
			if ans % 3 == 0:
				ans = ans // 3
			else:
				ans = ans // 2
		elif ans == 1:
			break
		elif ans % 3 != 0 and ans % 2 != 0:
			count = -1
			break
else:
	count = -1
print(count)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
elif m == n:
	print(0)
else:
	t = 0
	k = m // n
	while k % 3 == 0:
		k = k // 3
		t += 1
	while k % 2 == 0:
		k = k // 2
		t += 1
	if k == 1:
		print(t)
	else:
		print(-1)


(n, m) = map(int, input().rstrip().split())
if n == m:
	print(0)
elif n > m or m % n != 0:
	print(-1)
else:
	cnt = 0
	div = m // n
	while div != 0 and div % 3 == 0:
		cnt += 1
		if div == 3:
			div = 0
		else:
			div //= 3
	while div != 0 and div & 1 != 1:
		cnt += 1
		if div == 2:
			div = 0
		else:
			div >>= 1
	if div != 0:
		print(-1)
	else:
		print(cnt)


from collections import deque
(n, m) = map(int, input().split())
cnt = {}
dq = deque()
cnt[n] = 0
dq.append(n)
lenQ = 1
while lenQ:
	x = dq.popleft()
	lenQ -= 1
	if x == m:
		break
	if x < m:
		if cnt.get(x * 2) == None:
			cnt[x * 2] = cnt[x] + 1
			dq.append(x * 2)
			lenQ += 1
		if cnt.get(x * 3) == None:
			cnt[x * 3] = cnt[x] + 1
			dq.append(x * 3)
			lenQ += 1
print(cnt[m] if cnt.get(m) != None else -1)


(n, m) = map(int, input().split())
p = m / n
sum1 = 0
while p % 2 == 0:
	p /= 2
	sum1 += 1
while p % 3 == 0:
	p /= 3
	sum1 += 1
if p == 1:
	print(sum1)
else:
	print(-1)


line = input().split(' ')
n = int(line[0])
m = int(line[1])
k = 0
res = m / n
if m % n != 0:
	res = 1
	print(-1)
else:
	while res != 1:
		if res % 3 == 0:
			res = res / 3
			k = k + 1
		elif res % 2 == 0:
			res = res / 2
			k = k + 1
		else:
			res = 1
			k = -1
	print(k)


(a, b) = map(int, input().split())
t = b // a
if t % 2 != 0 and t % 3 != 0 and (t != 1) or b % a != 0:
	print(-1)
else:
	t = b // a
	k = 0
	while t % 3 == 0:
		if t % 9 == 0:
			t = t // 9
			k = k + 2
		elif t % 3 == 0:
			t = t // 3
			k = k + 1
			break
	while t % 2 == 0:
		if t % 4 == 0:
			t = t // 4
			k = k + 2
		elif t % 2 == 0:
			t = t // 2
			k = k + 1
			break
	if t != 1:
		print(-1)
	else:
		print(k)


def multi(x, y, moves):
	if x > y:
		return None
	elif x == y:
		return moves
	else:
		(a, b) = (multi(x * 2, y, moves + 1), multi(x * 3, y, moves + 1))
		if a is not None:
			return a
		elif b is not None:
			return b
n = [int(i) for i in input().split(' ')]
x = n[0]
y = n[1]
result = multi(x, y, 0)
if result is not None:
	print(result)
else:
	print(-1)


(x, y) = map(int, input().split())
count = 0
if y % x != 0:
	print(-1)
else:
	z = y // x
	while z % 2 == 0:
		z = z // 2
		count = count + 1
	while z % 3 == 0:
		count = count + 1
		z = z // 3
	if z != 1:
		print(-1)
	else:
		print(count)


(n, k) = map(int, input().split())
z = k / n
count = 0
while z != 1:
	if z % 2 == 0:
		z = z / 2
		count = count + 1
	elif z % 3 == 0:
		z = z / 3
		count = count + 1
	else:
		count = -1
		break
print(count)


(n, m) = map(int, input().split())
r = m / n
st = 0
while r % 2 == 0:
	st = st + 1
	r = r / 2
while r % 3 == 0:
	st = st + 1
	r = r / 3
if r == 1:
	print(st)
else:
	print('-1')


from math import log

def nod(a, b):
	while a * b != 0:
		if a > b:
			a %= b
		else:
			b %= a
	return a + b
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	three = 1350851717672992089
	thow = 576460752303423488
	m = m // n
	nd1 = nod(m, three)
	nd2 = nod(m, thow)
	ans = int(log(nd1, 3) + log(nd2, 2))
	if m != nd1 * nd2:
		print(-1)
	else:
		print(ans)


import math

def run():
	(n, m) = map(int, input().split())
	if n == m:
		print(0)
		return
	num_2s_n = get_num_times_k_divisible_by_n(n, 2)
	num_3s_n = get_num_times_k_divisible_by_n(n, 3)
	remaining = n // 2 ** num_2s_n // 3 ** num_3s_n
	if m % remaining != 0:
		print(-1)
		return
	num_2s_m = get_num_times_k_divisible_by_n(m, 2)
	num_3s_m = get_num_times_k_divisible_by_n(m, 3)
	num_moves_2 = num_2s_m - num_2s_n
	num_moves_3 = num_3s_m - num_3s_n
	if num_moves_2 < 0 or num_moves_3 < 0:
		print(-1)
		return
	n_after_moves = n * 2 ** num_moves_2 * 3 ** num_moves_3
	if n_after_moves != m:
		print(-1)
		return
	num_moves = num_moves_2 + num_moves_3
	print(num_moves)

def get_num_times_k_divisible_by_n(k, n):
	if n == 0:
		raise ValueError('Cannot divide by 0')
	elif n == 1:
		return math.inf
	num_times = 0
	while True:
		if k % n == 0:
			k //= n
			num_times += 1
		else:
			break
	return num_times
run()


(a, b) = [int(i) for i in input().split()]
n = 0
end = False
if b % a != 0:
	print(-1)
else:
	b //= a
	while b != 1:
		n += 1
		if b % 3 == 0:
			b //= 3
		elif b % 2 == 0:
			b //= 2
		else:
			print(-1)
			end = True
			break
	if not end:
		print(n)


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
(n, m) = map(int, input().split())
count = 0
p = m / n
if p % 2 == 0 or p % 3 == 0:
	while True:
		if p % 2 == 0:
			p = p // 2
			count += 1
		elif p % 3 == 0:
			p = p // 3
			count += 1
		else:
			break
	if p == 1:
		print(count)
	else:
		print(-1)
elif n == m:
	print(count)
else:
	print(-1)


import math
import random
import sys
from collections import defaultdict as dd
mod = 7 + 10 ** 9
col = dd(int)
g = dd(list)
(n, m) = [int(i) for i in input().split()]
if m % n != 0:
	print(-1)
elif m == n:
	print(0)
else:
	ans = 0
	a = m // n
	while a % 2 == 0:
		a = a // 2
		ans += 1
	while a % 3 == 0:
		a = a // 3
		ans += 1
	if a == 1:
		print(ans)
	else:
		print(-1)


(a, b) = map(int, input().split())
n = b / a
(p2, p3) = (0, 0)
m2 = 2
m3 = 3
while n % m2 == 0 or n % m3 == 0:
	if n % m2 == 0:
		p2 += 1
		m2 *= 2
	if n % m3 == 0:
		p3 += 1
		m3 *= 3
if 2 ** p2 * 3 ** p3 == n or n == 1:
	print(p2 + p3)
else:
	print(-1)


(n, m) = map(int, input().split())
k = m / n
p = 0
if m % n == 0:
	while k % 2 == 0 or (k % 3 == 0 and k != 0):
		p = p + 1
		if k % 2 == 0:
			k = k / 2
		else:
			k = k / 3
	if k == 1:
		print(p)
	else:
		print('-1')
else:
	print('-1')


import math
(n, m) = input().split()
n = int(n)
m = int(m)
s = m / n
two = 0
three = 0
block = 0
if s != math.floor(s):
	print(-1)
else:
	while s != 1:
		if s % 2 == 0:
			two = two + 1
			s = s / 2
		elif s % 3 == 0:
			three = three + 1
			s = s / 3
		else:
			print(-1)
			block = 1
			break
	if block == 0:
		print(two + three)


n = input().split()
(a, b) = (int(n[0]), int(n[1]))
ans = int(0)
if b % a != 0:
	print(-1)
else:
	now = int(b / a)
	while now % 2 == 0 and now > 0:
		now /= 2
		ans += 1
	while now % 3 == 0 and now > 0:
		now /= 3
		ans += 1
	if now != 1:
		print(-1)
	else:
		print(ans)


import sys
import math
fileoperation = 0
if fileoperation:
	orig_stdout = sys.stdout
	orig_stdin = sys.stdin
	inputfile = open('W:/Competitive Programming/input.txt', 'r')
	outputfile = open('W:/Competitive Programming/output.txt', 'w')
	sys.stdin = inputfile
	sys.stdout = outputfile
mod = 1000000007

def nospace(l):
	ans = ''.join((str(i) for i in l))
	return ans
t = 1
for tt in range(t):
	(n, m) = map(int, sys.stdin.readline().split(' '))
	v = m // n
	if m % n:
		print(-1)
		continue
	c = 0
	while v and v % 2 == 0:
		v = v // 2
		c += 1
	x = pow(2, c)
	cc = 0
	while v and v % 3 == 0:
		v = v // 3
		c += 1
		cc += 1
	y = pow(3, cc)
	if x * y * n != m:
		print(-1)
		continue
	print(c)
if fileoperation:
	sys.stdout = orig_stdout
	sys.stdin = orig_stdin
	inputfile.close()
	outputfile.close()


(m, n) = map(int, input().split())
if n % m == 0:
	i = 0
	n = n // m
	while n > 1 and (n % 3 == 0 or n % 2 == 0):
		if n % 3 == 0:
			n = n // 3
		elif n % 2 == 0:
			n = n // 2
		i += 1
	if n <= 1:
		print(i)
	else:
		print(-1)
else:
	print(-1)


(a, b) = list(map(int, input().split()))
if b % a == 0:
	c = b // a
	moves = 0
	while c % 2 == 0:
		c //= 2
		moves += 1
	while c % 3 == 0:
		c //= 3
		moves += 1
	if c == 1:
		print(moves)
	else:
		print(-1)
else:
	print(-1)


from collections import Counter
(a, b) = list(map(int, input().split()))

def pfac(factors, n):
	if n == 1:
		return factors
	if n % 2 == 0:
		factors.append(2)
		return pfac(factors, n // 2)
	for j in range(3, int(n ** 0.5) + 1, 2):
		if n % j == 0:
			factors.append(j)
			return pfac(factors, n // j)
	factors.append(n)
	return factors
f2 = pfac([], a)
f1 = pfac([], b)
flag = True
for thing in f2:
	try:
		f1.remove(thing)
	except:
		flag = False
		break
counter = 0
for i in f1:
	if i not in [2, 3]:
		flag = False
if flag:
	if not f1 and (not f2) and (a != b):
		print(-1)
	else:
		print(len(f1))
else:
	print(-1)


(a, b) = map(int, input().split())
count = 0
n = b // a
if a == b:
	print(0)
elif b % a != 0:
	print(-1)
else:
	while n % 2 == 0:
		count += 1
		n //= 2
	while n % 3 == 0:
		count += 1
		n //= 3
	if n == 1:
		print(count)
	else:
		print(-1)


(a, b) = map(int, input().split())
d = 2
res = 0
z = b // a if b % a == 0 else -1
while z > 1 and d < 4:
	if z % d == 0:
		z /= d
		res += 1
	else:
		d += 1
print(res if z == 1 and d < 4 else -1)


from sys import stdin, stdout
for t in range(1):
	(src, des) = list(map(int, stdin.readline().split()))
	if des % src != 0:
		print(-1)
	else:
		prod = des // src
		ans = 0
		while prod % 2 == 0:
			prod //= 2
			ans += 1
		while prod % 3 == 0:
			prod //= 3
			ans += 1
		if prod == 1:
			print(ans)
		else:
			print(-1)


X = list(map(int, input().split()))
if X[1] % X[0] != 0:
	print(-1)
	exit()
(Reminder, Count) = (X[1] // X[0], 0)
while Reminder % 3 == 0:
	(Count, Reminder) = (Count + 1, Reminder // 3)
while Reminder % 2 == 0:
	(Count, Reminder) = (Count + 1, Reminder // 2)
print(Count if Reminder == 1 else -1)


def sum_digits(n):
	r = 0
	while n:
		(r, n) = (r + n % 10, n // 10)
	return r
(n, m) = map(int, input().split())
case = True
if m == n:
	print(0)
elif m % n != 0:
	print(-1)
else:
	a = m / n
	count = 0
	while a != 1:
		if sum_digits(a) % 3 == 0:
			a /= 3
		elif int(str(a)[-1]) % 2 == 0:
			a /= 2
		else:
			case = False
			break
		count += 1
	if case == True:
		print(count)
	else:
		print(-1)


(n, m) = list(map(int, input().split()))
co = 0
if m % n != 0:
	print('-1')
else:
	m = m // n
	while m % 3 == 0:
		m = m // 3
		co += 1
	while m % 2 == 0:
		m = m // 2
		co += 1
	if m == 1:
		print(co)
	else:
		print('-1')


(a, b) = map(int, input().split())
cnt2 = 0
cnt3 = 0
k = b / a
if k - b // a > 0:
	print(-1)
else:
	k = b // a
	while k % 2 == 0:
		k //= 2
		cnt2 += 1
	while k % 3 == 0:
		k //= 3
		cnt3 += 1
	if k == 1:
		print(cnt2 + cnt3)
	else:
		print(-1)


import math
(n, m) = map(int, input().split())
if math.gcd(n, m) == n:
	num = m // n
	flag = 0
	ans = 0
	while num != 1:
		if num % 2 == 0:
			num = num // 2
			ans += 1
		elif num % 3 == 0:
			num = num // 3
			ans += 1
		else:
			flag = -1
			break
	if flag == -1:
		print(-1)
	else:
		print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
t = m // n
ex = m % n
if ex != 0:
	print(-1)
	exit()
for i in range(0, 51):
	for j in range(0, 51):
		x = 2 ** i * 3 ** j
		if x == t:
			print(i + j)
			exit()
		if x > t:
			break
print(-1)


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)
isdebug = False
try:
	import pylint
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
	isdebug = True
except Exception:

	def dprint(*args, **kwargs):
		pass

def red_inout():
	inId = 0
	outId = 0
	if not isdebug:
		inId = 0
		outId = 0
	if inId > 0:
		dprint('use input', inId)
		try:
			f = open('input' + str(inId) + '.txt', 'r')
			sys.stdin = f
		except Exception:
			dprint('invalid input file')
	if outId > 0:
		dprint('use output', outId)
		try:
			f = open('stdout' + str(outId) + '.txt', 'w')
			sys.stdout = f
		except Exception:
			dprint('invalid output file')
		atexit.register(lambda : sys.stdout.close())
if isdebug and len(sys.argv) == 1:
	red_inout()

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

def solve():
	pass
T_ = 1
for iii_ in range(T_):
	(N, M) = getIntList()
	r = 0
	if M % N != 0:
		print(-1)
		break
	a = M // N
	while a % 2 == 0:
		r += 1
		a //= 2
	while a % 3 == 0:
		r += 1
		a //= 3
	if a != 1:
		print(-1)
	else:
		print(r)


line = input().split()
b = int(line[0])
a = int(line[1])
two = 0
three = 0
if a % b == 0:
	if a == b:
		print('0')
	else:
		quot = a / b
		while quot > 1:
			if quot % 3 == 0:
				three += 1
				quot /= 3
				continue
			elif quot % 2 == 0:
				two += 1
				quot /= 2
				continue
			else:
				print('-1')
				quit()
		print(two + three)
else:
	print('-1')


(n, m) = map(int, input().split())
if n == m:
	print(0)
elif m / n != m // n:
	print(-1)
else:
	x = m // n
	flag = 1
	count = 0
	while x % 2 == 0:
		count += 1
		x = x // 2
	while x % 3 == 0:
		count += 1
		x = x // 3
	if x != 1:
		flag = 0
	if flag == 1:
		print(count)
	else:
		print(-1)


(m, n) = list(map(int, input().split()))
ans = -1
count = 0
if n % m == 0:
	d = n / m
	while d % 2 == 0:
		count += 1
		d /= 2
	while d % 3 == 0:
		count += 1
		d /= 3
	if d == 1:
		ans = count
print(ans)


(a, b) = map(int, input().split())
t = b / a
s = 0
p = 1
if t % 1 != 0:
	print(-1)
else:
	while t % 2 == 0:
		t = t / 2
		p = 0
		s = s + 1
	while t % 3 == 0:
		t = t / 3
		p = 0
		s = s + 1
	if t == 1:
		print(s)
	else:
		print(-1)


import math
import bisect

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 0:
		return int(input())
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	else:
		return sorted(list(map(int, input().split())))
(n, m) = f(1)
if m % n != 0:
	print(-1)
else:
	t = m // n
	a = 0
	b = 0
	while t != 0:
		if t % 3 == 0:
			a += 1
		else:
			break
		t = t // 3
	while t != 0:
		if t % 2 == 0:
			b += 1
		else:
			break
		t = t // 2
	if t > 1:
		print(-1)
	else:
		print(a + b)


(a, b) = list(map(int, input().split()))
pam = int(b // a)
if pam == b / a:
	sam = 0
	lam = 0
	while pam != 1:
		if pam % 2 == 0:
			pam = int(pam // 2)
			lam += 1
		elif pam % 3 == 0:
			pam = int(pam // 3)
			lam += 1
		else:
			sam = 1
			break
	if sam == 1:
		print(-1)
	else:
		print(lam)
else:
	print(-1)


vvod = input()
(n, m) = vvod.split()
n = int(n)
m = int(m)
k = m / n
if k > 1 and k % 2 != 0 and (k % 3 != 0):
	print('-1')
else:
	c = 0
	while k > 1:
		if k % 3 == 0:
			k = k / 3
			c = c + 1
			continue
		if k % 2 == 0:
			k = k / 2
			c = c + 1
			continue
		print('-1')
		break
	if k == 1:
		print(c)


(n, m) = map(int, input().split())
c = 0
if m % n == 0 and m != 1:
	m = m / n
	while m % 2 == 0:
		m = m / 2
		c += 1
	while m % 3 == 0:
		m = m / 3
		c += 1
	if m != 1:
		print(-1)
	else:
		print(c)
elif m == 1 and n == 1:
	print(0)
else:
	print(-1)


(n, m) = map(int, input().split())
res = -1
if m % n == 0:
	k = m // n
	count = 0
	while k % 2 == 0:
		k //= 2
		count += 1
	while k % 3 == 0:
		k //= 3
		count += 1
	if k == 1:
		res = count
print(res)


import sys

def solution(n, m):
	if m % n > 0:
		return -1
	k = m / n
	res = 0
	for i in [2, 3]:
		while k % i == 0:
			res += 1
			k /= i
	return [-1, res][k == 1]
(n, m) = map(int, sys.stdin.readline().strip().split(' '))
print(solution(n, m))


(n, m) = map(int, input().split())
ans = -1
if m % n == 0:
	o = m // n
	p = 0
	while o % 2 == 0:
		o //= 2
		p += 1
	while o % 3 == 0:
		o //= 3
		p += 1
	if o == 1:
		ans = p
print(ans)


def solve():
	inputs = input().split()
	n = int(inputs[0])
	m = int(inputs[1])
	if m % n != 0:
		print('-1')
		return
	start = m / n
	result = 0
	while True:
		if start == 1:
			break
		elif start % 2 == 0:
			start = start / 2
		elif start % 3 == 0:
			start = start / 3
		else:
			result = -1
			break
		result = result + 1
	print(result)
solve()


def next_int():
	return int(input())

def next_int_list():
	return [int(i) for i in input().split(' ')]

def next_float():
	return float(input())

def next_float_list():
	return [int(i) for i in input().split(' ')]

def next_string():
	return input()

def print_int_list(l):
	print(' '.join([str(i) for i in l]))
(n, m) = next_int_list()
if m % n != 0:
	print(-1)
else:
	div = m // n
	fact2 = 0
	while div > 0 and div % 2 == 0:
		div //= 2
		fact2 += 1
	fact3 = 0
	while div > 0 and div % 3 == 0:
		div //= 3
		fact3 += 1
	if div == 1:
		print(fact2 + fact3)
	else:
		print(-1)


(n, m) = map(int, input().split())
s = 0
if m / n == m // n:
	a = m // n
	while a % 3 == 0:
		a //= 3
		s += 1
	while a % 2 == 0:
		a >>= 1
		s += 1
	if a == 1:
		print(s)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
(target, rest) = divmod(m, n)

def main():
	d = target
	out = 0
	while d != 1:
		(a, b) = divmod(d, 2)
		if b:
			break
		d = a
		out += 1
	while d != 1:
		(a, b) = divmod(d, 3)
		if b:
			break
		d = a
		out += 1
	if d == 1:
		print(out)
	else:
		print(-1)
if rest:
	print(-1)
else:
	main()


(n, m) = map(int, input().split())
num = float('inf')
for i in range(61):
	for j in range(61):
		if n * 2 ** i * 3 ** j == m:
			num = min(i + j, num)
if num == float('inf'):
	print(-1)
else:
	print(num)


from sys import stdin, stdout
from collections import Counter

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

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

def ip():
	return int(stdin.readline().strip())

def op(ans):
	return stdout.write(str(ans) + '\n')
(n, m) = ei()
ans = -1
if m % n == 0:
	ans = 0
	d = m // n
	while d % 2 == 0:
		d //= 2
		ans += 1
	while d % 3 == 0:
		d //= 3
		ans += 1
	if d != 1:
		ans = -1
op(ans)


(a, b) = map(int, input().split())
(x, counter) = (b / a, 0)
if a == b:
	print(0)
	exit()
else:
	while x % 2 == 0:
		counter += 1
		x /= 2
	while x % 3 == 0:
		counter += 1
		x /= 3
	print(counter if x == 1 else -1)


nm = list(map(int, input().split()))
(n, m) = (nm[0], nm[1])
if m % n != 0:
	print(-1)
else:
	m //= n
	ans = 0
	while m != 1:
		if m % 2 == 0:
			m //= 2
		elif m % 3 == 0:
			m //= 3
		else:
			print(-1)
			break
		ans += 1
	else:
		print(ans)


(n, m) = [int(i) for i in input().split()]
q = m / n
r = 0
while q != 1:
	if q % 3 == 0:
		q = q / 3
		r += 1
	elif q % 2 == 0:
		q = q / 2
		r += 1
	else:
		print(-1)
		break
else:
	print(r)


(n, m) = map(int, input().split())
c2 = 0
c3 = 0
if m % n != 0:
	print(-1)
else:
	k = m // n
	while k != 1:
		if k % 2 == 0:
			c2 += 1
			k /= 2
		elif k % 3 == 0:
			c3 += 1
			k /= 3
		else:
			break
	if k != 1:
		print(-1)
	else:
		print(c2 + c3)


miu = input().split()
sella = int(miu[0])
delphine = int(miu[1])
eris = delphine / sella
if sella == delphine:
	print(0)
	exit()
ryude = 0
if eris - int(eris) != 0:
	print(-1)
else:
	while eris % 2 == 0:
		eris /= 2
		ryude += 1
	while eris % 3 == 0:
		eris /= 3
		ryude += 1
	if eris == 1:
		print(ryude)
	else:
		print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	n2 = 0
	n3 = 0
	ra = m // n
	while ra % 2 == 0:
		ra //= 2
		n2 += 1
	while ra % 3 == 0:
		ra //= 3
		n3 += 1
	if ra == 1:
		print(n2 + n3)
	else:
		print(-1)


(n, m) = input().split()
div = int(m) // int(n)
c = 0
if int(m) % int(n) == 0:
	for i in [2, 3]:
		while div % i == 0:
			div //= i
			c += 1
	if div == 1:
		print(c)
	else:
		print(-1)
else:
	print(-1)


import re, math, decimal, bisect

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]
(a, b) = viread()
moves = 0
if b % a != 0:
	print(-1)
	exit()
c = b // a
while c % 2 == 0:
	moves += 1
	c //= 2
while c % 3 == 0:
	moves += 1
	c //= 3
if c != 1:
	print(-1)
else:
	print(moves)


(x, y) = map(int, input().split())
k = -1
if y % x == 0:
	k = 0
	p = y / x
	while p % 2 == 0:
		p /= 2
		k += 1
	while p % 3 == 0:
		p /= 3
		k += 1
	if p != 1:
		k = -1
print(k)


(n, m) = list(map(int, input().split()))
if n == m:
	print('0')
else:
	import math
	if math.ceil(m / n) == math.floor(m / n):
		(k, i, j) = (m // n, 1, 1)
		while n * 2 ** i <= m:
			i += 1
		while n * 3 ** j <= m:
			j += 1
		l = 0
		for d in range(i + 1):
			for e in range(j + 1):
				if n * 2 ** d * 3 ** e == m:
					l += 1
					break
			if l == 1:
				break
		if l == 0:
			print('-1')
		else:
			print(d + e)
	else:
		print('-1')


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	di = m / n
	count = 0
	while di % 2 == 0:
		count += 1
		di /= 2
	while di % 3 == 0:
		count += 1
		di /= 3
	if di != 1:
		print(-1)
	else:
		print(count)


def main():
	s = list(map(int, input().split()))
	a = s[0]
	b = s[1]
	if b % a != 0:
		print(-1)
	else:
		b /= a
		n = 0
		while b % 2 == 0:
			b /= 2
			n += 1
		while b % 3 == 0:
			b /= 3
			n += 1
		if b != 1:
			print(-1)
		else:
			print(n)
main()


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
elif m == n:
	print(0)
else:
	p = m / n
	count2 = 0
	count3 = 0
	while p % 2 == 0 and p > 0:
		count2 += 1
		p /= 2
	while p % 3 == 0 and p > 0:
		count3 += 1
		p /= 3
	if p == 1:
		print(count2 + count3)
	else:
		print(-1)


(a, b) = map(int, input().split())
ans = 0
if b % a == 0:
	b /= a
	while b > 1:
		if b % 3 == 0:
			ans += 1
			b /= 3
		elif b % 2 == 0:
			ans += 1
			b /= 2
		else:
			ans = -1
			break
else:
	ans = -1
print(ans)


(f, m) = map(int, input().split())
if m % f == 0:
	cont = 0
	div = m / f
	while div % 2 == 0:
		cont += 1
		div = div / 2
	while div % 3 == 0:
		cont += 1
		div = div / 3
	if div == 1:
		print(cont)
	else:
		print(-1)
else:
	print(-1)


import math
(n, m) = map(int, input().split())
k = m / n
a = 0
b = 0
if k - math.trunc(k) == 0:
	while k % 2 == 0:
		k /= 2
		a += 1
	while k % 3 == 0:
		k /= 3
		b += 1
	if a + b == 0:
		if k == 1:
			print(a + b)
		else:
			print(-1)
	elif k == 1:
		print(a + b)
	else:
		print(-1)
else:
	print(-1)


def solve(n, m):
	if m % n != 0:
		return -1
	k = m // n
	moves = 0
	while k > 1 and k % 6 == 0:
		k //= 6
		moves += 2
	while k > 1 and k % 3 == 0:
		k //= 3
		moves += 1
	while k > 1 and k % 2 == 0:
		k //= 2
		moves += 1
	return -1 if k > 1 else moves

def main():
	(n, m) = map(int, input().split())
	print(solve(n, m))
main()


a = list((int(x) for x in input().split()))
sno = a[0]
lno = a[1]
ex2 = 0
ex3 = 0
r = lno / sno
t = r
while r % 2 == 0:
	ex2 += 1
	r /= 2
while r % 3 == 0:
	ex3 += 1
	r /= 3
if t < 1:
	print(-1)
elif t != int(t):
	print(-1)
elif t == 2 ** ex2 * 3 ** ex3:
	print(ex2 + ex3)
else:
	print(-1)


(n, m) = map(int, input().split())
if m / n != m // n:
	print(-1)
else:
	temp = m // n
	(res, f) = (0, 1)
	while temp:
		if temp % 3 == 0:
			temp /= 3
			res += 1
		elif temp % 2 == 0:
			temp /= 2
			res += 1
		elif temp == 1:
			res += 1
			break
		else:
			f = 0
			break
	if f:
		print(res - 1)
	else:
		print(-1)


(n, m) = map(int, input().split())
d = m / n
(c_2, c_3) = (0, 0)
if m % n == 0:
	while d % 2 == 0:
		d = d / 2
		c_2 += 1
	while d % 3 == 0:
		d = d / 3
		c_3 += 1
	if d == 1:
		print(c_2 + c_3)
	else:
		print('-1')
else:
	print('-1')


import sys
(a, b) = [int(i) for i in input().split()]
j = 0
if b != a:
	if b % a == 0:
		i = b / a
		while i > 3:
			if i % 3 == 0:
				i = i / 3
				j = j + 1
			elif i % 2 == 0:
				i = i / 2
				j += 1
			else:
				print(-1)
				sys.exit(0)
		print(j + 1)
	else:
		print(-1)
else:
	print(0)


import math

def primeFactors(n):
	ans = []
	while n % 2 == 0:
		ans.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			ans.append(i)
			n = n / i
	if n > 2:
		ans.append(n)
	return ans
(n, m) = list(map(int, input().strip().split()))
if n == m:
	print(0)
elif m % n == 0:
	check = primeFactors(m // n)
	if set(check) == {2, 3} or set(check) == {2} or set(check) == {3}:
		print(len(check))
	else:
		print(-1)
else:
	print(-1)


(a, b) = input().split()
n = int(a)
m = int(b)
flag = 0
if m / n == 1:
	print('0')
	flag = 1
elif m / n < 2 and m / n != 1 or m // n != m / n or (m % 2 != 0 and m % 3 != 0):
	print('-1')
	flag = 1
q = m / n
c = 0
e = q
flag1 = 0
if flag == 0:
	while q != 1:
		if q / 3 - q // 3 == 0:
			q = q / 3
			c += 1
		elif q / 2 - q // 2 == 0:
			q = q / 2
			c += 1
		e = e // 2
		if e == 0:
			flag1 = 1
			break
if flag == 0 and flag1 == 0:
	print(c)
elif flag == 0 and flag1 == 1:
	print('-1')


def game23_cf():
	(n, m) = list(map(int, input().split()))
	if m % n != 0:
		return -1
	k = m // n
	if k == 1:
		return 0
	r = 0
	while k % 2 == 0:
		k //= 2
		r += 1
		if k == 1:
			return r
	while k % 3 == 0:
		k //= 3
		r += 1
		if k == 1:
			return r
	return -1
print(game23_cf())


(n, m) = map(int, input().split(' '))
k = 0
t = m // n
while t % 2 == 0:
	t = t // 2
	k += 1
while t % 3 == 0:
	t = t // 3
	k += 1
if t != 1 or (t == 1 and m % n != 0):
	print(-1)
elif t == 1:
	print(k)


inp = input().split()
(n, m) = (int(inp[0]), int(inp[1]))

def ans(a, b):
	if b % a != 0:
		return -1
	d = b // a
	c = 0
	while d > 0 | d % 2 == 0:
		c += 1
		d = d // 2
	while d > 0 | d % 3 == 0:
		c += 1
		d = d // 3
	return c if d == 1 else -1
print(ans(n, m))


(n, m) = map(int, input().split())
res = -1
if m % n == 0:
	res = 0
	d = m // n
	while d % 2 == 0:
		d //= 2
		res += 1
	while d % 3 == 0:
		d //= 3
		res += 1
	if d != 1:
		res = -1
print(res)


(a, b) = map(int, input().split())
c = 0
for i in [2, 3]:
	while b % (a * i) == 0:
		a *= i
		c += 1
if a != b:
	print(-1)
else:
	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 pp(A):
	for i in A:
		print(i)

def solve():
	(n, m) = ip()
	if m % n != 0:
		return -1
	else:
		ans = 0
		k = int(m / n)
		while k != 1:
			if k % 2 == 0:
				k /= 2
				ans += 1
			elif k % 3 == 0:
				k /= 3
				ans += 1
			else:
				return -1
		return ans
print(solve())


a = input().split()
s = int(a[0])
d = int(a[1])
if d % s != 0:
	print(-1)
	exit()
a = d // s
n = 0
while a % 2 == 0:
	a //= 2
	n += 1
while a % 3 == 0:
	a //= 3
	n += 1
if a != 1:
	print(-1)
else:
	print(n)


import sys
from collections import defaultdict
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
(n, m) = intInp()

def countStep(n, m, step):
	if n == m:
		return step
	elif n > m:
		return -1
	else:
		a = countStep(n * 2, m, step + 1)
		b = countStep(n * 3, m, step + 1)
		if a == -1:
			return b
		elif b == -1:
			return a
		else:
			return min(a, b)
print(countStep(n, m, 0))


(a, b) = map(int, input().split())
d = 0
if b % a != 0:
	print(-1)
else:
	c = b // a
	while c != 1:
		if c % 3 == 0:
			c //= 3
			d += 1
		elif c % 2 == 0:
			c //= 2
			d += 1
		else:
			print(-1)
			break
	else:
		print(d)


(n, m) = (int(i) for i in input().split(' '))
if m % n == 0:
	k = m / n
	steps = 0
	while k != 0 and k % 6 == 0:
		k = k / 6
		steps = steps + 2
	while k != 0 and k % 3 == 0:
		k = k / 3
		steps = steps + 1
	while k != 0 and k % 2 == 0:
		k = k / 2
		steps = steps + 1
	if k == 1:
		print(steps)
	else:
		print('-1')
else:
	print('-1')


(n, m) = map(int, input().split())
ans = 0
oo = 1
if m % n != 0:
	print(-1)
elif n == m:
	print(0)
else:
	x = m // n
	while x > 1:
		if x % 2 == 0 or x % 3 == 0:
			if x % 2 == 0:
				ans += 1
				x = x // 2
			else:
				ans += 1
				x = x // 3
		else:
			oo = 11
			print(-1)
			break
	if oo != 11:
		print(ans)


(n, m) = list(map(int, input().split()))
a = 0
for d in [2, 3]:
	while m % (n * d) == 0:
		n *= d
		a += 1
if n != m:
	a = -1
print(a)


(n, m) = map(int, input().split())
c = 0
f = 0
d = m / n
while d % 2 == 0:
	d //= 2
	c += 1
while d % 3 == 0:
	d //= 3
	c += 1
if d == 1:
	f = 1
if f == 1:
	print(c)
else:
	print('-1')


(n, m) = map(int, input().split())
t = m // n
count = 0
if m == n:
	print(0)
	exit()
if m % n == 0 and (t % 3 == 0 or t % 2 == 0 or t % 6 == 0):
	while t > 0 and t % 6 == 0:
		count += 2
		t //= 6
	while t > 0 and t % 3 == 0:
		count += 1
		t //= 3
	while t > 0 and t % 2 == 0:
		count += 1
		t //= 2
	if t == 1:
		print(count)
	else:
		print(-1)
else:
	print(-1)


[n, m] = list(map(lambda x: int(x), input().split(' ')))
result = 0
tmp = 0
if m % n == 0:
	tmp = m / n
	while tmp % 2 == 0:
		tmp /= 2
		result += 1
	while tmp % 3 == 0:
		tmp /= 3
		result += 1
if tmp != 1:
	result = -1
print(result)


from sys import stdin, stdout
import bisect

def st():
	return list(stdin.readline().strip())

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, m) = mp()
	if m % n == 0:
		c = 0
		d = m // n
		while d % 2 == 0:
			d //= 2
			c += 1
		while d % 3 == 0:
			d //= 3
			c += 1
		if d != 1:
			pr(-1)
		else:
			pr(c)
	else:
		pr(-1)
for _ in range(1):
	solve()


from math import *

def main():
	(n, m) = map(int, input().split())
	if m % n > 0:
		print(-1)
		quit()
	else:
		step = 0
		while m // n % 2 == 0:
			n *= 2
			step += 1
		while m // n % 3 == 0:
			n *= 3
			step += 1
		if m == n:
			print(step)
		else:
			print(-1)
main()


(n, m) = map(int, input().split())
if n == m:
	print(0)
else:
	x = n
	lst = [x]
	while x <= m:
		x *= 3
		lst.append(x)
	(d, u) = ({1: 0}, 1)
	for i in range(1, 31):
		u *= 2
		d[u] = i
	(k, res) = (0, -1)
	for (i, x) in enumerate(lst):
		if x <= m:
			(div, mod) = (m // x, m % x)
			if mod == 0:
				if d.get(div) != None:
					res = i + d[div]
					break
	print(res)


(n, m) = map(int, str(input()).split())
moves = 0
while 1:
	if n == m:
		print(moves)
		break
	elif m % (n * 3) == 0:
		moves += 1
		n = n * 3
	elif m % (n * 2) == 0:
		moves += 1
		n = n * 2
	elif n > m:
		print('-1')
		break
	else:
		print('-1')
		break


(n, m) = list(map(int, input().split()))
count = 0

def solve(n, m, count=0):
	if m % n != 0:
		return -1
	if m == n:
		return count
	else:
		two = solve(2 * n, m, count + 1)
		three = solve(3 * n, m, count + 1)
		if two is not -1:
			return two
		elif three is not -1:
			return three
		else:
			return -1
print(solve(n, m))


import math
arr = list(map(int, input().split()))
x = arr[0]
y = arr[1]
if y % x != 0:
	print(-1)
else:
	x = y / x
	ans = 0
	while x % 2 == 0:
		x /= 2
		ans += 1
	while x % 3 == 0:
		x /= 3
		ans += 1
	if x == 1:
		print(ans)
	else:
		print(-1)


(n, m) = (int(t) for t in input().split(' '))
d = m // n
if m % n != 0 or d == 0:
	print(-1)
else:
	c2 = 0
	while d % 2 == 0:
		c2 += 1
		d //= 2
	c3 = 0
	while d % 3 == 0:
		c2 += 1
		d //= 3
	if d != 1:
		print(-1)
	else:
		print(c2 + c3)


(left, right) = map(int, input().split())

def calculating():
	if left == right:
		return 0
	if right % left != 0:
		return -1
	counter = 0
	x = right // left
	while x > 1:
		if x % 3 == 0:
			x //= 3
		elif x % 2 == 0:
			x //= 2
		else:
			return -1
		counter += 1
	return counter
print(calculating())


a = list(map(int, input().split()))
n = a[0]
m = a[1]
if m % n != 0:
	print(-1)
elif m == n:
	print(0)
else:
	b = m // n
	if b % 6 == 0:
		c = b // 6
		p = 2
		while c % 6 == 0:
			c = c // 6
			p = p + 2
		if c == 1:
			print(p)
		elif c % 2 == 0:
			q = 0
			while c % 2 == 0:
				c = c // 2
				q = q + 1
			if c == 1:
				print(p + q)
			else:
				print(-1)
		elif c % 3 == 0:
			r = 0
			while c % 3 == 0:
				c = c // 3
				r = r + 1
			if c == 1:
				print(p + r)
			else:
				print(-1)
		else:
			print(-1)
	elif b % 2 == 0:
		q = 0
		while b % 2 == 0:
			b = b // 2
			q = q + 1
		if b == 1:
			print(q)
		else:
			print(-1)
	elif b % 3 == 0:
		r = 0
		while b % 3 == 0:
			b = b // 3
			r = r + 1
		if b == 1:
			print(r)
		else:
			print(-1)
	else:
		print(-1)


arr = input()
(N, M) = [int(x) for x in arr.split(' ')]
if M % N != 0:
	print(-1)
else:
	res = 0
	while N < M:
		d = M // N
		if d % 3 == 0:
			N *= 3
			res += 1
		elif d % 2 == 0:
			N *= 2
			res += 1
		else:
			print(-1)
			quit()
	print(res)


(m, n) = map(int, input().split())
if n % m == 0:
	cout = 0
	t = int(n / m)
	flag = 1
	while t > 1:
		if t % 3 == 0:
			t = t / 3
			cout = cout + 1
		elif t % 2 == 0:
			t = t / 2
			cout = cout + 1
		else:
			flag = 0
			break
	if flag == 1:
		print(cout)
	else:
		print(-1)
else:
	print(-1)


(a, b) = [int(x) for x in input().split(' ')]
resp = b / a
flag = True
count = 0
while flag and resp != 1:
	if resp % 2 == 0:
		resp = resp // 2
		count += 1
	elif resp % 3 == 0:
		resp = resp // 3
		count += 1
	else:
		flag = False
if flag:
	print(count)
else:
	print(-1)


l = input()
l = l.split()
n = int(l[0])
m = int(l[1])
if m % n != 0:
	print(-1)
elif n == m:
	print(0)
else:
	s = 0
	a = m // n
	while a % 2 == 0:
		s += 1
		a = a / 2
	while a % 3 == 0:
		s += 1
		a = a / 3
	if a == 1:
		print(s)
	else:
		print(-1)


def movimientos(n, m):
	if m % n == 0:
		a = m // n
		cont = 0
		while a % 2 == 0:
			a = a // 2
			cont += 1
		while a % 3 == 0:
			a = a // 3
			cont += 1
		if a == 1:
			return cont
	return -1
l = input().split(' ')
print(movimientos(int(l[0]), int(l[1])))


(n, m) = map(int, input().split())
num = m / n
flag = 1
ans = 0
while num > 1:
	if num % 2 == 0:
		num //= 2
		ans += 1
		continue
	if num % 3 == 0:
		num //= 3
		ans += 1
		continue
	flag = 0
	break
if flag:
	print(ans)
else:
	print(-1)


[n, m] = [int(x) for x in input().split()]
flag = True
for i in range(50):
	for j in range(50):
		if n * 2 ** i * 3 ** j == m:
			print(i + j)
			flag = False
if flag:
	print(-1)


s = input().split()
n = int(s[0])
m = int(s[1])
flag = 0
no = 0
k = 1
if n == m:
	print('0')
	flag = 1
elif n < m:
	k = m / n
	while k > 1:
		if k % 6 == 0:
			k /= 6
			no += 2
		elif k % 3 == 0:
			k /= 3
			no += 1
		elif k % 2 == 0:
			k /= 2
			no += 1
		else:
			break
else:
	print('-1')
	flag = 1
if k == 1 and flag == 0:
	print(no)
elif k != 1 and flag == 0:
	print(-1)


import sys, math
(n, m) = map(int, sys.stdin.readline().split())
minn = 100
cur = 0
for i in range(31):
	for j in range(31):
		k = n
		k *= 2 ** i
		k *= 3 ** j
		cur = i + j
		if k == m:
			minn = min(minn, cur)
if minn == 100:
	print(-1)
else:
	print(minn)


from math import gcd
(a, b) = list(map(int, input().split()))
if a == b:
	print(0)
	exit(0)
for i in range(0, 30):
	for j in range(0, 30):
		if a * 2 ** i * 3 ** j == b:
			print(i + j)
			exit(0)
print(-1)


(n, m) = map(int, input().split())
ans = m / n
count = 0
if n == m:
	print(0)
elif ans % 5 == 0 or ans % 7 == 0 or ans % 11 == 0 or (ans % 13 == 0) or (ans % 17 == 0) or (ans % 19 == 0) or (ans % 23 == 0) or (ans % 29 == 0):
	print(-1)
elif ans % 2 != 0 and ans % 3 != 0:
	print(-1)
else:
	while ans > 1:
		if ans % 2 == 0:
			ans = ans // 2
			count += 1
		else:
			ans = ans // 3
			count += 1
	print(count)


Input = lambda : map(int, input().split())

def cal(m, num):
	counter = 0
	while m % num == 0:
		m //= num
		counter += 1
	return (m, counter)
(n, m) = Input()
if m % n != 0:
	print(-1)
	exit()
m //= n
(m, ans) = cal(m, 2)
(m, h) = cal(m, 3)
ans += h
print(ans if m == 1 else -1)


lista = [int(x) for x in input().split()]
if lista[1] % lista[0] != 0:
	print(-1)
else:
	numero = lista[1] / lista[0]
	contador = 0
	while numero > 1:
		if numero % 3 == 0:
			contador += 1
			numero = numero / 3
		elif numero % 2 == 0:
			contador += 1
			numero = numero / 2
		else:
			break
	if numero != 1:
		print(-1)
	else:
		print(contador)


(a, b) = map(int, input().split())
if a > b:
	print('-1')
elif a == b:
	print('0')
else:
	copy = b / a
	c = 0
	while copy % 2 == 0:
		copy = copy / 2
		c = c + 1
	while copy % 3 == 0:
		copy /= 3
		c = c + 1
	if copy == 1:
		print(c)
	else:
		print(-1)


(a, b) = map(int, input().split())
if a == b:
	print(0)
else:
	x = b / a
	cnt = 0
	while x % 3 == 0:
		x /= 3
		cnt += 1
	while x % 2 == 0:
		x /= 2
		cnt += 1
	print(cnt if x == 1 else -1)


(a, b) = map(int, input().split())
if b % a > 0:
	print(-1)
else:
	res = 0
	c = b // a
	while c % 2 == 0:
		c = c // 2
		res = res + 1
	while c % 3 == 0:
		c = c // 3
		res = res + 1
	if c != 1:
		print(-1)
	else:
		print(res)


from sys import exit
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit(0)
c = 0
t = m // n
while t % 2 == 0:
	t = t // 2
	c += 1
while t % 3 == 0:
	t = t // 3
	c += 1
if t == 1:
	print(c)
else:
	print(-1)


(n, m) = map(int, input().split())
count = 0
if m / n == m // n:
	quotient = m // n
	while quotient != 1:
		if quotient % 6 == 0:
			count += 2
			quotient /= 6
		elif quotient % 3 == 0:
			count += 1
			quotient /= 3
		elif quotient % 2 == 0:
			count += 1
			quotient /= 2
		else:
			print(-1)
			break
	else:
		print(count)
else:
	print(-1)


def div_2_3(n, m):
	result = 0
	numb = m // n
	if not m % n == 0:
		result = -1
		return -1
	while numb % 3 == 0:
		numb = numb // 3
		result += 1
	while numb % 2 == 0:
		numb = numb // 2
		result += 1
	if numb == 1:
		return result
	else:
		return -1
(n, m) = tuple([int(i) for i in input().split()])
print(div_2_3(n, m))


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
(n, m) = mi()
if m % n == 0:
	d = m // n
	c2 = c3 = 0
	while d % 2 == 0:
		c2 += 1
		d //= 2
	while d % 3 == 0:
		c3 += 1
		d //= 3
	print(c2 + c3 if d == 1 else -1)
else:
	print(-1)


(n, m) = map(int, input().split())
cou = 0
if not m % n:
	d = m // n
	while d > 1:
		if d % 2 == 0:
			cou += 1
			d = d // 2
		elif d % 3 == 0:
			cou += 1
			d = d // 3
		else:
			break
	if d == 1:
		print(cou)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
two = 0
three = 0
if m % n == 0:
	n = m / n
	while n % 2 == 0:
		n = n / 2
		two += 1
	while n % 3 == 0:
		n = n / 3
		three += 1
	if n == 1:
		print(two + three)
	else:
		print(-1)
else:
	print(-1)


(a, b) = map(int, input().split())
if b % a:
	print(-1)
else:
	count = 0
	nb = b // a
	while not nb % 2:
		nb = nb >> 1
		count += 1
	while not nb % 3:
		nb = nb // 3
		count += 1
	if nb == 1:
		print(count)
	else:
		print(-1)


def fun(n, m):
	if m % n != 0:
		return -1
	x = m // n
	ans = 0
	while x != 1:
		if x % 3 == 0:
			x = x // 3
			ans += 1
		elif x % 2 == 0:
			x = x // 2
			ans += 1
		else:
			x = 1
			ans = -1
	return ans
(n, m) = map(int, input().split())
print(fun(n, m))


(n, m) = map(int, input().split())
d = m // n
ans = 0
p = 1
while d % 3 == 0:
	ans += 1
	d //= 3
	p *= 3
while d % 2 == 0:
	ans += 1
	d //= 2
	p *= 2
if d != 1 or p * n != m:
	print(-1)
else:
	print(ans)


def main():
	(a, b) = map(int, input().split())
	if b % a != 0:
		print(-1)
		return
	n = b // a
	i = 0
	while n % 2 == 0:
		n //= 2
		i += 1
	while n % 3 == 0:
		n //= 3
		i += 1
	if n != 1:
		print(-1)
		return
	print(i)
main()


(n, m) = map(int, input().split())
t = 0
ans = 0
x = m
if m % n != 0:
	t = 1
else:
	m = m / n
while m % 2 == 0:
	m = m / 2
	ans = ans + 1
while m % 3 == 0:
	m = m / 3
	ans = ans + 1
if m != 1:
	t = 1
if t == 1:
	print(-1)
elif x == n:
	print(0)
else:
	print(ans)


(n, m) = map(int, input().split())
flag = 1
count = 0
if m % n != 0:
	flag = 0
else:
	noi = m / n
	while noi != 1 and noi % 2 == 0:
		noi = noi / 2
		count += 1
	while noi != 1 and noi % 3 == 0:
		noi = noi / 3
		count += 1
	if noi != 1:
		flag = 0
if flag == 1:
	print(count)
else:
	print('-1')


import sys
(n, m) = map(int, input().split())
x = m / n
if x % 2 == 0 or x % 3 == 0 or x == 1:
	ans = 0
	for i in {3, 2}:
		while x % i == 0:
			x //= i
			ans += 1
	if x != 1:
		print(-1)
	else:
		print(ans)
else:
	print(-1)


def solve():
	(n, m) = [int(x) for x in input().split()]
	if m % n > 0:
		print(-1)
		return
	factor = m // n
	i = 0
	while factor > 1:
		i += 1
		if factor % 3 == 0:
			factor //= 3
		elif factor % 2 == 0:
			factor //= 2
		else:
			print(-1)
			return
	print(i)
solve()


(n, m) = map(int, input().split())
count = 0
while True:
	if not m:
		print(-1)
		break
	elif m == n:
		print(count)
		break
	m = m // 3 if m / 3 % n == 0 else m // 2 if m / 2 % n == 0 else 0
	count += 1


import sys
DBG = True
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	sys.exit(0)
p = m // n
x = 0
while p % 2 == 0:
	x += 1
	p = p // 2
while p % 3 == 0:
	x += 1
	p = p // 3
if p == 1:
	print(x)
else:
	print(-1)


def fact(n):
	count = 0
	while n % 2 == 0:
		n = n // 2
		count += 1
	while n % 3 == 0:
		n = n // 3
		count += 1
	if n == 1:
		return count
	else:
		return -1
(x, y) = list(map(int, input().split()))
if y % x == 0:
	ans = y // x
	res = fact(ans)
	if res != -1:
		print(res)
	else:
		print(-1)
else:
	print(-1)


import sys

def solve(n, m):
	if m % n != 0:
		return -1
	x = m // n
	ans = 0
	while x != 1:
		if x % 3 == 0:
			x = x // 3
			ans += 1
		elif x % 2 == 0:
			x = x // 2
			ans += 1
		else:
			return -1
	return ans
(n, m) = map(int, sys.stdin.readline().split())
print(solve(n, m))


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 ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

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

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

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
(N, M) = MAP()
for i in range(30):
	for j in range(30):
		num = N * 2 ** i * 3 ** j
		if num > M:
			break
		elif num == M:
			print(i + j)
			exit()
print(-1)


(N, M) = list(map(int, input().split()))
d = M / N
if not d.is_integer():
	print(-1)
else:
	d = int(d)
	n = 0
	while d and d % 2 == 0:
		n += 1
		d >>= 1
	while d and d % 3 == 0:
		n += 1
		d //= 3
	if d == 1:
		print(n)
	else:
		print(-1)


import sys
(n, m) = [int(x) for x in input().strip().split()]
summ = 0
counter = 0
while m != n:
	counter = 0
	if m / n % 3 == 0:
		n = n * 3
		counter = 1
		summ += 1
	if m / n % 2 == 0:
		n = n * 2
		counter = 1
		summ += 1
	if counter == 0 and m != n:
		print(-1)
		sys.exit()
print(summ)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	m //= n
	while True:
		_0XFF = 0
		_ = 2
		while True:
			if m % _ == 0:
				m //= _
				_0XFF += 1
			elif _ == 3 and m != 1:
				print(-1)
				exit(0)
			elif _ == 3 and m == 1:
				print(_0XFF)
				exit(0)
			else:
				_ = 3


inp = list(map(int, input().split()))
(n, m) = (inp[0], inp[1])
bolum = 0
if m % n == 0:
	bolum = int(m / n)
else:
	print(-1)
	quit()
sonuc = 0
while bolum % 3 == 0:
	bolum = bolum / 3
	sonuc += 1
while bolum % 2 == 0:
	bolum = bolum / 2
	sonuc += 1
if (not bolum % 2 == 0 or not bolum % 3 == 0) and (not bolum == 1):
	sonuc = -1
print(sonuc)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	div = m / n
	a = 2
	b = 3
	c = 0
	while div % a == 0 and div != 0:
		c += 1
		div = div / a
	while div % b == 0 and div != 0:
		c += 1
		div = div / b
	if div == 1:
		print(c)
	else:
		print(-1)


R = lambda : map(int, input().split())
(n, m) = R()
if m % n:
	print(-1)
else:
	c = 0
	m //= n
	while m:
		if m % 2 == 0:
			m //= 2
		elif m % 3 == 0:
			m //= 3
		else:
			break
		c += 1
	if m == 1:
		print(c)
	else:
		print(-1)


def repeat(k, c):
	if k > 1:
		if k % 2 == 0:
			k = k // 2
			c = c + 1
			repeat(k, c)
		elif k % 3 == 0:
			k = k // 3
			c = c + 1
			repeat(k, c)
		else:
			print('-1')
	elif k == 1:
		print(c)
(n, m) = map(int, input().split())
k = m / n
c = 0
repeat(k, c)


(n, m) = [int(i) for i in input().split()]
cnt = 0
num = m / n
while 1:
	if num != int(num):
		print(-1)
		break
	elif num == 1:
		print(cnt)
		break
	elif num % 2 == 0:
		num /= 2
		cnt += 1
	else:
		num /= 3
		cnt += 1


(n, m) = map(int, input().split(' '))
if m % n != 0 or m < n:
	print('-1')
else:
	d = m // n
	steps = 0
	while d % 3 == 0:
		d = d // 3
		steps += 1
	while d % 2 == 0:
		d = d // 2
		steps += 1
	if d != 1:
		print('-1')
	else:
		print(steps)


p = input().rstrip().split(' ')
a = int(p[0])
b = int(p[1])
if b % a != 0 or a > b:
	print(-1)
elif a == b:
	print(0)
else:
	b = b // a
	X = 0
	F = 0
	while b - 1 != 0:
		if b % 3 == 0:
			b = b // 3
			X += 1
		elif b % 2 == 0:
			b = b // 2
			X += 1
		else:
			F = 1
			break
	if F == 1:
		print(-1)
	else:
		print(X)


k = [int(i) for i in input().split()]
res = 0
if k[1] / k[0] >= 2.0 and k[1] / k[0] % 3 == 0 or k[1] / k[0] % 2 == 0:
	while k[1] / k[0] > 1.0:
		if k[1] / k[0] % 3 == 0:
			res += 1
			k[0] *= 3
		elif k[1] / k[0] % 2 == 0:
			res += 1
			k[0] *= 2
		else:
			res = -1
			break
elif k[1] / k[0] > 1.0:
	res = -1
print(res)


x = list(map(int, input().split()))
n = 0
if x[1] % x[0] == 0:
	if x[1] == x[0]:
		print(0)
	else:
		z = x[1] // x[0]
		while z != 1:
			if z % 2 == 0:
				z = z // 2
			elif z % 3 == 0:
				z = z // 3
			else:
				n = -1
				break
			n += 1
		print(n)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	v = -1
else:
	v = m // n
	c = 0
	while v % 2 == 0:
		v = v // 2
		c = c + 1
	while v % 3 == 0:
		v = v // 3
		c = c + 1
	if v == 1:
		v = c
	else:
		v = -1
print(v)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	res = m // n
	counter = 0
	while res != 1:
		if res % 2 == 0:
			res //= 2
			counter += 1
		elif res % 3 == 0:
			res //= 3
			counter += 1
		else:
			print(-1)
			exit()
	print(counter)


nm = list(map(int, input().split()))
(n, m) = (nm[0], nm[1])
count = 0
factor = m / n
if factor % 1 != 0:
	print(-1)
else:
	while factor % 2 == 0:
		factor //= 2
		count += 1
	while factor % 3 == 0:
		factor //= 3
		count += 1
	if factor == 1:
		print(count)
	else:
		print(-1)


n = input()
l = [int(i) for i in n.split()]
first = l[0]
second = l[1]
if second % first == 0:
	number = second / first
	count = 0
	while number % 2 == 0:
		number = number / 2
		count += 1
	while number % 3 == 0:
		number = number / 3
		count += 1
	if number == 1:
		print(count)
	else:
		print(-1)
else:
	print(-1)


def solved(n, m):
	if m % n == 0:
		temp = m // n
		ans = 0
		while temp % 2 == 0:
			ans += 1
			temp //= 2
		if temp == 1:
			return ans
		while temp % 3 == 0:
			ans += 1
			temp //= 3
		if temp == 1:
			return ans
		else:
			return -1
	else:
		return -1
(n, m) = map(int, input().split())
count = solved(n, m)
print(count)


def fact(k):
	(a, b) = (0, 0)
	while k % 2 == 0:
		k //= 2
		a += 1
	while k % 3 == 0:
		k //= 3
		b += 1
	if k != 1:
		return -1
	return a + b
(n, m) = list(map(int, input().split()))
if n > m or m / n != m // n:
	print(-1)
elif n == m:
	print(0)
else:
	print(fact(m // n))


z = list(map(int, input().split()))
(n, m) = (z[0], z[1])
c = m / n
s = 0
if c % 1 == 0:
	while c != 1:
		if c % 2 == 0:
			s = s + 1
			c = c / 2
		elif c % 3 == 0:
			s = s + 1
			c = c / 3
		else:
			print(-1)
			break
	if int(c) == 1:
		print(s)
else:
	print(-1)


(a, b) = [int(i) for i in input().split(' ')]
if a == b:
	print(0)
else:
	c = 0
	l = [a]
	newl = []
	while l.count(b) == 0:
		if l[0] > b:
			print(-1)
			break
		for i in l:
			newl.append(i * 2)
			newl.append(i * 3)
		c = c + 1
		l = newl
		newl = []
		l = list(dict.fromkeys(l))
	if l.count(b) > 0:
		print(c)


(a, b) = [int(x) for x in input().split()]
ans = 0
if b % a == 0:
	b //= a
	while b != 1:
		if b % 2 == 0:
			b //= 2
		elif b % 3 == 0:
			b //= 3
		else:
			ans = -1
			break
		ans += 1
else:
	ans = -1
print(ans)


import sys
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')
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	a = m // n
	m = 0
	while a % 2 == 0:
		a = a // 2
		m += 1
	while a % 3 == 0:
		a = a // 3
		m += 1
	if a == 1:
		print(m)
	else:
		print(-1)


x = input().split()
x = int(x[1]) / int(x[0])
c = 0
if x % 1 != 0:
	x = 1
	c = -1
while x != 1:
	if x % 3 == 0:
		x /= 3
		c += 1
	elif x % 2 == 0:
		x /= 2
		c += 1
	else:
		x = 1
		c = -1
print(c)


(n, m) = map(int, input().split())
a = n * 1
b = m * 1
dn = [0, 0]
dm = [0, 0]
while True:
	if n % 2 == 0:
		n //= 2
		dn[0] += 1
	elif n % 3 == 0:
		n //= 3
		dn[1] += 1
	else:
		break
while True:
	if m % 2 == 0:
		m //= 2
		dm[0] += 1
	elif m % 3 == 0:
		m //= 3
		dm[1] += 1
	else:
		break
if dn[0] > dm[0] or dn[1] > dm[1]:
	print(-1)
else:
	a = a * 2 ** (dm[0] - dn[0])
	a = a * 3 ** (dm[1] - dn[1])
	if a == b:
		print(dm[0] - dn[0] + dm[1] - dn[1])
	else:
		print(-1)


(a, b) = input().split()
n = int(a)
m = int(b)
ans = -1
if m % n == 0:
	ans = 0
	m = m // n
	while m % 2 == 0:
		m = m // 2
		ans += 1
	while m % 3 == 0:
		m = m // 3
		ans += 1
	if m != 1:
		ans = -1
print(ans)


(n, m) = [int(x) for x in input().split()]
if int(m / n) != m / n:
	print(-1)
else:
	f = m / n
	k = 0
	while int(f / 2) == f / 2:
		k = k + 1
		f = f / 2
	while int(f / 3) == f / 3:
		k = k + 1
		f = f / 3
	if f == 1:
		print(k)
	else:
		print(-1)


[m, k] = list(map(int, input().split()))
check = 0
a = []
i = 0
steps = 0
if m == k:
	print(0)
elif k % m != 0:
	print(-1)
else:
	t = k // m
	while t != 1:
		if t % 2 == 0:
			t = t // 2
			steps += 1
		elif t % 3 == 0:
			t = t // 3
			steps += 1
		else:
			print(-1)
			i = 1
			break
	if i == 0:
		print(steps)


(N1, N2) = map(int, input().split())
count = 0
factor = N2 / N1
if factor < 1:
	print(-1)
else:
	while factor > 1:
		if factor % 2 == 0:
			factor /= 2
			count += 1
		elif factor % 3 == 0:
			factor /= 3
			count += 1
		else:
			print(-1)
			break
	if factor == 1:
		print(count)


firstString = input()
firstStringList = list(firstString.split(' '))
firstStringInt = list(map(int, firstStringList))
n = firstStringInt[0]
m = firstStringInt[1]
result = -1
if m % n == 0:
	result = 0
	d = m / n
	while d % 2 == 0:
		d /= 2
		result += 1
	while d % 3 == 0:
		d /= 3
		result += 1
	if d != 1:
		result = -1
print(result)


def divisor(n, div):
	res = 0
	while n != 0 and n % div == 0:
		n //= div
		res += 1
	return res

def main():
	(n, m) = map(int, input().split())
	(count_1_2, count_1_3) = (divisor(n, 2), divisor(n, 3))
	(count_2_2, count_2_3) = (divisor(m, 2), divisor(m, 3))
	n //= 2 ** count_1_2
	n //= 3 ** count_1_3
	m //= 2 ** count_2_2
	m //= 3 ** count_2_3
	if count_1_2 == count_2_2 and count_1_3 == count_2_3 and (n != m) or count_1_2 > count_2_2 or count_1_3 > count_2_3 or (n != m):
		return -1
	return count_2_2 - count_1_2 + (count_2_3 - count_1_3)
print(main())


[n, m] = input().split(' ')
n = int(n)
m = int(m)
s = 0
while True:
	k = m / n
	if m % n > 0:
		s = -1
		break
	elif k % 3 == 0:
		n = n * 3
		s = s + 1
		continue
	elif k % 2 == 0:
		n = n * 2
		s = s + 1
		continue
	elif k == 1:
		break
	else:
		s = -1
		break
print(s)


import math

def primeFactors(n):
	A = [0] * 4
	while n % 2 == 0:
		A[2] += 1
		n = n / 2
	while n % 3 == 0:
		A[3] += 1
		n = n / 3
	if int(n) == n and int(n) == 1:
		return A[2] + A[3]
	else:
		print('-1')
		exit()
(n, m) = map(int, input().split())
a = m / n
if int(a) == a:
	print(primeFactors(a))
else:
	print('-1')


a = list(map(int, input().split()))
ticlo = 0
if a[1] % a[0] != 0:
	ticlo = -1
else:
	num = a[1] // a[0]
	while num != 1:
		if num % 2 != 0:
			if num % 3 != 0:
				ticlo = -1
				break
			else:
				num //= 3
				ticlo += 1
		else:
			num //= 2
			ticlo += 1
print(ticlo)


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations

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

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

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
mm = 1000000007

def solve():
	t = 1
	for it in range(t):
		(n, m) = num()
		x = m / n
		if x != int(x):
			print(-1)
		elif x == 1:
			print(0)
		else:
			ff = 2
			cc = 0
			while x > 1:
				if x % ff == 0:
					cc += 1
					x = x // ff
				else:
					ff += 1
				if ff > 3:
					break
			if x != 1:
				print(-1)
			else:
				print(cc)
solve()


(n, m) = [int(x) for x in input().strip().split()]

def testCase(n, m):
	solution = [-1]
	starting_val = m

	def isDivBy2(val):
		return val % 2 == 0

	def isDivBy3(val):
		return val % 3 == 0

	def recure(val, steps):
		if solution[0] != -1:
			return
		if val <= n:
			if val == n:
				solution[0] = steps
			return
		if isDivBy2(val) and isDivBy3(val):
			recure(val // 2, steps + 1)
			recure(val // 3, steps + 1)
			return
		if isDivBy2(val):
			recure(val // 2, steps + 1)
		elif isDivBy3(val):
			recure(val // 3, steps + 1)
	recure(starting_val, 0)
	print(solution[0])
testCase(n, m)


s = [int(i) for i in input().split()]
n = s[0]
m = s[1]
counter = 0
rest = 0
if m == n:
	print(0)
elif m % n != 0 or (m % 2 != 0 and m % 3 != 0):
	print(-1)
else:
	rest = m / n
	while rest > 1:
		if rest % 2 == 0:
			rest /= 2
			counter += 1
		elif rest % 3 == 0:
			rest /= 3
			counter += 1
		else:
			counter = -1
			break
	print(counter)


__author__ = 'NIORLYS'
[n, m] = list(map(int, input().split()))
if m % n != 0:
	print(-1)
elif m == n:
	print(0)
else:
	count = 0
	res = m // n
	while res % 3 == 0:
		count += 1
		res //= 3
	while res % 2 == 0:
		count += 1
		res //= 2
	if res == 1:
		print(count)
	else:
		print(-1)


def pr(x):
	s = 2
	s1 = 0
	while x != 1:
		if x % s != 0:
			s += 1
			if s > 3:
				return -1
		else:
			x /= s
			s1 += 1
	return s1
(a, b) = map(int, input().split())
b = b / a
print(pr(b))


arr = [int(i) for i in input().split(' ')]
n = arr[0]
m = arr[1]
step = 0
if m % n != 0:
	print(-1)
elif m == n:
	print(0)
else:
	temp = m / n
	while temp % 3 == 0:
		step += 1
		temp /= 3
	while temp % 2 == 0:
		step += 1
		temp /= 2
	if temp != 1:
		print(-1)
	else:
		print(step)


ent = input()
(a, b) = ent.split()
c = int(b) / float(a)
cont = 0
quebrou = 1
while c > 1:
	if c % 2 == 0:
		cont += 1
		c /= 2
	elif c % 3 == 0:
		cont += 1
		c /= 3
	else:
		cont = -1
		break
print(cont)


def main():
	(n, m) = map(int, input().split())
	if m % n != 0:
		print(-1)
		return
	moves = 0
	ans = m // n
	while True:
		if ans % 2 == 0:
			moves += 1
			ans = ans / 2
		elif ans % 3 == 0:
			moves += 1
			ans = ans / 3
		else:
			break
	if ans != 1:
		print(-1)
		return
	print(moves)
main()


from collections import defaultdict

def prime_factorize(n):
	prime_factors = defaultdict(int)
	i = 2
	while i * i <= n:
		if n % i == 0:
			while n % i == 0:
				prime_factors[i] += 1
				n //= i
		i += 1
	if n != 1:
		prime_factors[n] += 1
	return prime_factors
(n, m) = map(int, input().split())
primes_n = prime_factorize(n)
primes_m = prime_factorize(m)
num_moves_3 = primes_m[3] - primes_n[3]
num_moves_2 = primes_m[2] - primes_n[2]
if num_moves_3 < 0 or num_moves_2 < 0:
	print(-1)
	exit(0)
if n * int(2 ** num_moves_2) * int(3 ** num_moves_3) != m:
	print(-1)
else:
	print(num_moves_2 + num_moves_3)


(n, m) = map(int, input().split())
z = m // n
cnt = 0
if z != m / n:
	print(-1)
	exit(0)
while z % 2 == 0 and z > 1:
	cnt += 1
	z = z // 2
while z % 3 == 0 and z > 1:
	cnt += 1
	z = z // 3
if z == 1:
	print(cnt)
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
(n, m) = map(int, input().split())
if m < n or m % n:
	ans = -1
elif n == m:
	ans = 0
else:
	m //= n
	a = factor(m)
	ans = 0
	for i in a:
		if i == 2 or i == 3:
			ans += 1
		else:
			ans = -1
			break
print(ans)


s = input()
s = s.split(' ')
n = int(s[0])
m = int(s[1])
count = 0
if m % n != 0:
	print(-1)
	exit()
else:
	n = int(m / n)
	while n != 1:
		if n % 2 == 0:
			count += 1
			n /= 2
		elif n % 3 == 0:
			count += 1
			n /= 3
		else:
			print(-1)
			exit()
print(count)


(n, m) = list(map(int, input().split()))
a = 0
while n % 2 == 0:
	a += 1
	n = n // 2
b = 0
while n % 3 == 0:
	b = b + 1
	n = n // 3
l = 0
while m % 2 == 0:
	l += 1
	m = m // 2
r = 0
while m % 3 == 0:
	r += 1
	m = m // 3
if n == m:
	if a > l or b > r:
		print(-1)
	else:
		ans = 0
		ans += l - a + r - b
		print(ans)
else:
	print(-1)


def solve():
	(n, m) = map(int, input().split())
	if m % n != 0:
		return -1
	m = m / n
	ans = 0
	while m % 2 == 0 or m % 3 == 0:
		if m % 2 == 0:
			m /= 2
			ans += 1
		if m % 3 == 0:
			m /= 3
			ans += 1
	if m != 1:
		return -1
	return ans
print(solve())


(a, b) = [int(i) for i in input().split()]
if a > b:
	print(-1)
else:
	r = b % a
	if r == 0:
		c = b // a
		nk = 0
		nd = 0
		while c > 1:
			if c % 2 == 0:
				c = c // 2
				nd += 1
			elif c % 3 == 0:
				c = c // 3
				nd += 1
			elif c != 1:
				print(-1)
				nk = 1
				break
		if nk == 0:
			print(nd)
	else:
		print(-1)


(n, m) = map(int, input().split())
x = m // n
if m % n:
	cnt = -1
elif x == 1 or x % 2 == 0 or x % 3 == 0:
	if x == 1:
		cnt = 0
	else:
		cnt = 0
		while x % 2 == 0:
			x /= 2
			cnt += 1
		while x % 3 == 0:
			x /= 3
			cnt += 1
else:
	cnt = -1
if x == 1:
	print(cnt)
else:
	print(-1)


import sys
import math
import collections
import heapq
input = sys.stdin.readline

def primeFactorisation(n):
	c = 0
	while n % 2 == 0:
		c += 1
		n //= 2
	while n % 3 == 0:
		c += 1
		n //= 3
	if n > 1:
		return -1
	else:
		return c
(n, m) = (int(i) for i in input().split())
if m % n != 0:
	print(-1)
else:
	print(primeFactorisation(m // n))


(n, m) = map(int, input().split())
(q, r) = divmod(m, n)
if r != 0:
	print(-1)
	exit()
moves = 0
while q & 1 == 0:
	q >>= 1
	moves += 1
while q % 3 == 0:
	q //= 3
	moves += 1
if q == 1:
	print(moves)
else:
	print(-1)


def count_step(m, n):
	u = 0
	w = m // n
	while w % 2 == 0:
		w //= 2
		u += 1
	while w % 3 == 0:
		w //= 3
		u += 1
	return (u, w)
(n, m) = map(int, input().strip().split())
if m % n != 0:
	print(-1)
else:
	(u, w) = count_step(m, n)
	if w == 1:
		print(u)
	else:
		print(-1)


(n, m) = map(int, input().split())
c1 = 0
c2 = 0
x = m // n
y = m % n
cv = x
if y == 0:
	while x % 2 == 0:
		x = x / 2
		c1 = c1 + 1
	while x % 3 == 0:
		x = x / 3
		c2 = c2 + 1
	z = 2 ** c1 * 3 ** c2
	if z == cv:
		print(c1 + c2)
	else:
		print('-1')
else:
	print('-1')


from sys import stdin, stdout
import math

def calculate(a, b):
	n = b / a
	cnt = 0
	if a == b:
		return 0
	if not int(n) == n:
		return -1
	else:
		while n == int(n):
			cnt += 1
			n = n / 2
		n = n * 2
		while n == int(n):
			cnt += 1
			n = n / 3
		n = n * 3
	if n == 1:
		return cnt - 2
	else:
		return -1
(a, b) = [int(x) for x in stdin.readline().rstrip().split()]
n = calculate(a, b)
stdout.write(str(n) + '\n')


gi = lambda : list(map(int, input().strip().split()))
(n, m) = gi()
ans = 0
if m % n:
	print(-1)
	exit()
m //= n
while m != 1:
	if m % 2 == 0:
		m //= 2
	elif m % 3 == 0:
		m //= 3
	else:
		print(-1)
		exit()
	ans += 1
print(ans)


(begin, end) = map(int, input().split())
co = 0
if end % begin != 0:
	print('-1')
else:
	end = end // begin
	while end % 3 == 0:
		end = end // 3
		co += 1
	while end % 2 == 0:
		end //= 2
		co += 1
	if end == 1:
		print(co)
	else:
		print('-1')


[n, m] = [int(x) for x in input().split(' ')]
ans = 0
x = m // n
if x * n != m:
	print('-1')
	exit(0)
while x % 2 == 0:
	x /= 2
	ans = ans + 1
while x % 3 == 0:
	x /= 3
	ans = ans + 1
if x != 1:
	print('-1')
else:
	print(ans)


def f(x):
	c = 0
	while True:
		if x == 1:
			return c
		elif not x % 2:
			x /= 2
			c += 1
		elif not x % 3:
			x /= 3
			c += 1
		else:
			return -1
(x, y) = map(int, input().split())
if y % x:
	print(-1)
else:
	print(f(y // x))


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit()
m //= n
k = 0
while m % 3 == 0 or m % 2 == 0:
	if m % 3 == 0:
		m //= 3
		k += 1
	if m % 2 == 0:
		m //= 2
		k += 1
print(k if m == 1 else -1)


(n, m) = map(int, input().split())
count = 0
if m == n:
	print(0)
elif m % n != 0:
	print(-1)
else:
	k = m // n
	i = k
	if k % 2 != 0 and k % 3 != 0:
		print(-1)
	else:
		while i != 1:
			if i % 3 == 0:
				i = i / 3
				count += 1
			elif i % 2 == 0:
				i = i / 2
				count += 1
			else:
				count = -1
				break
		print(count)


def solve(a):
	d2 = 0
	while a % 2 == 0:
		d2 += 1
		a = a // 2
	d3 = 0
	while a % 3 == 0:
		d3 += 1
		a = a // 3
	if a != 1:
		return -1
	return d2 + d3
(n, m) = map(int, input().split())
x = m / n
if x != int(x):
	print(-1)
else:
	print(solve(x))


inputRe = input()
mnArray = inputRe.split()
n = int(mnArray[0])
m = int(mnArray[1])
if m % n != 0:
	print(-1)
else:
	quotient = m / n
	twoIndex = 0
	threeIndex = 0
	numberMove = 0
	while quotient % 2 == 0:
		quotient = quotient // 2
		twoIndex += 1
	while quotient % 3 == 0:
		quotient = quotient // 3
		threeIndex += 1
	if quotient == 1:
		numberMove = threeIndex + twoIndex
		print(numberMove)
	else:
		print(-1)


[N, M] = map(int, input().split())
if N == M:
	print(0)
else:
	div = M // N
	if N * div != M:
		print(-1)
	else:
		result = 0
		while div != 1:
			if div % 2 == 0:
				result += 1
				div /= 2
				continue
			elif div % 3 == 0:
				result += 1
				div /= 3
				continue
			else:
				result = -1
				break
		print(result)


(a, b) = map(int, input().split())
if b % a == 0:
	c = b // a
	j = k = 0
	while c % 2 == 0 and c > 0:
		k += 1
		c //= 2
	while c % 3 == 0 and c > 0:
		j += 1
		c //= 3
	if b // a == 2 ** k * 3 ** j:
		print(j + k)
	else:
		print(-1)
else:
	print(-1)


inp = [int(i) for i in input().split(' ')]
count = 0
if inp[1] % inp[0] == 0:
	res = int(inp[1] / inp[0])
	while res > 1:
		if res % 2 == 0:
			count += 1
			res = int(res / 2)
		elif res % 3 == 0:
			count += 1
			res = int(res / 3)
		else:
			count = -1
			break
else:
	count = -1
print(count)


def solve_it(n, m):
	count = 0
	m = m / n
	while m > 1 and (m % 2 == 0 or m % 3 == 0):
		if m % 2 == 0:
			m = m / 2
			count += 1
		if m % 3 == 0:
			m = m / 3
			count += 1
	if m == 1:
		return count
	else:
		return -1
	return 0
line = input()
(n, m) = map(int, line.split())
print(solve_it(n, m))


def solve(n, m):
	if m < n:
		return -1
	if m == n:
		return 0
	r = m % n
	if r > 0:
		return -1
	d = m // n
	if d % 2 != 0 and d % 3 != 0:
		return -1
	ans = 0
	while d > 1:
		if d % 2 == 0:
			d //= 2
			ans += 1
		else:
			break
	if d == 1:
		return ans
	while d > 1:
		if d % 3 == 0:
			d //= 3
			ans += 1
		else:
			return -1
	if d != 1:
		return -1
	return ans
(n, m) = (int(i) for i in input().split())
print(solve(n, m))


(n, m) = map(int, input().split())
if m % n:
	print(-1)
else:
	d = m // n
	x = y = 0
	while d % 2 == 0:
		x += 1
		d //= 2
	while d % 3 == 0:
		y += 1
		d //= 3
	print(-1 if d != 1 else x + y)


(a, b) = input().split()
a = int(a)
b = int(b)
x = b / a
k = 0
while x > 0 and x % 2 == 0:
	x = x / 2
	k = k + 1
while x > 0 and x % 3 == 0:
	x = x / 3
	k = k + 1
if x == 1:
	print(k)
else:
	print(-1)


(n, m) = input().split()
n = int(n)
m = int(m)
if m % n == 0:
	k = m / n
	cnt = 0
	while k % 2 == 0:
		k = k / 2
		cnt = cnt + 1
	while k % 3 == 0:
		k = k / 3
		cnt = cnt + 1
	if k == 1:
		print(cnt)
	else:
		print(-1)
else:
	print(-1)


(n, m) = input().split()
n = int(n)
m = int(m)
if n == m:
	print(0)
elif m % n != 0:
	print(-1)
else:
	count = 0
	aux = m // n
	divisivel = True
	while divisivel and aux > 1:
		if aux % 3 == 0:
			aux = aux // 3
			count += 1
		elif aux % 2 == 0:
			aux = aux // 2
			count += 1
		else:
			divisivel = False
	if divisivel:
		print(count)
	else:
		print(-1)


(n, m) = map(int, input().split())
if m < n:
	print(-1)
elif m % n == 0:
	x = m // n
	steps = 0
	while x % 2 == 0:
		steps += 1
		x /= 2
	while x % 3 == 0:
		steps += 1
		x /= 3
	if x == 1:
		print(steps)
	else:
		print(-1)
else:
	print(-1)


(i, j) = list(map(int, input().split()))

def Game(i, j):
	if j % i:
		print(-1)
		return 0
	else:
		j //= i
		c = 0
		while j != 1:
			if not j % 3:
				j //= 3
				c += 1
			elif not j % 2:
				j //= 2
				c += 1
			else:
				print(-1)
				return 0
	print(c)
Game(i, j)


(n, m) = list(map(int, input().split()))
(twos, threes) = (0, 0)
if m % n != 0:
	print('-1')
else:
	dist = m // n
	while dist % 2 == 0:
		dist //= 2
		twos += 1
	while dist % 3 == 0:
		dist //= 3
		threes += 1
	print(twos + threes if dist == 1 else -1)


t = list(map(int, input().split()))
if t[0] == t[1]:
	print(0)
elif t[1] % t[0] == 0:
	x = t[1] // t[0]
	count = 0
	while x > 1:
		if x % 3 == 0:
			count = count + 1
			x = x // 3
			continue
		elif x % 2 == 0:
			count = count + 1
			x = x // 2
		else:
			count = -1
			break
	print(count)
else:
	print(-1)


(n, m) = list(map(int, input().split()))
N = m / 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:
	print(c2 + c3)
else:
	print(-1)


def line():
	return map(int, input().split())
from math import log, log2, isclose
(n, m) = line()
q = m // n
w = q & ~(q - 1)
e = log(q // w, 3)
if m % n != 0 or not isclose(e, round(e)):
	print(-1)
else:
	print(int(log2(w) + e))


(a, b) = input().split()
a = int(a)
b = int(b)
c = 0
k = b / a
if a == b:
	print(0)
elif b % a != 0:
	print(-1)
else:
	while k % 6 == 0:
		k /= 6
		c += 2
	while k % 3 == 0:
		k /= 3
		c += 1
	while k % 2 == 0:
		k /= 2
		c += 1
	if k == 1:
		print(c)
	else:
		print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	a = m // n
	a1 = a
	k2 = 0
	while a % 2 == 0:
		a //= 2
		k2 += 1
	k3 = 0
	while a % 3 == 0:
		a //= 3
		k3 += 1
	if n * 2 ** k2 * 3 ** k3 != m:
		print(-1)
	else:
		print(k2 + k3)


(a, b) = list(map(int, input().split()))
c = b % a == 0
if c:
	z = 0
	x = b // a
	while x % 2 == 0:
		x //= 2
		z += 1
	while x % 3 == 0:
		x //= 3
		z += 1
	if x == 1:
		print(z)
	else:
		print(-1)
else:
	print(-1)


(n, m) = input().split(' ')
c = 0
a = int(m) / int(n)
while a % 2 == 0:
	a = a / 2
	c += 1
while a % 3 == 0:
	a = a / 3
	c += 1
if a == 1:
	print(c)
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
(a, b) = map(int, input().split())
if a > b:
	print(-1)
elif a == b:
	print(0)
else:
	n = b // a
	if b % a != 0:
		print(-1)
	elif not (set(Factor(n)) == {2, 3} or set(Factor(n)) == {2} or set(Factor(n)) == {3}):
		print(-1)
	else:
		print(len(Factor(n)))


nums = input().split()
num1 = int(nums[0])
num2 = int(nums[1])
count = 0
div = num2 / num1
while div % 2 == 0:
	div /= 2
	count += 1
while div % 3 == 0:
	div /= 3
	count += 1
if div == 1:
	print(count)
else:
	print(-1)


def h(a):
	nd = 0
	while a % 2 == 0:
		nd += 1
		a = a // 2
	while a % 3 == 0:
		nd += 1
		a = a // 3
	if a == 1:
		return nd
	return -1
(a, b) = [int(i) for i in input().split()]
r = b % a
if r == 0:
	c = b // a
	if c == 1:
		print(0)
	else:
		d = h(c)
		print(d)
else:
	print(-1)


(n, m) = [int(i) for i in input().split()]
m2 = m3 = 0
n2 = n3 = 0
while m % 2 == 0:
	m2 += 1
	m //= 2
while m % 3 == 0:
	m3 += 1
	m //= 3
while n % 2 == 0:
	n2 += 1
	n //= 2
while n % 3 == 0:
	n3 += 1
	n //= 3
ans = m3 - n3 + m2 - n2
if n != m or n2 > m2 or n3 > m3:
	ans = -1
print(ans)


(a, b) = [int(i) for i in input().split()]
k = b // a
moves = 0
if b == a:
	print('0')
	exit()
if b % a == 0:
	while k > 0:
		flag = 0
		if k % 2 == 0:
			moves += 1
			k = k // 2
			flag = 1
		if k % 3 == 0:
			moves += 1
			k = k // 3
			flag = 1
		if flag == 0:
			moves = -1
			break
		if k == 1:
			break
	print(moves)
	exit()
print('-1')


(n, m) = map(int, input().split())
counter = 0
target = m / n
while target % 2 == 0 or target % 3 == 0:
	if target % 2 == 0:
		target = target / 2
		counter = counter + 1
	if target % 3 == 0:
		target = target / 3
		counter = counter + 1
if target == 1:
	print(counter)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n == 0 and m >= n:
	mov = 0
	m = m // n
	while m % 2 == 0:
		mov += 1
		m //= 2
		if m == 0:
			break
	while m % 3 == 0:
		mov += 1
		m //= 3
		if m == 0:
			break
	if m == 1:
		print(mov)
	else:
		print(-1)
else:
	print(-1)


import sys

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

def rop():
	return map(int, pro().split())

def main():
	a = list(rop())
	o = 0
	if a[1] % a[0] == 0:
		q = a[1] // a[0]
		while q % 2 == 0:
			q = q // 2
			o += 1
		while q % 3 == 0:
			q = q // 3
			o += 1
		if q != 1:
			print(-1)
		else:
			print(o)
	else:
		print(-1)
main()


from math import *
(n, m) = map(int, input().split())
x = m // n
ans = 0
if x * n == m:
	while x != 1:
		if x % 3 == 0:
			x = x // 3
			ans += 1
		elif x % 2 == 0:
			x = x // 2
			ans += 1
		else:
			print(-1)
			exit()
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
count = -1
if m % n == 0:
	if m / n == 1:
		count = 0
	a = m / n
	if a % 2 == 0 or a % 3 == 0:
		count = 0
	while a % 2 == 0 and a != 1:
		n = 2 * n
		a = m / n
		count += 1
	while a % 3 == 0 and a != 1:
		n = 3 * n
		a = m / n
		count += 1
	if a != 1:
		count = -1
print(count)


def pr(n):
	a = []
	d = 2
	while d * d <= n:
		if n % d == 0:
			a.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		a.append(n)
	return a
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	k = m // n
	d = pr(k)
	s = set(d)
	for i in s:
		if i != 2 and i != 3:
			print(-1)
			exit()
	print(len(d))


(n, m) = map(int, input().split())
if m / n != m // n:
	print(-1)
	exit(0)
a = 0
d = m / n
while d % 2 == 0:
	d /= 2
	a += 1
while d % 3 == 0:
	d /= 3
	a += 1
if d != 1:
	print(-1)
	exit(0)
print(a)


def main():
	(n, m) = map(int, input().split())
	for i in range(32):
		for j in range(32):
			if n * 2 ** i * 3 ** j == m:
				print(i + j)
				return
	print(-1)
main()


INF = 100000000
from collections import defaultdict
t = 1
for test in range(t):
	(n, m) = map(int, input().split())
	if m % n != 0:
		print(-1)
		continue
	n = m // n
	n2 = 0
	while n > 1:
		if n % 2 == 0:
			n2 += 1
			n = n // 2
		else:
			break
	n3 = 0
	while n > 1:
		if n % 3 == 0:
			n3 += 1
			n = n // 3
		else:
			break
	if n > 1:
		print(-1)
	else:
		print(n2 + n3)


(n, m) = input().split(' ')
n = int(n)
m = int(m)
if n == m:
	print(0)
elif m / n % 2 == 0 or m / n % 3 == 0:
	step = 0
	i = 0
	c = m / n
	while c % 3 == 0:
		c = c // 3
		step += 1
	while c % 2 == 0:
		c = c // 2
		step += 1
	if c == 1:
		print(step)
	else:
		print(-1)
else:
	print(-1)


(n, m) = [int(x) for x in input().split()]
if m % n == 0:
	t = int(m // n)
	if m == n:
		print(0)
	else:
		ans = 0
		while t % 3 == 0:
			t /= 3
			ans += 1
		while t % 2 == 0:
			t /= 2
			ans += 1
		if ans == 0 or not t == 1:
			print(-1)
		else:
			print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
dp2 = [-1 for _ in range(50)]
dp3 = [-1 for _ in range(50)]
dp2[0] = 1
dp3[0] = 1
for i in range(1, 50):
	dp2[i] = dp2[i - 1] * 2
	dp3[i] = dp3[i - 1] * 3
ans = -1
for i in range(50):
	if m == n * dp2[i]:
		print(i)
		exit()
	for j in range(1, 50):
		if m == n * dp2[i] * dp3[j]:
			print(i + j)
			exit()
print(ans)


(x, y) = map(int, input().split())
total = 0
if y % x == 0:
	t = y // x
	while t != 1:
		if t % 3 == 0:
			t = t // 3
			total += 1
		elif t % 2 == 0:
			t = t // 2
			total += 1
		else:
			total = -1
			break
	print(total)
else:
	print(-1)


(n, m) = map(int, input().split())
if m == n:
	print(0)
elif m % n != 0:
	print('-1')
else:
	a = m // n
	if a % 2 != 0 and a % 3 != 0:
		print('-1')
	else:
		ans = 0
		while a % 2 == 0:
			a = a // 2
			ans += 1
		while a % 3 == 0:
			a = a // 3
			ans += 1
		if a == 1:
			print(ans)
		else:
			print('-1')


(x, y) = map(int, input().split())
if y == x:
	print(0)
elif y < x or y % x != 0:
	print(-1)
else:
	hod = 0
	g = y / x
	while g > 1:
		if g % 3 and g % 2:
			hod = -1
			break
		if g % 3:
			g = g / 2
		else:
			g = g / 3
		hod = hod + 1
	print(hod)


def normalize(n, modulo):
	result = 0
	while n % modulo == 0:
		result += 1
		n /= modulo
	return (n, result)

def solution(n, m):
	if m < n:
		return -1
	(n, n_double) = normalize(n, 2)
	(m, m_double) = normalize(m, 2)
	(n, n_triple) = normalize(n, 3)
	(m, m_triple) = normalize(m, 3)
	if n != m or m_double < n_double or m_triple < n_triple:
		return -1
	return m_triple + m_double - n_double - n_triple
(n, m) = map(int, input().split(' '))
print(solution(n, m))


def solve(n, m):
	if m % n != 0:
		return -1
	k = m / n
	undividable = False
	steps = 0
	while k > 1 and (not undividable):
		if k % 2 == 0:
			k = k / 2
			steps += 1
		elif k % 3 == 0:
			k = k / 3
			steps += 1
		else:
			undividable = True
	if undividable:
		return -1
	else:
		return steps
(n, m) = map(int, input().split())
print(solve(n, m))


(n, m) = map(int, input().strip().split(' '))
if m % n != 0:
	print('-1')
else:
	f = 0
	p = m // n
	c = 0
	while p != 1:
		if p % 2 != 0 and p % 3 != 0:
			print('-1')
			f = 1
			break
		elif p % 3 == 0:
			p = p // 3
			c += 1
		else:
			p = p // 2
			c += 1
	if f == 0:
		print(c)


(k, m) = list(map(int, input().split()))
if m % k == 0:
	s = m // k
	i = 0
else:
	i = -1
	s = 1
while s != 1:
	if s % 3 == 0:
		s //= 3
		i += 1
	elif s % 2 == 0:
		s //= 2
		i += 1
	else:
		i = -1
		break
print(i)


(x, y) = map(int, input().split())
if y % x:
	print('-1')
else:
	y /= x
	ans = 0
	while y % 2 == 0:
		y /= 2
		ans += 1
	while y % 3 == 0:
		y /= 3
		ans += 1
	if y == 1:
		print(ans)
	else:
		print('-1')


(n, m) = map(int, input().split())
if m % n == 0:
	x = int(m / n)
	c = 0
	s = 0
	while x > 1:
		if x % 2 and x % 3 and x % 6:
			print(-1)
			s = 1
			break
		elif x % 2 == 0:
			x /= 2
		else:
			x /= 3
		c += 1
	if not s:
		print(c)
else:
	print(-1)


import copy

def helper(m):
	(n_2, n_3) = (0, 0)
	while m % 2 == 0:
		m //= 2
		n_2 += 1
	while m % 3 == 0:
		m //= 3
		n_3 += 1
	return (n_2, n_3, m)

def test_helper():
	(result, primes) = helper(20)
	print('primes:', primes)
	for (i, ele) in enumerate(result, 1):
		print(i, ele)

def solve(n, m):
	(n_2, n_3, n_r) = helper(n)
	(m_2, m_3, m_r) = helper(m)
	if m_r == n_r and m_2 >= n_2 and (m_3 >= n_3):
		return m_2 + m_3 - n_2 - n_3
	else:
		return -1

def main():
	(n, m) = map(int, input().split())
	print(solve(n, m))
main()


import sys
(n, m) = map(int, input().split())
q = m / n
counter = 0
if not q.is_integer():
	print(-1)
	sys.exit()
while (q / 2).is_integer():
	q /= 2
	counter += 1
while (q / 3).is_integer():
	q /= 3
	counter += 1
if q == 1:
	print(counter)
else:
	print(-1)


(n, m) = list(map(int, input().split()))
if m % n != 0:
	print(-1)
	exit()
ans = 0
d = m // n
while d % 3 == 0:
	d /= 3
	ans += 1
while d % 2 == 0:
	d /= 2
	ans += 1
if d != 1:
	print(-1)
else:
	print(ans)


a = 0
(first, second) = input().split()
first = int(first)
second = int(second)
k = second / first
if k != int(k):
	print(-1)
else:
	while True:
		if k == 1:
			print(a)
			break
		elif k > 1:
			if k % 2 == 0:
				k = k / 2
				a += 1
			elif k % 3 == 0:
				k = k / 3
				a += 1
			else:
				print(-1)
				break


from sys import stdin
(n, m) = list(map(int, stdin.readline().split()))
if m % n == 0:
	m = m // n
	count = 0
	while m % 2 == 0 and m > 1:
		m //= 2
		count += 1
	while m % 3 == 0 and m > 1:
		m //= 3
		count += 1
	if m == 1:
		print(count)
	else:
		print(-1)
else:
	print(-1)
	exit()


(n, m) = list(map(int, input().split()))
if m % n:
	print(-1)
else:
	m //= n
	n = 0
	while not m & 1:
		m >>= 1
		n += 1
	while m % 3 == 0:
		m //= 3
		n += 1
	print(-1 if m != 1 else n)


text = input()
t = text.split()
n = int(t[0])
m = int(t[1])
ans = 0

def r(f, step):
	if f == 1:
		print(step)
		return True
	if f % 3 == 0:
		if r(f / 3, step + 1):
			return True
	elif f % 2 == 0:
		if r(f / 2, step + 1):
			return True
	else:
		print(-1)
	return False
if int(m / n) != m / n:
	print(-1)
else:
	r(m / n, 0)


def play23(n, m):
	if m % n != 0:
		return -1
	result = 0
	ratio = m // n
	while ratio % 3 == 0:
		result += 1
		ratio //= 3
	while ratio % 2 == 0:
		result += 1
		ratio //= 2
	if ratio != 1:
		return -1
	return result
(N, M) = [int(i) for i in input().split()]
print(play23(N, M))


(n, m) = map(int, input().split())
d = dict()
if m % n != 0:
	print(-1)
	exit()
m //= n
ans = 0
while m % 2 == 0 or m % 3 == 0:
	if m % 2 == 0:
		m //= 2
		ans += 1
	if m % 3 == 0:
		m //= 3
		ans += 1
if m != 1:
	print(-1)
	exit()
print(ans)


(n, m) = list(map(int, input().split()))
if n == m:
	print(0)
elif n > m:
	print(-1)
else:
	k = m / n
	if k == int(k):
		cnt = z = 0
		k = int(k)
		while k != 1:
			if k % 3 == 0:
				k = k // 3
				cnt += 1
			elif k % 2 == 0:
				k = k // 2
				cnt += 1
			else:
				z = 1
				break
		if z:
			print(-1)
		else:
			print(cnt)
	else:
		print(-1)


z = str(input()).split()
kecil = int(z[0])
besar = int(z[1])
counter = 0
if besar % kecil != 0:
	print(-1)
else:
	d = besar / kecil
	while d:
		if d == 1:
			break
		if d % 2 == 0:
			d = d // 2
			counter += 1
		elif d % 3 == 0:
			d = d // 3
			counter += 1
		else:
			k = 0
			counter = -1
			break
	print(counter)


(x, y) = list(map(int, input().split()))
if y % x == 0:
	res = 0
	d = y / x
	while d % 2 == 0:
		d /= 2
		res += 1
	while d % 3 == 0:
		d /= 3
		res += 1
	if d != 1:
		res = -1
else:
	res = -1
print(res)


def divisionsBy2(number):
	if number % 2 == 0:
		return divisionsBy3(number / 2) + 1
	return 0

def divisionsBy3(number):
	if number % 3 == 0:
		return divisionsBy3(number / 3) + 1
	return 0

def main():
	(n, m) = map(int, input().split())
	moves = 0
	if m % n != 0:
		print(-1)
	elif m == n:
		print(0)
	else:
		div = m / n
		while div % 3 == 0:
			div = div / 3
			moves += 1
		while div % 2 == 0:
			div = div / 2
			moves += 1
		if div == 1:
			print(moves)
		else:
			print(-1)
main()


def fat32(num):
	c3 = 0
	c2 = 0
	while num % 3 == 0:
		num = num // 3
		c3 += 1
	while num % 2 == 0:
		num = num // 2
		c2 += 1
	return (num == 1, c2, c3)
(n, m) = map(int, input().split())
cont = -1
if m % n == 0:
	temp = m // n
	(valida, c2, c3) = fat32(temp)
	if valida:
		cont = c2 + c3
print(cont)


(n, m) = list(map(int, input().split()))
x = y = 0
for i in range(50):
	if m // n % 2 ** i == 0:
		x = i
for i in range(50):
	if m // n % 3 ** i == 0:
		y = i
if 2 ** x * 3 ** y * n == m:
	print(x + y)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	d = m // n
	twos = 0
	while d % 2 == 0:
		d //= 2
		twos += 1
	threes = 0
	while d % 3 == 0:
		d //= 3
		threes += 1
	if d != 1:
		print(-1)
	else:
		print(twos + threes)


(n, m) = [int(x) for x in input().split()]
p = 0
div = m / n
while div % 2 == 0:
	p += 1
	div /= 2
while div % 3 == 0:
	p += 1
	div /= 3
if div != 1:
	p = -1
print(p)


import math

def primeFactors(n):
	A = []
	while n % 2 == 0:
		A.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			A.append(i)
			n = n // i
	if n > 2:
		A.append(n)
	return A
(n, m) = map(int, input().split())
if m % n == 0:
	a = m // n
	b = primeFactors(a)
	if set(b) == {2, 3} or set(b) == {2} or set(b) == {3}:
		print(len(b))
	elif len(b) == 0:
		print(0)
	else:
		print(-1)
else:
	print(-1)


from __future__ import division, print_function
import math
import os
import sys
from fractions import *
from sys import *
from decimal import *
from io import BytesIO, IOBase
from itertools import *
from collections import *
M = 10 ** 9 + 7
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

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 fsep():
	return map(float, inp().split())

def inpu():
	return int(inp())

def regularbracket(t):
	p = 0
	for i in t:
		if i == '(':
			p += 1
		else:
			p -= 1
		if p < 0:
			return False
	else:
		if p > 0:
			return False
		else:
			return True

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def reverse1(string):
	pp = ''
	for i in string[::-1]:
		pp += i
	if pp == string:
		return True
	return False

def reverse2(list1):
	l = []
	for i in list1[::-1]:
		l.append(i)
	if l == list1:
		return True
	return False

def mex(list1):
	p = max(list1) + 1
	for i in range(len(list1)):
		if list1[i] != i:
			p = i
			break
	return p

def sumofdigits(n):
	n = str(n)
	s1 = 0
	for i in n:
		s1 += int(i)
	return s1

def perfect_square(n):
	s = math.sqrt(n)
	if s == int(s):
		return True
	return False

def roman_number(x):
	if x > 15999:
		return
	value = [5000, 4000, 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
	symbol = ['F', 'MF', 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
	roman = ''
	i = 0
	while x > 0:
		div = x // value[i]
		x = x % value[i]
		while div:
			roman += symbol[i]
			div -= 1
		i += 1
	return roman

def soretd(s):
	for i in range(1, len(s)):
		if s[i - 1] > s[i]:
			return False
	return True

def countRhombi(h, w):
	ct = 0
	for i in range(2, h + 1, 2):
		for j in range(2, w + 1, 2):
			ct += (h - i + 1) * (w - j + 1)
	return ct

def countrhombi2(h, w):
	return h * h // 4 * (w * w // 4)

def binpow(a, b):
	if b == 0:
		return 1
	else:
		res = binpow(a, b // 2)
	if b % 2 != 0:
		return res * res * a
	else:
		return res * res

def binpowmodulus(a, b, m):
	a %= m
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b >>= 1
	return res

def coprime_to_n(n):
	result = n
	i = 2
	while i * i <= n:
		if n % i == 0:
			while n % i == 0:
				n //= i
			result -= result // i
		i += 1
	if n > 1:
		result -= result // n
	return result

def prime(x):
	if x == 1:
		return False
	else:
		for i in range(2, int(math.sqrt(x)) + 1):
			if x % i == 0:
				return False
		else:
			return True

def luckynumwithequalnumberoffourandseven(x, n, a):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	elif x < 1000000000000.0:
		luckynumwithequalnumberoffourandseven(x * 10 + 4, n, a)
		luckynumwithequalnumberoffourandseven(x * 10 + 7, n, a)
	return a

def luckynum(x, l, r, a):
	if x >= l and x <= r:
		a.append(x)
	if x > r:
		a.append(x)
		return a
	if x < 10000000000.0:
		luckynum(x * 10 + 4, l, r, a)
		luckynum(x * 10 + 7, l, r, a)
	return a

def luckynuber(x, n, a):
	p = set(str(x))
	if len(p) <= 2:
		a.append(x)
	if x < n:
		luckynuber(x + 1, n, a)
	return a

def interact(type, x):
	if type == 'r':
		inp = input()
		return inp.strip()
	else:
		print(x, flush=True)

def findTrailingZeros(n):
	count = 0
	while n >= 5:
		n //= 5
		count += n
	return count

def mergeSort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		L = arr[:mid]
		R = arr[mid:]
		mergeSort(L)
		mergeSort(R)
		i = j = k = 0
		while i < len(L) and j < len(R):
			if L[i] < R[j]:
				arr[k] = L[i]
				i += 1
			else:
				arr[k] = R[j]
				j += 1
			k += 1
		while i < len(L):
			arr[k] = L[i]
			i += 1
			k += 1
		while j < len(R):
			arr[k] = R[j]
			j += 1
			k += 1
res = set()

def solven(p, l, a, b, n):
	if p > n or l > 10:
		return
	if p > 0:
		res.add(p)
	solven(p * 10 + a, l + 1, a, b, n)
	solven(p * 10 + b, l + 1, a, b, n)

def subsetsUtil(A, subset, index, d):
	print(*subset)
	s = sum(subset)
	d.append(s)
	for i in range(index, len(A)):
		subset.append(A[i])
		subsetsUtil(A, subset, i + 1, d)
		subset.pop(-1)
	return d

def subsetSums(arr, l, r, d, sum=0):
	if l > r:
		d.append(sum)
		return
	subsetSums(arr, l + 1, r, d, sum + arr[l])
	subsetSums(arr, l + 1, r, d, sum)
	return d

def print_factors(x):
	factors = []
	for i in range(1, x + 1):
		if x % i == 0:
			factors.append(i)
	return factors

def calc(X, d, ans, D):
	if len(X) == 0:
		return
	i = X.index(max(X))
	ans[D[max(X)]] = d
	Y = X[:i]
	Z = X[i + 1:]
	calc(Y, d + 1, ans, D)
	calc(Z, d + 1, ans, D)

def factorization(n, l):
	c = n
	if prime(n) == True:
		l.append(n)
		return l
	for i in range(2, c):
		if n == 1:
			break
		while n % i == 0:
			l.append(i)
			n = n // i
	return l

def good(b):
	l = []
	i = 0
	while len(b) != 0:
		if b[i] < b[len(b) - 1 - i]:
			l.append(b[i])
			b.remove(b[i])
		else:
			l.append(b[len(b) - 1 - i])
			b.remove(b[len(b) - 1 - i])
	if l == sorted(l):
		return True
	return False

def generate(st, s):
	if len(s) == 0:
		return
	if s not in st:
		st.add(s)
		for i in range(len(s)):
			t = list(s).copy()
			t.remove(s[i])
			t = ''.join(t)
			generate(st, t)
	return

def largestincreasingsubsequence(A):
	l = [1] * len(A)
	sub = []
	for i in range(1, len(l)):
		for k in range(i):
			if A[k] < A[i]:
				sub.append(l[k])
		l[i] = 1 + max(sub, default=0)
	return max(l, default=0)

def findOR(nums, N):
	prefix_sum = 0
	result = 0
	for i in range(N):
		result |= nums[i]
		prefix_sum += nums[i]
		result |= prefix_sum
	return result

def OR(a, n):
	ans = a[0]
	for i in range(1, n):
		ans |= a[i]
	return ans

def toString(List):
	return ''.join(List)
p = []

def permute(a, l, r):
	if l == r:
		p.append(toString(a))
	else:
		for i in range(l, r + 1):
			(a[l], a[i]) = (a[i], a[l])
			permute(a, l + 1, r)
			(a[l], a[i]) = (a[i], a[l])

def squareRoot(number, precision):
	start = 0
	(end, ans) = (number, 1)
	while start <= end:
		mid = int((start + end) / 2)
		if mid * mid == number:
			ans = mid
			break
		if mid * mid < number:
			start = mid + 1
		else:
			end = mid - 1
	increment = 0.1
	for i in range(0, precision):
		while ans * ans <= number:
			ans += increment
		ans = ans - increment
		increment = increment / 10
	return ans

def main():
	(n, m) = sep()
	cnt = 0
	if m % n != 0:
		print(-1)
	else:
		q = m // n
		cnt = 0
		while q % 2 == 0:
			q //= 2
			cnt += 1
		while q % 3 == 0:
			q //= 3
			cnt += 1
		if q == 1:
			print(cnt)
		else:
			print(-1)
main()


(q, w) = map(int, input().split())
z = 0
if w % q != 0:
	print(-1)
else:
	x = w // q
	while x % 2 == 0:
		x = x // 2
		z += 1
	while x % 3 == 0:
		x //= 3
		z += 1
	if x == 1:
		print(z)
	else:
		print(-1)


(n, m) = [int(s) for s in input().split()]
if m % n != 0:
	print(-1)
else:
	r = m // n
	step = 0
	while r % 2 == 0:
		r //= 2
		step += 1
	while r % 3 == 0:
		r //= 3
		step += 1
	if r != 1:
		print(-1)
	else:
		print(step)


def f(m, n):
	if n % m != 0:
		return -1
	k = n // m
	changes = 0
	while k > 1:
		if k % 2 == 0:
			k /= 2
		else:
			k /= 3
		changes += 1
	if k == 1:
		return changes
	return -1
(m, n) = map(int, input().split())
print(f(m, n))


import math
scan = input().split()
num = int(scan[0])
bignum = int(scan[1])
x = bignum / num
newx = 1
newxx = 1
if num == 1 and bignum == 1:
	print('0')
elif bignum % num == 0:
	limit = math.log(x, 2)
	for i in range(0, int(limit) + 2):
		if x % pow(2, i) == 0:
			step1 = i
		else:
			newx = x / pow(2, i - 1)
			break
	limit = math.log(newx, 3)
	i = 0
	for i in range(0, int(limit) + 2):
		if newx % pow(3, i) == 0:
			step2 = i
		else:
			newxx = newx / pow(3, i - 1)
			break
	if newxx == 1:
		print(step1 + step2)
	else:
		print('-1')
else:
	print('-1')


(n, m) = map(int, input().split())
if n > m or m % n:
	print(-1)
elif n == m:
	print(0)
else:
	tm = m // n
	co = 0
	while tm != 1:
		co += 1
		if tm % 2 == 0:
			tm //= 2
		elif tm % 3 == 0:
			tm //= 3
		else:
			print(-1)
			exit(0)
	print(co)


def main():
	(n, m) = map(int, input().split())
	x = m / n
	if x.is_integer() == False:
		print('-1')
		return
	x = m // n
	c = 0
	while x % 2 == 0:
		x = x // 2
		c += 1
	while x % 3 == 0:
		x = x // 3
		c += 1
	if x != 1:
		c = -1
	print(c)
main()


(a, b) = list(map(int, input().split()))
que = [(a, 0)]
mila = 0
while que:
	(now, move) = que.pop()
	if now == b:
		mila = 1
		print(move)
		break
	elif now > b:
		pass
	else:
		que.append((now * 2, move + 1))
		que.append((now * 3, move + 1))
if mila == 0:
	print(-1)


def rec(a, h):
	global f
	if f:
		return
	if a == n:
		print(h)
		f = 1
		return 0
	if a % 3 == 0:
		rec(a // 3, h + 1)
	if a % 2 == 0:
		rec(a // 2, h + 1)
(n, m) = map(int, input().split())
f = 0
rec(m, 0)
if not f:
	print(-1)


nandm = input().split()
n = int(nandm[0])
m = int(nandm[1])
if m % n == 0:
	x = m // n
	two = 0
	three = 0
	while x % 2 == 0:
		x = x / 2
		two += 1
	while x % 3 == 0:
		x = x / 3
		three += 1
	if x == 1:
		print(two + three)
	else:
		print(-1)
else:
	print(-1)


(n, m) = [int(x) for x in input().split(' ')]
x = m // n
c = 0
if m % n == 0:
	while x and x % 2 == 0:
		x //= 2
		c += 1
	while x and x % 3 == 0:
		x //= 3
		c += 1
	if x == 1:
		print(c)
		exit()
print('-1')


(N, M) = map(int, input().split())
ans = 0
if M % N:
	ans = -1
else:
	K = M // N
	while not K % 2:
		K //= 2
		ans += 1
	while not K % 3:
		K //= 3
		ans += 1
	if K != 1:
		ans = -1
print(ans)


mi = lambda : [int(i) for i in input().split()]
(n, m) = mi()
if m % n != 0:
	print(-1)
	exit(0)
k = m / n
r = 0
while k % 2 == 0:
	r += 1
	k //= 2
while k % 3 == 0:
	r += 1
	k //= 3
if k != 1:
	print(-1)
	exit(0)
print(r)


(n, m) = map(int, input().strip().split())
if m % n != 0:
	print(-1)
	exit()
d = m // n
moves = 0
while d > 1:
	if d % 3 == 0:
		moves += 1
		d /= 3
		continue
	elif d % 2 == 0:
		moves += 1
		d /= 2
		continue
	else:
		print(-1)
		exit()
else:
	print(moves)


def game23(n, m):
	if m % n != 0:
		return -1
	count = 0
	value = m // n
	while value != 1:
		if value % 2 == 0:
			count += 1
			value //= 2
		elif value % 3 == 0:
			count += 1
			value //= 3
		else:
			return -1
	return count
line = input().split(' ')
n = int(line[0])
m = int(line[1])
print(game23(n, m))


(n, m) = map(int, input().split())
x = m / n
if x != int(x) or (n != m and x % 2 != 0 and (x % 3 != 0)):
	print(-1)
else:
	c = 0
	while x % 2 == 0:
		x /= 2
		c += 1
	while x % 3 == 0:
		x /= 3
		c += 1
	if x == 1 or c == 0:
		print(c)
	else:
		print(-1)


(n, m) = [int(x) for x in input().split()]
if m % n == 0:
	if m == n:
		print('0')
	else:
		tmp = m / n
		c2 = 0
		c3 = 0
		while tmp % 2 == 0:
			tmp = tmp / 2
			c2 += 1
		while tmp % 3 == 0:
			tmp = tmp / 3
			c3 += 1
		if tmp != 1:
			print('-1')
		else:
			print(c2 + c3)
else:
	print('-1')


import sys
import math
import bisect
import itertools
import random

def main():
	(n, m) = map(int, input().split())
	if m % n:
		print(-1)
	else:
		p = m // n
		ans = 0
		while p % 2 == 0:
			p //= 2
			ans += 1
		while p % 3 == 0:
			p //= 3
			ans += 1
		if p == 1:
			print(ans)
		else:
			print(-1)
main()


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

def ca():
	return input().split()

def ii():
	return int(input())
(a, b) = ia()
x = b // a
if a * x < b:
	print(-1)
else:
	c = 0
	flag = 0
	while x > 1:
		if x % 2 == 0:
			c += 1
			x = x // 2
		elif x % 3 == 0:
			c += 1
			x = x // 3
		else:
			flag = 1
			print(-1)
			break
	if flag == 0:
		print(c)


def factorize(n):
	factors = []
	i = 2
	while i * i <= n:
		while n % i == 0:
			n //= i
			factors.append(i)
		i += 1
	if n > 1:
		factors.append(n)
	return factors
(n, m) = map(int, input().split())
result = 0
n_list = factorize(n)
m_list = factorize(m)
temp1 = m_list.count(2) - n_list.count(2)
temp2 = m_list.count(3) - n_list.count(3)
result += temp1 + temp2
n_list = [x for x in n_list if x != 2 and x != 3]
m_list = [x for x in m_list if x != 2 and x != 3]
if temp1 < 0 or temp2 < 0 or n_list != m_list:
	print(-1)
else:
	print(result)


(n, m) = map(int, input().split())
ans = 0
if m % n == 0:
	x = m // n
	flag = True
	while x > 1 and flag:
		flag = False
		if x % 6 == 0:
			x //= 6
			ans += 2
			flag = True
		elif x % 3 == 0:
			x //= 3
			ans += 1
			flag = True
		elif x % 2 == 0:
			x //= 2
			ans += 1
			flag = True
	if flag == False:
		print(-1)
	else:
		print(ans)
else:
	print(-1)


(n, m) = map(int, input().split(' '))
result = 0
if m % n == 0:
	m = m // n
	if m == 0:
		result = 0
	else:
		while m != 1:
			if m % 2 == 0:
				m = m // 2
				result += 1
			elif m % 3 == 0:
				m = m // 3
				result += 1
			else:
				result = -1
				break
else:
	result = -1
print(result)


import sys
import math
input = sys.stdin.readline
(n, m) = input().split()
n = int(n.strip())
m = int(m.strip())
if m < n or m > 5 * 10 ** 8:
	print(-1)
elif n == 0 or m == 0 or m % n != 0:
	print(-1)
else:
	steps3 = 0
	totalMultiplier = m // n
	while totalMultiplier % 3 == 0:
		steps3 += 1
		totalMultiplier //= 3
	steps2 = math.log(totalMultiplier, 2)
	if int(steps2) == steps2:
		print(steps3 + int(steps2))
	else:
		print(-1)


import sys
(n, m) = map(int, input().split())
if m / n != m // n:
	print(-1)
	sys.exit()
x = m // n
ANS = 0
while x % 2 == 0:
	x = x // 2
	ANS += 1
while x % 3 == 0:
	x = x // 3
	ANS += 1
if x == 1:
	print(ANS)
else:
	print(-1)


l = input().split(' ')
(n, m) = (int(l[0]), int(l[1]))
if m % n != 0:
	print(-1)
elif n == m:
	print(0)
else:
	nb = m // n
	s = 0
	while True:
		if nb % 3 == 0:
			nb //= 3
			s += 1
		elif nb % 2 == 0:
			nb //= 2
			s += 1
		elif nb == 1:
			print(s)
			break
		else:
			print(-1)
			break


import math

def primefact(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
(n, m) = map(int, input().split())
if m % n != 0:
	print('-1')
elif m == n:
	print('0')
else:
	s = m // n
	a = primefact(s)
	flag = 0
	count = 0
	for i in a:
		if i == 2 or i == 3:
			count += 1
		else:
			flag = 1
	if flag == 1:
		print('-1')
	else:
		print(count)


def game(n, m):
	if m == n:
		return 0
	if m % n != 0:
		return -1
	a = m / n
	return moves(a)

def moves(n, count=1):
	if n % 2 == 0:
		if n != 2:
			n = n / 2
			count += 1
			return moves(n, count)
		else:
			return count
	elif n % 3 == 0:
		if n != 3:
			n = n / 3
			count += 1
			return moves(n, count)
		else:
			return count
	else:
		return -1
a = input()
b = a.split()
n = int(b[0])
m = int(b[1])
c = game(n, m)
print(c)


from sys import stdin, stdout
from collections import defaultdict, Counter, deque
from bisect import bisect, bisect_left
import math
from itertools import permutations
import queue
I = stdin.readline
(n, m) = map(int, I().split())
ans = -1
if m % n == 0:
	num = m // n
	count = 0
	while num % 2 == 0:
		num //= 2
		count += 1
	while num % 3 == 0:
		num //= 3
		count += 1
	if num == 1:
		ans = count
print(ans)


import sys
from math import *

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

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def facts(x):
	r = []
	for i in (2, 3):
		c = 0
		while x % i == 0:
			c += 1
			x //= i
		r.append(c)
	r.append(x)
	return r
(n, m) = mints()
a = facts(n)
b = facts(m)
if a[0] > b[0] or a[1] > b[1] or a[2] != b[2]:
	print(-1)
else:
	print(b[0] - a[0] + b[1] - a[1])


(n, m) = input().split()
n = int(n)
m = int(m)
d = m / n
v = 0
while d != 1:
	if d % 6 == 0:
		d = d / 6
		v += 2
	elif d % 2 == 0:
		d = d / 2
		v += 1
	elif d % 3 == 0:
		d = d / 3
		v += 1
	else:
		break
if d == 1 and v == 0:
	print(0)
elif d == 1 and v > 0:
	print(v)
else:
	print(-1)


from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, sin, cos, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000000)
INF = float('inf')
(YES, Yes, yes, NO, No, no) = ('YES', 'Yes', 'yes', 'NO', 'No', 'no')
(dy4, dx4) = ([0, 1, 0, -1], [1, 0, -1, 0])
(dy8, dx8) = ([0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1])

def inside(y, x, H, W):
	return 0 <= y < H and 0 <= x < W

def ceil(a, b):
	return (a + b - 1) // b

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

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

def euclidean_distance(y1, x1, y2, x2):
	return sqrt((y1 - y2) ** 2 + (x1 - x2) ** 2)

def manhattan_distance(y1, x1, y2, x2):
	return abs(y1 - y2) + abs(x1 - x2)

def prime_factor_decomposition(n):
	import math
	m = defaultdict(int)
	while n > 1:
		find_factor = False
		for i in range(2, int(math.sqrt(n)) + 1):
			if n % i == 0:
				m[i] += 1
				n //= i
				find_factor = True
				break
		if not find_factor:
			m[n] += 1
			break
	return m

def main():
	(N, M) = map(int, input().split())
	p1 = prime_factor_decomposition(N)
	p2 = prime_factor_decomposition(M)
	ans = 0
	for k in set(list(p1.keys()) + list(p2.keys())):
		if k == 2 or k == 3:
			if p1[k] > p2[k]:
				print(-1)
				return
			ans += p2[k] - p1[k]
		elif p1[k] != p2[k]:
			print(-1)
			return
	print(ans)
main()


(n, m) = [int(item) for item in input().split()]
ans = 0
if m % n != 0:
	print(-1)
else:
	k = m // n
	while k != 1:
		if k % 2 == 0:
			k //= 2
		elif k % 3 == 0:
			k //= 3
		else:
			break
		ans += 1
	if k == 1:
		print(ans)
	else:
		print(-1)


def checkfactor(n):
	for i in [5, 7, 10]:
		if n % i == 0:
			return False
	return True
(a, b) = map(int, input().split())
if b % a != 0:
	print(-1)
elif a == b:
	print(0)
else:
	flag = True
	factor = int(b / a)
	if factor % 2 != 0 and factor % 3 != 0:
		print(-1)
	else:
		moves = 0
		if checkfactor(factor):
			while factor > 1:
				if factor % 3 == 0:
					factor /= 3
					moves += 1
				elif factor % 2 == 0:
					factor /= 2
					moves += 1
				else:
					flag = False
					break
			if flag:
				print(moves)
			else:
				print(-1)
		else:
			print(-1)


def game23(n: int, m: int):
	memo = dict()
	return _game23(n, m, memo, step=0)

def _game23(n, m, memo, step):
	if n == m:
		return step
	if n > m:
		return -1
	if n in memo:
		return memo[n]
	result = max(_game23(n * 2, m, memo, step=step + 1), _game23(n * 3, m, memo, step=step + 1))
	memo[n] = result
	return result

def main():
	(n, m) = map(int, input().split())
	print(game23(n=n, m=m))
main()


(n, m) = [int(i) for i in input().split(' ')]
if m % n == 0:
	t = m // n
	ans = 0
	f = t
	while f % 2 == 0:
		f = f // 2
		ans += 1
	g = f
	while g % 3 == 0:
		g = g // 3
		ans += 1
	if g == 1:
		print(ans)
	else:
		print(-1)
else:
	print(-1)


(n, k) = map(int, input().rstrip().split(' '))
if n == k:
	print(0)
elif k % n:
	print(-1)
else:
	f = k // n
	total = 0
	i = 3
	count = 0
	if not f % 2 or not f % 3:
		while f % 2 == 0:
			f = f / 2
			count = count + 1
		while f % 3 == 0:
			f = f / 3
			count = count + 1
		if f > 1:
			print(-1)
		else:
			print(count)
	else:
		print(-1)


TN = 1

def solution():
	from math import log
	(n, m) = map(int, input().split())
	ans = 0
	if m % n == 0:
		cur = m // n
		while cur % 2 == 0:
			ans += 1
			cur //= 2
		while cur % 3 == 0:
			ans += 1
			cur //= 3
		if cur != 1:
			ans = -1
	else:
		ans -= 1
	print(ans)
while TN != 0:
	solution()
	TN -= 1


(n, m) = [int(x) for x in input().split()]
if m % n != 0:
	print(-1)
else:
	temp = m / n
	count = 0
	while temp % 2 == 0:
		temp = temp / 2
		count = count + 1
	while temp % 3 == 0:
		temp = temp / 3
		count = count + 1
	if temp == 1:
		print(count)
	else:
		print(-1)


from math import sqrt

def factor(n):
	potentional_p = 3
	itog_list = []
	while n % 2 == 0:
		n = n // 2
		itog_list.append(2)
	while n - 1:
		if potentional_p > sqrt(n):
			itog_list.append(n)
			return itog_list
		while n % potentional_p == 0:
			n = n // potentional_p
			itog_list.append(potentional_p)
		potentional_p += 2
	return itog_list
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit(0)
z = m // n
F = factor(z)
if F == []:
	print(0)
	exit(0)
if len(set(F)) == 2 and 2 in F and (3 in F) or (len(set(F)) == 1 and (2 in F or 3 in F)):
	print(len(F))
else:
	print(-1)


(n, m) = map(int, input().strip().split())
if n == m:
	print(0)
else:
	tost = m / n
	if tost % 1 != 0:
		print(-1)
	else:
		n2 = 0
		n3 = 0
		while tost % 2 == 0:
			n2 += 1
			tost /= 2
		while tost % 3 == 0:
			n3 += 1
			tost /= 3
		if tost == 1:
			print(n2 + n3)
		else:
			print(-1)


from math import log, ceil, modf
a = list(map(int, input().split(' ')))
p = ceil(log(a[1] / (2 * a[0]), 2) + 1)
p = int(p)
c = 0
for x in range(0, p + 1):
	k = log(a[1] / (a[0] * 2 ** x), 3) + x
	if modf(k)[0] == 0:
		print(int(k))
		c = 1
		break
if c == 0:
	print(-1)


(n, m) = list(map(int, input().split()))
n2 = 0
n3 = 0
m2 = 0
m3 = 0
while n % 2 == 0:
	n //= 2
	n2 += 1
while n % 3 == 0:
	n //= 3
	n3 += 1
while m % 2 == 0:
	m //= 2
	m2 += 1
while m % 3 == 0:
	m //= 3
	m3 += 1
if m != n or n2 > m2 or n3 > m3:
	print(-1)
else:
	print(m2 - n2 + m3 - n3)


import math
(n, m) = list(map(int, input().lstrip().split(' ')))
n2 = n3 = m2 = m3 = 0

def check_index(x, f):
	index = 0
	while x % f == 0:
		index += 1
		x = x / f
	return index
(n2, n3, m2, m3) = [check_index(x, f) for x in (n, m) for f in (2, 3)]
if m2 >= n2 and m3 >= n3 and (int(math.pow(2, m2 - n2) * math.pow(3, m3 - n3) * n) == m):
	print(m2 - n2 + m3 - n3)
else:
	print(-1)


(n, m) = map(int, input().split())

def log(n, x):
	out = 0
	while n >= x and n % x == 0:
		n //= x
		out += 1
	return (out, n)

def parse(n):
	(out2, n) = log(n, 2)
	(out3, n) = log(n, 3)
	return (out2, out3, n)
(n2, n3, rmn) = parse(n)
(m2, m3, rmm) = parse(m)
if rmn == rmm and n2 <= m2 and (n3 <= m3):
	print(m2 - n2 + (m3 - n3))
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	exit(print(-1))
elif m // n == 1:
	print(0)
else:
	p = 0
	m = m // n
	while m != 1:
		if m % 2 == 0:
			p += 1
			m = m // 2
		elif m % 3 == 0:
			p += 1
			m = m // 3
		else:
			exit(print(-1))
	print(p)


l = list(map(int, input().split()))
a = l[0]
b = l[1]
ans = 0
if a == b:
	print(0)
elif b % a != 0:
	print(-1)
else:
	c = b / a
	possible = True
	while c != 1:
		if c % 2 == 0:
			c = c // 2
			ans += 1
		elif c % 3 == 0:
			c = c // 3
			ans += 1
		else:
			possible = False
			break
	if possible:
		print(ans)
	else:
		print(-1)


cnt = 0
(a, b) = map(int, input().split())
d = b // a
if b % a:
	print(-1)
	exit()
f = 1
c = 0
for i in range(2, int(d ** 0.5) + 1):
	if d % i == 0:
		if i != 2 and i != 3:
			f = 0
		while d % i == 0:
			d //= i
			c += 1
if d > 1:
	if d != 2 and d != 3:
		f = 0
	c += 1
print(c if f else -1)


(a, b) = input().split()
if int(b) % int(a) != 0:
	print(-1)
else:
	c = int(b) // int(a)
	li = [2, 3]
	ans = 0
	while c != 1:
		if c % 2 == 0:
			c = c // 2
			ans = ans + 1
		elif c % 3 == 0:
			c = c // 3
			ans = ans + 1
		else:
			ans = -1
			c = 1
	print(ans)


(a, b) = map(int, input().split())
if a == b:
	print(0)
elif b % a != 0:
	print(-1)
else:
	r = 0
	p = 0
	a = b // a
	while a > 1:
		if a % 3 == 0:
			a = a // 3
			r += 1
		elif a % 2 == 0:
			a = a // 2
			r += 1
		else:
			p = 1
			break
	if p == 0:
		print(r)
	else:
		print(-1)


[n, m] = [int(i) for i in input().split()]
v = m // n
if m == n:
	print(0)
elif m % n != 0:
	print(-1)
else:
	count = 0
	while v % 2 == 0:
		v = v // 2
		count += 1
	while v % 3 == 0:
		v = v // 3
		count += 1
	if count == 0 or v != 1:
		print(-1)
	else:
		print(count)


import math
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	a = m // n
	s = 0
	while a % 6 == 0:
		s += 2
		a = a // 6
	t1 = math.log2(a)
	t2 = int(math.log2(a))
	t3 = 0
	while a % 3 == 0:
		t3 += 1
		a = a // 3
	if t1 == t2:
		print(s + t2)
	elif a == 1:
		print(s + t3)
	else:
		print(-1)


(n, m) = list(map(int, input().split(' ')))
if m % n != 0:
	print('-1')
	exit()
(t, count) = (m // n, 0)
while t % 3 == 0:
	t //= 3
	count += 1
while t % 2 == 0:
	t //= 2
	count += 1
print(count if t == 1 else -1)


(n, k) = map(int, input().split())
if n == k:
	print(0)
elif k % n > 0:
	print(-1)
else:
	t = k // n
	ans = 0
	a = 0
	while t > 1:
		if t % 3 == 0:
			t = t // 3
		elif t % 2 == 0:
			t = t // 2
		else:
			a = 1
			break
		ans += 1
	if a == 1:
		ans = -1
	print(ans)


(n, m) = map(int, input().split())
if m % n == 0:
	m = m // n
	cont = 0
	for k in (2, 3):
		while m % k == 0:
			m //= k
			cont += 1
	print([cont, -1][m > 1])
else:
	print(-1)


def func(a, b):
	if b % a != 0:
		return -1
	else:
		store = b // a
		count = 0
		while store % 2 == 0:
			store //= 2
			count += 1
		while store % 3 == 0:
			store //= 3
			count += 1
		if store == 1:
			return count
		else:
			return -1

def main():
	arr = input().split()
	print(func(int(arr[0]), int(arr[1])))
main()


(n, m) = map(int, input().split())
num1 = n
a = [0] * 10
i = 2
while num1 > 1:
	if num1 % i == 0:
		a[i] += 1
		num1 //= i
	else:
		i += 1
	if i > 9:
		break
b = [0] * 10
num2 = m
i = 2
while num2 > 1:
	if num2 % i == 0:
		b[i] += 1
		num2 //= i
	else:
		i += 1
	if i > 9:
		break
rez = [0] * 10
for i in range(10):
	rez[i] = b[i] - a[i]
output = 0
for i in range(10):
	if i == 2 or i == 3:
		output += rez[i]
	if i != 2 and i != 3 and (rez[i] != 0):
		output = -1
		break
	if num1 != num2:
		output = -1
		break
	if rez[i] < 0:
		output = -1
		break
print(output)


(n, m) = map(int, input().split())
aa = -1
two = 0
while n * pow(2, two) < m:
	thr = 0
	while n * pow(3, thr) * pow(2, two - thr) < m and thr < two:
		thr += 1
	if n * pow(3, thr) * pow(2, two - thr) == m:
		aa = two
		break
	two += 1
if n * pow(2, two) == m:
	aa = two
print(aa)


def solve():
	(n, m) = [int(x) for x in input().split(' ')]
	if m == n:
		print(0)
		return
	if m % n != 0:
		print(-1)
		return
	p = m // n
	s = 0
	while p % 3 == 0:
		p //= 3
		s += 1
	while p % 2 == 0:
		p //= 2
		s += 1
	if p != 1:
		print(-1)
	else:
		print(s)
solve()


def sint():
	return int(input())

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

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

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

def main():
	(a, b) = sints()
	cnt = 0
	if b % a == 0:
		b //= a
		while b % 2 == 0:
			b /= 2
			cnt += 1
		while b % 3 == 0:
			b /= 3
			cnt += 1
	if b > 1:
		print(-1)
	else:
		print(cnt)
main()


(n, m) = input().split()
n = int(n)
m = int(m)
temp = m / n
count = 0
while n < m:
	temp = m / n
	if temp % 2 == 0:
		count += 1
		n = n * 2
	elif temp % 3 == 0:
		count += 1
		n = n * 3
	else:
		count = -1
		break
print(count)


(n, m) = map(int, input().split())
(c, z) = (0, m // n)
if m % n == 0:
	while z % 2 == 0 or z % 3 == 0:
		if z % 2 == 0:
			c += 1
			z = z // 2
		if z % 3 == 0:
			c += 1
			z = z // 3
	if z == 1:
		print(c)
	else:
		print('-1')
else:
	print('-1')


(a, b) = map(int, input().split())
b1 = b
b //= a
if b * a != b1:
	print(-1)
else:
	calc = 0
	while b % 2 == 0:
		b //= 2
		calc += 1
	while b % 3 == 0:
		b //= 3
		calc += 1
	if b == 1:
		print(calc)
	else:
		print(-1)


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		a = self.next_int()
		b = self.next_int()
		c = 0
		while b % (2 * a) == 0:
			b //= 2
			c += 1
		while b % (3 * a) == 0:
			b //= 3
			c += 1
		print(c if a == b else -1)
Main().solve()


(n1, n2) = map(int, input().split())
moves = 0
if n1 == n2:
	print('0')
elif n2 % n1 != 0:
	print('-1')
else:
	res = n2 // n1
	while res % 3 == 0 or res % 2 == 0:
		if res % 3 == 0:
			res = res // 3
		else:
			res = res // 2
		moves += 1
	if moves == 0 or res > 1:
		print('-1')
	else:
		print(moves)


(n, m) = [int(i) for i in input().split()]
c = 0
while n < m:
	if m // n % 2 == 0:
		n *= 2
		c += 1
	if n < m and m // n % 3 == 0:
		n *= 3
		c += 1
	elif m // n % 2 != 0 and m // n % 3 != 0:
		break
if n == m:
	print(c)
else:
	print(-1)


import math
s = input()
(n, m) = s.split(' ')
n = int(n)
m = int(m)
c = 0

def f(n, m):
	if n == m:
		return 0
	elif n > m:
		return math.inf
	elif n < m:
		return 1 + min(f(2 * n, m), f(3 * n, m))
if f(n, m) == math.inf:
	print(-1)
else:
	print(f(n, m))


a = list(map(int, input().split()))
c = 0
if a[1] % a[0] != 0:
	c = -1
else:
	x = a[1] // a[0]
	while x != 1:
		if x % 6 == 0:
			x = x // 6
			c = c + 2
		elif x % 2 == 0:
			x = x // 2
			c = c + 1
		elif x % 3 == 0:
			x = x // 3
			c = c + 1
		else:
			c = -1
			break
print(c)


count = 0
(n, m) = map(int, input().split())
toAc = m / n
if int(toAc) != toAc:
	print(-1)
elif toAc == 1:
	print(0)
else:
	while toAc % 3 == 0:
		toAc = toAc // 3
		count += 1
	while toAc % 2 == 0:
		toAc = toAc // 2
		count += 1
	if toAc != 1:
		print(-1)
	else:
		print(count)


s = input()
s = s.split()
n = int(s[0])
m = int(s[1])
if m % n:
	print(-1)
else:
	count = 0
	k = m / n
	while k % 2 == 0:
		count += 1
		k = k / 2
	while k % 3 == 0:
		count += 1
		k = k / 3
	if k == 1:
		print(count)
	else:
		print(-1)


import math
a = list(map(int, input().split()))
n = a[0]
m = a[1]
if m % n != 0:
	print('-1')
	quit()
n = m / n
sum = 0
while n % 2 == 0:
	n = n / 2
	sum += 1
while n % 3 == 0:
	n = n / 3
	sum += 1
if n != 1:
	print('-1')
	quit()
print(sum)


(n, m) = map(int, input().split())
(x, r) = (0, m / n)
for i in [2, 3]:
	while r % i == 0:
		r /= i
		x += 1
print(x if r == 1 else -1)


from math import gcd

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

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

def num_of_twos(n):
	cnt = 0
	while n % 2 == 0:
		cnt += 1
		n //= 2
	return cnt

def num_of_threes(n):
	cnt = 0
	while n % 3 == 0:
		cnt += 1
		n //= 3
	return cnt
(n, m) = rl()
if n == m:
	print(0)
elif m % n != 0:
	print(-1)
elif m % 2 == 0 or m % 3 == 0 or m % 6 == 0:
	k = n * pow(3, num_of_threes(m) - num_of_threes(n)) * pow(2, num_of_twos(m) - num_of_twos(n))
	if k == m:
		print(num_of_threes(m) - num_of_threes(n) + num_of_twos(m) - num_of_twos(n))
	else:
		print(-1)
else:
	print(-1)


from math import floor
(n, m) = [int(x) for x in input().split()]
q = m / n
count = 0
while q % 2 == 0:
	count = count + 1
	q = q / 2
while q % 3 == 0:
	count = count + 1
	q = q / 3
if q == 1:
	print(count)
else:
	print(-1)


(n, m) = (int(i) for i in input().split())
count = 0
y = m / n
while True:
	if y % 2 != 0:
		break
	else:
		y = y / 2
		count += 1
while True:
	if y % 3 != 0:
		break
	else:
		y = y / 3
		count += 1
if m % n != 0 or y != 1:
	print(-1)
else:
	print(count)


from collections import defaultdict as dd, deque as dq
import math, string
import sys

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()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(A, B) = getInts()
	ans = 0
	if A > B:
		(B, A) = (A, B)
	if B % A > 0:
		return -1
	X = B // A
	while X % 2 == 0:
		X //= 2
		ans += 1
	while X % 3 == 0:
		X //= 3
		ans += 1
	if X > 1:
		return -1
	return ans
print(solve())


def result(num, count):
	if num == 1:
		return count
	elif num > 1:
		if num % 2 == 0:
			num = num / 2
			return result(num, count + 1)
		elif num % 3 == 0:
			num = num / 3
			return result(num, count + 1)
		else:
			return -1
(n, m) = map(int, input().split())
check = m / n
print(result(check, 0))


a = input()
n = int(a.split()[0])
m = int(a.split()[1])
answer = 0
if m % n == 0:
	m = m / n
	while m != 1:
		if m % 2 == 0:
			m = m / 2
			answer += 1
		elif m % 3 == 0:
			m = m / 3
			answer += 1
		else:
			answer = -1
			break
else:
	answer = -1
print(answer)


def transform(n, m):
	if m % n != 0:
		return -1
	elif m // n == 0:
		return 0
	else:
		t = 0
		p = m // n
		while p % 2 == 0 or p % 3 == 0:
			if p % 2 == 0:
				p = p // 2
			else:
				p = p // 3
			t += 1
		if p == 1:
			return t
		else:
			return -1
(n, m) = list(map(int, input().split()))
print(transform(n, m))


def f(x):
	global count
	if x == 1:
		return True
	elif x % 2 == 0:
		count += 1
		return f(x // 2)
	elif x % 3 == 0:
		count += 1
		return f(x // 3)
	else:
		return False
(n, m) = map(int, input().split())
x = m / n
if int(x) != x:
	print(-1)
else:
	count = 0
	if f(x):
		print(count)
	else:
		print(-1)


(a, b) = map(int, input().split())
c = 0
if b % a:
	c = -1
else:
	b //= a
	x = 2
	while b > 1:
		if not b % x:
			b //= x
			c += 1
		else:
			x += 1
			if x == 4:
				c = -1
				break
print(c)


a = list(map(int, input().split()))
(flag1, flag2, co) = (0, 0, 0)
do = 1
if a[1] % a[0]:
	print(-1)
	do = 0
else:
	d = int(a[1] / a[0])
	em = d
	while flag1 * flag2 != 1:
		if d % 2 == 0:
			d = int(d / 2)
			co += 1
		elif d == 0:
			flag1 = 1
		else:
			flag1 = 1
		if d % 3 == 0:
			d = int(d / 3)
			co += 1
		elif d == 0:
			flag2 = 1
		else:
			flag2 = 1
if do != 0:
	if d != 1:
		print(-1)
	else:
		print(co)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit(0)
if m == n:
	print(0)
	exit(0)
dn = m / n
count = 0
while dn != 1:
	if dn % 2 == 0:
		dn /= 2
		count += 1
	elif dn % 3 == 0:
		dn /= 3
		count += 1
	else:
		print(-1)
		exit(0)
print(count)


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
import math
import random
MAX = sys.maxsize
MAXN = 10 ** 5 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN():
	return sys.stdin.readline().strip().split()

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def maxsum(a):
	l = []
	msf = 0
	meh = 0
	for i in range(len(a)):
		meh += a[i][1]
		if meh < 0:
			meh = 0
		elif msf < meh:
			msf = meh
			l.append(a[i])
	return (l[::-1], msf)

def fact(n):
	l = []
	x = []
	if n % 2 == 0:
		l.append(2)
	while n % 2 == 0:
		x.append(2)
		n = n // 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			l.append(i)
		while n % i == 0:
			x.append(i)
			n = n // i
	if n > 2:
		l.append(n)
		x.append(n)
	return (l, x)
(a, b) = numIN()
if b % a != 0:
	print(-1)
elif b == a:
	print(0)
else:
	(l, x) = fact(b // a)
	if len(l) > 2:
		print(-1)
	elif len(l) == 1:
		if l[0] == 2 or l[0] == 3:
			print(len(x))
		else:
			print(-1)
	elif sorted(l) == [2, 3]:
		print(len(x))
	else:
		print(-1)


import math

def primeFactors(n):
	la = []
	while n % 2 == 0:
		la.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			la.append(i)
			n = n // i
	if n > 2:
		la.append(n)
	return la
(n, m) = map(int, input().split())
if m % n == 0:
	z = m // n
	if z == 1:
		print(0)
	else:
		k = primeFactors(z)
		if len(set(k)) == 2:
			if 2 in set(k) and 3 in set(k):
				print(len(k))
			else:
				print(-1)
		elif len(set(k)) == 1:
			if set(k) == {2} or set(k) == {3}:
				print(len(k))
			else:
				print(-1)
		else:
			print(-1)
else:
	print(-1)


(n, m) = [int(x) for x in input().split()]
if m == n:
	print(0)
elif m % n == 0:
	aux = m // n
	cont = 0
	if aux % 2 == 0 or aux % 3 == 0:
		while aux % 2 == 0:
			cont += 1
			aux = aux // 2
		while aux % 3 == 0:
			cont += 1
			aux = aux // 3
		if aux != 1:
			print(-1)
		else:
			print(cont)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
if n >= m:
	print(int(m / n) - 1)
else:
	t = m / n
	if t % 1 != 0 and t < 1:
		print('-1')
	else:
		x = 0
		y = 0
		while t > 1:
			if t % 3 == 0:
				t /= 3
				y += 1
			elif t % 2 == 0:
				t /= 2
				x += 1
			else:
				print('-1')
				break
		else:
			print(x + y)


(n, m) = map(int, input().split())
if m % n != 0:
	print('-1')
else:
	d = m // n
	flag = 1
	ans = 0
	while d:
		if d == 1:
			break
		if d % 2 == 0:
			d /= 2
			ans += 1
		elif d % 3 == 0:
			d /= 3
			ans += 1
		else:
			flag = 0
			break
	if flag == 1:
		print(ans)
	else:
		print('-1')


def divide(divisable, divisor, i):
	if divisable % divisor == 0:
		return divide(divisable // divisor, divisor, i + 1)
	return (divisable, i)
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	counter = 0
	(m, counter) = divide(m // n, 3, counter)
	(m, counter) = divide(m, 2, counter)
	if m == 1:
		print(counter)
	else:
		print(-1)


def fun(a, b):
	if a % b != 0:
		return -1
	temp = 0
	m = a / b
	while m % 2 == 0:
		m = m / 2
		temp += 1
	while m % 3 == 0:
		m = m / 3
		temp += 1
	if m == 1:
		return temp
	else:
		return -1
(a, b) = map(int, input().split())
print(fun(b, a))


import sys, collections
(n, m) = map(int, sys.stdin.readline().split())
q = collections.deque()
q.append((n, 0))
check = dict()
while q:
	(now, t) = q.popleft()
	if now > m:
		continue
	elif now == m:
		print(t)
		sys.exit(0)
	else:
		if 2 * now not in check:
			check[2 * now] = True
			q.append((2 * now, t + 1))
		if 3 * now not in check:
			check[3 * now] = True
			q.append((3 * now, t + 1))
print(-1)


R = lambda : map(int, input().split())
(a, b) = R()
if b % a != 0:
	print(-1)
else:
	(res, b) = (0, b // a)
	while b % 2 == 0:
		b //= 2
		res += 1
	while b % 3 == 0:
		b //= 3
		res += 1
	print(res if b == 1 else -1)


def solve(a):
	d = 0
	t = 0
	while a != 1:
		if a % 2 == 0:
			a /= 2
			d += 1
		elif a % 3 == 0:
			a /= 3
			t += 1
		else:
			a = 1
	return (d, t)
(a, b) = list(map(int, input().split()))
x = solve(a)
y = solve(b)
z = y[1] - x[1]
w = y[0] - x[0]
res = z + w
while z > 0:
	a *= 3
	z -= 1
while w > 0:
	a *= 2
	w -= 1
if a == b:
	print(res)
else:
	print(-1)


def maximal(x, y):
	ans = 0
	while x % y == 0:
		x /= y
		ans += 1
	return ans

def main():
	(m, n) = map(int, input().split())
	(m, n) = (n, m)
	if m % n != 0:
		print(-1)
	elif m == n:
		print(0)
	else:
		l = m // n
		s1 = maximal(l, 3)
		s2 = maximal(l, 2)
		if l / (3 ** s1 * 2 ** s2) == 1:
			print(s1 + s2)
		else:
			print(-1)
main()


(n, m) = map(int, input().split(' '))
x = n
y = m
if n > m:
	print(-1)
if n == m:
	print(0)
if n < m:
	n_3 = 0
	n_2 = 0
	while n % 3 == 0:
		n_3 = n_3 + 1
		n = n / 3
	while n % 2 == 0:
		n_2 = n_2 + 1
		n = n / 2
	m_3 = 0
	m_2 = 0
	while m % 3 == 0:
		m_3 = m_3 + 1
		m = m / 3
	while m % 2 == 0:
		m_2 = m_2 + 1
		m = m / 2
	diff_2 = m_2 - n_2
	diff_3 = m_3 - n_3
	if int(x * 2 ** diff_2 * 3 ** diff_3) == y and m_3 - n_3 + m_2 - n_2 > 0 and (y % x == 0):
		print(m_3 - n_3 + m_2 - n_2)
	else:
		print(-1)


(n, m) = map(int, input().split())
if n == m:
	print(0)
	exit()
elif m % n != 0:
	print(-1)
	exit()
div = m // n
total = 0
while div % 2 == 0:
	total += 1
	div //= 2
while div % 3 == 0:
	total += 1
	div //= 3
if div == 1:
	print(total)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	m1 = m // n
	d2 = 0
	d3 = 0
	while m1 % 3 == 0:
		m1 //= 3
		d3 += 1
	while m1 % 2 == 0:
		m1 //= 2
		d2 += 1
	if m1 > 1:
		print(-1)
	else:
		print(d2 + d3)


(n, m) = map(int, input().strip().split())
can = True
if m % n != 0:
	can = False
k = m / n
ans = 0
while k % 2 == 0:
	k = k / 2
	ans += 1
while k % 3 == 0:
	k = k / 3
	ans += 1
if k != 1:
	can = False
if can:
	print(ans)
else:
	print(-1)


def power(x, y):
	res = 1
	while y > 0:
		if y & 1:
			res = res * x
		y = y >> 1
		x = x * x
	return res
(n, m) = map(int, input().split())
a = 0
b = 0
yy = m
while yy % 2 == 0:
	yy = yy // 2
	a += 1
while yy % 3 == 0:
	yy = yy // 3
	b += 1
x = 0
y = 0
xx = n
while xx % 2 == 0:
	xx = xx // 2
	x += 1
while xx % 3 == 0:
	xx = xx // 3
	y += 1
ans = a + b - (x + y)
aa = a - x
bb = b - y
temp = m
temp = temp // power(2, aa)
temp //= power(3, bb)
if temp != n:
	print(-1)
else:
	print(ans)


A = list(map(int, input().split()))
n = A[0]
m = A[1]
counter = 0
while m > n and (m // n % 3 == 0 or m // n % 2 == 0):
	if m // n % 3 == 0:
		counter += 1
		n *= 3
	elif m // n % 2 == 0:
		counter += 1
		n *= 2
if m == n:
	print(counter)
else:
	print(-1)


(n, m) = map(int, input().strip().split())
if n == m:
	print(0)
elif m % n != 0:
	print(-1)
else:
	count = 0
	while m % n == 0 and n != m:
		n = n * 2
		count += 1
	if m % n != 0:
		count -= 1
		n = n // 2
		while m % n == 0 and n != m:
			n = n * 3
			count += 1
	if n != m:
		print(-1)
	else:
		print(count)


lista = input().split()
lista1 = [int(m) for m in lista]
if lista1[1] % lista1[0] == 0:
	d = lista1[1] / lista1[0]
	i = 0
	while d % 2 == 0:
		d = d / 2
		i = i + 1
	while d % 3 == 0:
		d = d / 3
		i = i + 1
	if d == 1:
		print(i)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit(0)
if m == n:
	print(0)
	exit(0)
x = 0
m //= n
while m % 2 == 0:
	m //= 2
	x += 1
while m % 3 == 0:
	m //= 3
	x += 1
if m != 1:
	print(-1)
else:
	print(x)


def main():
	input_str = input()
	(n, m) = input_str.split()
	(n, m) = (int(n), int(m))
	if m % n:
		print(-1)
		return -1
	m = m // n
	count = 0
	while m > 1:
		if not m % 3:
			m = m / 3
		elif not m % 2:
			m = m / 2
		else:
			print(-1)
			return -1
		count += 1
	print(count)
	return count
main()


import math
(n, m) = map(int, input().split())
(i, flag) = (0, True)
try:
	while True:
		if m % (n * math.pow(3, i)) == 0:
			k = int(m / (n * math.pow(3, i)))
			count = 0
			while k % 2 == 0:
				k /= 2
				count += 1
			if k == 1:
				print(count + i)
				flag = False
				exit()
		i += 1
except:
	if flag:
		print('-1')


(n, m) = list(map(int, input().split()))
t = 0
if m % n == 0:
	p = m // n
	for j in range(100000000):
		if p % 2 == 0:
			p = p // 2
			t += 1
		elif p % 3 == 0:
			p = p // 3
			t += 1
		if p == 1:
			print(t)
			break
		elif p == 0:
			print(-1)
			break
		elif p % 3 != 0 and p % 2 != 0 and (p != 1):
			print(-1)
			break
else:
	print(-1)


(a, b) = map(int, input().split())
ans = 0
if b % a:
	print(-1)
	raise SystemExit
d = b // a
while d % 2 == 0:
	d //= 2
	ans += 1
while d % 3 == 0:
	d //= 3
	ans += 1
if d == 1:
	print(ans)
else:
	print(-1)


(n, m) = tuple(map(int, input().split()))
num1 = m / n
a = []
while True:
	if num1 % 2 == 0:
		a.append(2)
		num1 = num1 // 2
	elif num1 % 3 == 0:
		num1 = num1 // 3
		a.append(3)
	else:
		break
mult = 1
for k in range(len(a)):
	mult *= a[k]
if n * mult == m:
	print(len(a))
else:
	print(-1)


arr = list(map(int, input().split()))
n = arr[0]
m = arr[1]
if m % n == 0:
	x = m / n
	ans = 0
	while x % 2 == 0:
		x /= 2
		ans += 1
	while x % 3 == 0:
		x /= 3
		ans += 1
	if x == 1:
		print(ans)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
fact = m / n
numtwo = 0
numthree = 0
while fact % 2 == 0:
	fact = fact / 2
	numtwo += 1
while fact % 3 == 0:
	fact = fact / 3
	numthree += 1
if int(n) == int(m):
	print(0)
elif numthree + numtwo == 0:
	print(-1)
elif int(fact) == 1:
	print(numthree + numtwo)
else:
	print(-1)


(n, k) = [int(i) for i in input().split()]
if k % n != 0:
	print(-1)
else:
	k = k // n
	c = 0
	while k > 1 and (k % 3 == 0 or k % 2 == 0):
		if k % 3 == 0:
			k //= 3
			c += 1
		if k % 2 == 0:
			k //= 2
			c += 1
	if k == 1:
		print(c)
	else:
		print(-1)


(n, m) = [int(z) for z in input().split()]
if n == m:
	print(0)
	exit()
if m / n < 2:
	print(-1)
	exit()
mx2 = 0
mx3 = 0
while m % 2 == 0 or m % 3 == 0:
	if m % 2 == 0:
		mx2 += 1
		m = int(m / 2)
	else:
		mx3 += 1
		m = int(m / 3)
nx2 = 0
nx3 = 0
while n % 2 == 0 or n % 3 == 0:
	if n % 2 == 0:
		nx2 += 1
		n = int(n / 2)
	else:
		nx3 += 1
		n = int(n / 3)
print(mx2 + mx3 - nx2 - nx3) if n == m and mx2 >= nx2 and (mx3 >= nx3) else print(-1)


(a, b) = map(int, input().split())
if a > b or b % a != 0:
	print(a - (a + 1))
elif a == b:
	print(a - b)
else:
	h = 0
	res = b // a
	while res % 2 == 0:
		res //= 2
		h += 1
	while res % 3 == 0:
		res //= 3
		h += 1
	if res == 1:
		print(h)
	else:
		print(res - (res + 1))


(n, m) = map(int, input().split())
if m % n == 0:
	temp = m
	m //= n
	i = j = 1
	count2 = count3 = 0
	while 2 ** i <= m or 3 ** j <= m:
		if m % 2 ** i == 0:
			count2 += 1
		if m % 3 ** j == 0:
			count3 += 1
		i += 1
		j += 1
	if n * 2 ** count2 * 3 ** count3 == temp:
		print(count2 + count3)
	else:
		print(-1)
else:
	print(-1)


def run(n, m):
	if m % n != 0:
		return -1
	elif m == n:
		return 0
	else:
		res = 0
		a = m // n
		while a % 3 == 0 or a % 2 == 0:
			if a % 3 == 0:
				a = a // 3
				res += 1
			if a % 2 == 0:
				a = a // 2
				res += 1
		if a == 1:
			return res
		else:
			return -1
(m, n) = list(map(int, input().split()))
print(run(m, n))


(ilksayi, sonsayi) = [int(x) for x in input().split(' ')]
bolum = sonsayi / ilksayi
sayac2 = 0
while bolum % 2 == 0:
	bolum /= 2
	sayac2 += 1
sayac3 = 0
while bolum % 3 == 0:
	bolum /= 3
	sayac3 += 1
if bolum == 1:
	print(str(sayac2 + sayac3))
else:
	print(-1)


(a, b) = map(int, input().split())
c = b / a
if c.is_integer():
	count = 0
	while c % 2 == 0:
		c /= 2
		count += 1
	while c % 3 == 0:
		c /= 3
		count += 1
	if c == 1:
		print(count)
	else:
		print(-1)
elif c == 1:
	print(0)
else:
	print(-1)


(n, m) = list(map(int, input().split()))

def Game23(n, m):
	count = 0
	mod = m % n
	div = m / n
	if mod != 0:
		return -1
	else:
		while div % 3 == 0:
			div /= 3
			count += 1
		while div % 2 == 0:
			div /= 2
			count += 1
		if div == 1:
			return count
		else:
			return -1
print(Game23(n, m))


import math
(n, m) = map(int, input().split())
if m % n == 0:
	w = m // n
	if w == 1:
		print(0)
	else:
		p_two = int(math.log(w, 2))
		p_three = int(math.log(w, 3))
		f = 0
		for i in range(p_two + 1):
			for j in range(p_three + 1):
				if 2 ** i * 3 ** j == w:
					f = 1
					ww = i + j
					break
		if f == 0:
			print(-1)
		else:
			print(ww)
else:
	print(-1)


import math

def primeFactors(n):
	s = dict()
	while n % 2 == 0:
		if 2 not in s:
			s[2] = 1
		else:
			s[2] += 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			if i not in s:
				s[i] = 1
			else:
				s[i] += 1
			n = n // i
	if n > 2:
		if n not in s:
			s[n] = 1
		else:
			s[n] += 1
	return s

def main():
	(n, m) = map(int, input().split())
	if m % n != 0:
		print(-1)
		return
	s = primeFactors(m // n)
	keys = s.keys()
	if len(keys) > 2:
		print(-1)
		return
	ans = 0
	for (key, value) in s.items():
		if key > 3:
			print(-1)
			return
		else:
			ans += value
	print(ans)
main()


inputs = list(map(int, input().split()))

def p23r(n, m):
	count = 0
	if not m % n == 0 or m < n:
		return -1
	else:
		m = m // n
	while m % 3 == 0 and m // 3 >= 1:
		m = m // 3
		count = count + 1
	if m == 1:
		return count
	else:
		while m % 2 == 0 and m // 2 >= 1:
			m = m // 2
			count = count + 1
		if m == 1:
			return count
		else:
			return -1
print(p23r(inputs[0], inputs[1]))


(n, m) = map(int, input().split())
d = m // n
divs = [0, 0]
for (i, div) in enumerate([2, 3]):
	while d % div == 0:
		d //= div
		divs[i] += 1
if m % n != 0 or d != 1:
	print(-1)
else:
	print(sum(divs))


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
if m % n != 0:
	exit(print(-1))
x = m // n
cnt = 0
while x % 2 == 0:
	x //= 2
	cnt += 1
while x % 3 == 0:
	x //= 3
	cnt += 1
if x == 1:
	print(cnt)
else:
	print(-1)


input_ = input().split()
input_ = list(map(int, input_))
(n, m) = (input_[0], input_[1])
if m % n == 0:
	num = m / n
	res = 0
	while num != 1:
		if num % 3 == 0:
			num /= 3
			res += 1
			continue
		if num % 2 == 0:
			num /= 2
			res += 1
			continue
		break
	if num != 1:
		print(-1)
	else:
		print(res)
else:
	print(-1)


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
(n, m) = map(int, stdin.readline().split())
if m % n != 0:
	print(-1)
else:
	ans = m // n
	if ans == 0:
		print(0)
	else:
		count = 0
		while ans % 2 == 0 and ans > 0:
			count += 1
			ans = ans // 2
		while ans % 3 == 0 and ans > 0:
			count += 1
			ans = ans // 3
		if ans != 1:
			print(-1)
		else:
			print(count)


str = input()
l = str.split(' ')
n = int(l[1])
m = int(l[0])
if n % m != 0:
	print(-1)
else:
	num = n / m
	ctr = 0
	while num % 3 == 0 or num % 2 == 0 or num > 0:
		if num == 1:
			print(ctr)
			break
		if num % 3 == 0:
			num = num / 3
			ctr = ctr + 1
		elif num % 2 == 0:
			num = num / 2
			ctr = ctr + 1
		else:
			print('-1')
			break


(n, k) = map(int, input().split())
step = 0
p = -1
if k % n == 0:
	step = k // n
	p = 0
	while step % 2 == 0:
		step /= 2
		p += 1
	while step % 3 == 0:
		step /= 3
		p += 1
	if step != 1:
		p = -1
print(p)


import math
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	count = 0
	m = int(m / n)
	while True:
		if m % 3 != 0:
			break
		m = int(m / 3)
		count += 1
	p = math.log(m, 2)
	if p == int(p):
		print(int(count + p))
	else:
		print(-1)


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def modu(a, m):
	if a % m:
		return a % m
	return m

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

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

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	c = 0
	ans = m // n
	while ans % 2 == 0:
		ans //= 2
		c += 1
	while ans % 3 == 0:
		ans //= 3
		c += 1
	if ans == 1:
		print(c)
	else:
		print(-1)


import sys
(n, m) = [int(s) for s in input().split()]
counter = 0
diff = m / n
if diff % 1:
	print(-1)
	sys.exit(0)
while diff != 1:
	if not diff % 3:
		diff = diff // 3
		counter += 1
	elif not diff % 2:
		diff = diff // 2
		counter += 1
	else:
		print(-1)
		sys.exit(0)
print(counter)


import math
(x, y) = map(int, input().split())
(v, c) = (y / x, 0)
while v % 3 == 0:
	v /= 3
	c += 1
while v % 2 == 0:
	v /= 2
	c += 1
if v > 1:
	print(-1)
else:
	print(c)


ins = lambda : list(map(int, input().split(' ')))
(n, m) = ins()
for i in range(35):
	for j in range(40):
		if n * 2 ** i * 3 ** j > m:
			break
		if n * 2 ** i * 3 ** j == m:
			print(i + j)
			exit(0)
print(-1)


(n, m) = map(int, input().split())
x = 999
if m % n != 0:
	print('-1')
else:
	x = m // n
	counter = 0
	while x > 1 and x % 2 == 0:
		x /= 2
		counter += 1
	while x > 1 and x % 3 == 0:
		x /= 3
		counter += 1
	if x == 1:
		print(counter)
	else:
		print('-1')


(a, b) = map(int, input().split())
if b % a != 0:
	print(-1)
	exit()
b //= a
ans = 0
while b % 2 == 0:
	b //= 2
	ans += 1
while b % 3 == 0:
	b //= 3
	ans += 1
if b != 1:
	print(-1)
	exit()
print(ans)


def main():
	(n, m) = map(int, input().split())
	if m == n:
		print(0)
		return
	if m % n == 0:
		result = m // n
		ans = 0
		while result % 2 == 0:
			result /= 2
			ans += 1
		while result % 3 == 0:
			result /= 3
			ans += 1
		if result != 1:
			print(-1)
			return
	else:
		ans = -1
	if ans == 0:
		print(-1)
		return
	print(ans)
main()


(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)
(n, m) = zz()
t = m // n
ex = m % n
if ex != 0:
	print(-1)
	exit()
for i in range(0, 51):
	for j in range(0, 51):
		x = 2 ** i * 3 ** j
		if x == t:
			print(i + j)
			exit()
		if x > t:
			break
print(-1)


import sys

def run():
	(n, m) = map(int, sys.stdin.readline().rstrip().split())
	if m % n != 0:
		print(-1)
		return
	m = int(m / n)
	moves = 0
	while m % 2 == 0 or m % 3 == 0:
		if m % 2 == 0:
			m = int(m / 2)
			moves += 1
		if m % 3 == 0:
			m = int(m / 3)
			moves += 1
	if m != 1:
		print(-1)
		return
	print(moves)
run()


def fact(a):
	t = int(a)
	ft = {2: 0, 3: 0, 'x': -1}
	while t % 2 == 0:
		ft[2] += 1
		t //= 2
	while t % 3 == 0:
		ft[3] += 1
		t //= 3
	ft['x'] = t
	return ft
(n, m) = input().split()
ft_n = fact(n)
ft_m = fact(m)
if ft_n[2] <= ft_m[2] and ft_n[3] <= ft_m[3] and (ft_n['x'] == ft_m['x']):
	print(ft_m[2] - ft_n[2] + ft_m[3] - ft_n[3])
else:
	print(-1)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n, m) = I()
ans = 0
if m % n != 0:
	print(-1)
	exit()
k = m // n
while k % 2 == 0:
	ans += 1
	k //= 2
while k % 3 == 0:
	ans += 1
	k //= 3
if k == 1:
	print(ans)
else:
	print(-1)


input = input()
newarray = [int(i) for i in input.split()]
dividednumber = newarray[1] / newarray[0]
count = 0
while dividednumber != 1:
	if dividednumber % 3 == 0:
		dividednumber = dividednumber / 3
		count = count + 1
	elif dividednumber % 2 == 0:
		dividednumber = dividednumber / 2
		count = count + 1
	else:
		count = -1
		break
print(count)


import math
(x, y) = input().split()
n = int(x)
m = int(y)
if m == n:
	answer = 0
else:
	twos = 0
	a = m / n
	while a % 2 == 0:
		twos += 1
		a = a / 2
	threes = 0
	b = a
	while b % 3 == 0:
		threes += 1
		b = b / 3
	if b == 1:
		answer = twos + threes
	else:
		answer = -1
print(answer)


(a, b) = map(int, input().split())
dem = 0
z = b / a
while z % 2 == 0:
	z /= 2
	dem += 1
while z % 3 == 0:
	z /= 3
	dem += 1
if z == 1:
	print(dem)
else:
	print(-1)


k = [*map(int, input().split())]
o = 0
if k[0] == k[1]:
	print(0)
elif k[1] % k[0] == 0:
	z = k[1] / k[0]
	while z % 2 == 0:
		z /= 2
		o += 1
	while z % 3 == 0:
		z /= 3
		o += 1
	if z == 1:
		print(o)
	else:
		print(-1)
else:
	print(-1)


array = input().split(' ')
n = int(array[0])
m = int(array[1])
count = 0
k = 0
if m == n:
	print(0)
elif m % n == 0:
	k = m // n
	while True:
		if k == 1:
			print(count)
			break
		else:
			if k % 3 == 0:
				k = k // 3
			elif k % 2 == 0:
				k = k // 2
			else:
				print(-1)
				break
			count += 1
else:
	print(-1)


def ans(n, m):
	a2 = 0
	a1 = 0
	b1 = 0
	b2 = 0
	if m % n == 0 and n != 1:
		while n % 2 == 0:
			n = n // 2
			a1 += 1
		while n % 3 == 0:
			n = n // 3
			b1 += 1
		while m % 2 == 0:
			m = m // 2
			a2 += 1
		while m % 3 == 0:
			m = m // 3
			b2 += 1
		if n == m:
			return a2 - a1 + (b2 - b1)
		else:
			return -1
	elif n == 1:
		while m % 2 == 0:
			m = m // 2
			a2 += 1
		while m % 3 == 0:
			m = m // 3
			b2 += 1
		if m == 1:
			return a2 + b2
		else:
			return -1
	else:
		return -1
(n, m) = map(int, input().split())
print(ans(n, m))


s = str(input())
a = s.split(' ')
n = int(a[0])
m = int(a[1])
count1 = 0
count2 = 0
if n == m:
	print(0)
elif m % n != 0:
	print(-1)
else:
	x = m / n
	while True:
		if x % 3 == 0 and x != 0:
			x = x / 3
			count1 += 1
		else:
			break
	while True:
		if x % 2 == 0 and x != 0:
			x = x / 2
			count2 += 1
		else:
			break
	if m == n * 2 ** count2 * 3 ** count1:
		print(count1 + count2)
	else:
		print(-1)


(n, m) = list(map(int, input().split()))
(a, b) = divmod(m, n)
if b == 0:
	cnt = 0
	flag = True
	while a != 1:
		if a % 2 == 0:
			a /= 2
			cnt += 1
			continue
		if a % 3 == 0:
			cnt += 1
			a /= 3
			continue
		elif a != 1:
			flag = False
			break
	if flag:
		print(cnt)
	else:
		print(-1)
else:
	print(-1)


from math import log
(n, m) = map(int, input().split())
c = m / n
ans = 0
if c == m // n:
	while not c % 2:
		ans += 1
		c //= 2
	while not c % 3:
		ans += 1
		c //= 3
if c > 0 and c != 1:
	ans = -1
print(ans)


(n, m) = map(int, input().split())
cnt = -1
if m % n == 0:
	m //= n
	cnt = 0
	for i in (2, 3):
		while m % i == 0:
			m //= i
			cnt += 1
if m > 1:
	cnt = -1
print(cnt)


def divv(num, divider):
	count = 0
	while num % divider == 0:
		num //= divider
		count += 1
	return (num, count)

def main():
	(n, m) = [int(x) for x in input().split()]
	diff = m / n
	if int(diff) != diff:
		return -1
	(diff, count_2) = divv(diff, 2)
	(diff, count_3) = divv(diff, 3)
	return -1 if diff != 1 else count_2 + count_3
res = main()
print(res)


(a, b) = map(int, input().split())
count = 0
if a == b:
	print(0)
elif b % a != 0 or (b % 2 != 0 and b % 3 != 0):
	print(-1)
else:
	b = b // a
	for i in range(28, -1, -1):
		if b % 2 ** i == 0:
			b = b // 2 ** i
			break
	for j in range(18, -1, -1):
		if b % 3 ** j == 0:
			b = b // 3 ** j
			break
	if b != 1:
		print(-1)
	else:
		count = i + j
		print(count)


(n, m) = map(int, input().split())
count = 0
a = 0
b = 0
if m % n == 0:
	if m / n == 1:
		print(0)
	else:
		k = m // n
		while k % 2 == 0:
			k = k // 2
			a += 1
		while k % 3 == 0:
			k = k // 3
			b += 1
		count = a + b
		if a + b == 0:
			count = -1
		if 2 ** a * 3 ** b != m // n:
			count = -1
		print(count)
else:
	print(-1)


import sys
(a, b) = map(int, input().split())

def s(a, b, n):
	if b // 2 == b / 2 and b // 2 >= a:
		if b // 2 == a:
			print(n + 1)
			sys.exit()
		else:
			s(a, b // 2, n + 1)
	if b // 3 == b / 3 and b // 3 >= a:
		if b // 3 == a:
			print(n + 1)
			sys.exit()
		else:
			s(a, b // 3, n + 1)
if a == b:
	print(0)
	sys.exit()
s(a, b, 0)
print(-1)


(n, m) = list(map(int, input().split()))
if m % n != 0:
	print(-1)
else:
	d = int(m / n)
	a = 0
	b = 0
	while d % 2 == 0:
		d = int(d / 2)
		a += 1
	while d % 3 == 0:
		d = int(d / 3)
		b += 1
	if d == 1:
		print(a + b)
	else:
		print(-1)


def gh(a, n):
	if a == 1:
		return n
	if a % 2 == 0:
		return gh(a // 2, n + 1)
	if a % 3 == 0:
		return gh(a // 3, n + 1)
	else:
		return -1
(a, b) = map(int, input().split())
if b % a:
	print(-1)
else:
	b = b // a
	print(gh(b, 0))


import math

def primeFactors(n):
	c = []
	while n % 2 == 0:
		c.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			c.append(i)
			n = n // i
	if n > 2:
		c.append(n)
	return c
(n, m) = map(int, input().split())
k = m // n
l = m % n
if l != 0 or (k % 3 != 0 and k % 2 != 0):
	if n == m:
		print(0)
	else:
		print(-1)
else:
	z = primeFactors(k)
	if z.count(2) + z.count(3) == len(z):
		print(len(z))
	else:
		print(-1)


(a, b) = [int(x) for x in input().split(' ')]
if b / a == int(b / a):
	diff = int(b / a)
else:
	print(-1)
	exit()
n = 0
while diff > 1:
	if diff % 6 == 0:
		diff = diff // 6
		n += 2
	elif diff % 3 == 0:
		diff = diff // 3
		n += 1
	elif diff % 2 == 0:
		diff = diff // 2
		n += 1
	else:
		print(-1)
		exit()
print(n)


(n, m) = (int(t) for t in input().split())
if n == m:
	print('0')
elif m % n == 0:
	ndiv = 0
	mdiv = 0
	while n % 2 == 0:
		n = n / 2
		ndiv += 1
	while n % 3 == 0:
		n = n / 3
		ndiv += 1
	while m % 2 == 0:
		m = m / 2
		mdiv += 1
	while m % 3 == 0:
		m = m / 3
		mdiv += 1
	if n != m:
		print('-1')
	else:
		print(mdiv - ndiv)
else:
	print('-1')


import sys
(n, m) = [int(i) for i in sys.stdin.readline().split()]
k = m / n
t = 0
while k != 1:
	if k % 2 == 0:
		k /= 2
		t += 1
	elif k % 3 == 0:
		k /= 3
		t += 1
	else:
		t = -1
		break
sys.stdout.write(str(t))


def getOdd(num):
	cnt = 0
	while num % 3 == 0:
		num //= 3
		cnt += 1
	while num % 2 == 0:
		num //= 2
		cnt += 1
	if num != 1:
		return -1
	else:
		return cnt
(n, m) = map(int, input().split())
print(getOdd(m / n))


(n, m) = map(int, input().split())

def solution(m, n):
	d = m / n
	if d % 1 != 0:
		return -1
	d = int(d)
	i = 0
	j = 0
	while d % 2 == 0:
		d /= 2
		i += 1
	while d % 3 == 0:
		d /= 3
		j += 1
	if d > 1:
		return -1
	return i + j
print(solution(m, n))


(n, m) = map(int, input().split())
k = m / n
num = 1
counter = 0
while num < k:
	if (k / 2).is_integer():
		k //= 2
	elif (k / 3).is_integer():
		k //= 3
	else:
		counter = -1
		break
	counter += 1
print(counter)


(n, m) = map(int, input().split())

def sol(n, m):
	for x in range(0, 64):
		for y in range(0, 64):
			if n * (2 ** x * 3 ** y) == m:
				return x + y
	return -1
print(sol(n, m))


(n, m) = [int(x) for x in input().split()]
divisor = float(m / n)
num_moves = 0
divisor /= 2
while divisor == int(divisor):
	num_moves += 1
	divisor /= 2
divisor *= 2
divisor /= 3
while divisor == int(divisor):
	num_moves += 1
	divisor /= 3
divisor *= 3
if divisor != 1:
	num_moves = -1
print(num_moves)


(n, m) = map(int, input().split())
counter = 0
result = -1
if m % n == 0:
	result = 0
	modu = m // n
	while modu % 3 == 0:
		counter += 1
		modu = modu // 3
	while modu % 2 == 0:
		counter += 1
		modu = modu // 2
	if modu != 1:
		result = -1
	else:
		result = counter
print(result)


(n, m) = [int(x) for x in input().split()]
cnt = 0
if m % n:
	print('-1')
	exit()
elif m == n:
	print('0')
	exit()
else:
	q = m // n
	while q != 1:
		if q % 2 == 0:
			cnt = cnt + 1
			q = q // 2
		elif q % 3 == 0:
			cnt = cnt + 1
			q = q // 3
		else:
			print('-1')
			exit()
print(cnt)


def solve(n, m):
	if m % n == 0:
		d = m // n
		two = 2
		three = 3
		cnt = 0
		while d % two == 0:
			d = d // two
			cnt += 1
		if d == 1:
			return cnt
		while d % three == 0:
			d = d // three
			cnt += 1
		if d == 1:
			return cnt
		else:
			return -1
	else:
		return -1
line = input()
n = int(line.split(' ')[0])
m = int(line.split(' ')[1])
print(solve(n, m))


nos = list(map(lambda x: int(x), input().split()))
mul = nos[1] / nos[0]
k = 1
count = 0
flag = False
while mul != 1:
	if not (mul % 3 == 0 or mul % 2 == 0):
		count = -1
		break
	elif mul / 3 >= 1 and mul % 3 == 0:
		mul /= 3
		count += 1
	else:
		mul /= 2
		count += 1
print(count)


(x, y) = map(int, input().split())
counter = 0
if int(y / x) != y / x:
	print(-1)
else:
	z = y / x
	while 1:
		if z % 6 == 0 and z != 1:
			z = z / 6
			counter += 2
		else:
			break
	while 1:
		if z % 3 == 0 and z != 1:
			z = z / 3
			counter += 1
		else:
			break
	while 1:
		if z % 2 == 0 and z != 1:
			z = z / 2
			counter += 1
		else:
			break
	if z == 1:
		print(counter)
	else:
		print(-1)


(a, b) = map(int, input().split())
count = 0
if b % a != 0:
	print(-1)
else:
	d = b // a
	while d % 2 == 0:
		d = d // 2
		count += 1
	while d % 3 == 0:
		d = d // 3
		count += 1
	if d == 1:
		print(count)
	else:
		print(-1)


import math
from collections import defaultdict, Counter, deque

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def main():
	(n, m) = map(int, input().split())
	if m % n == 0:
		num = m // n
		count = 0
		while num > 1:
			if num % 2 == 0:
				num = num // 2
				count += 1
			elif num % 3 == 0:
				num = num // 3
				count += 1
			else:
				print(-1)
				return
		print(count)
		return
	print(-1)
	return
t = 1
for _ in range(t):
	main()


(n, m) = [int(i) for i in input().split()]
if m % n != 0:
	print('-1')
else:
	d = m // n
	count = 0
	while d > 1:
		if d % 2 == 0:
			d = d // 2
			count += 1
		elif d % 3 == 0:
			d = d // 3
			count += 1
		else:
			d = -9
	if d == 1:
		print(count)
	else:
		print('-1')


import sys
(n, m) = list(map(int, input().split()))
if m % n != 0:
	print(-1)
else:
	rem = m // n
	count = 0
	while rem != 1:
		if rem % 2 == 0:
			rem = rem // 2
		elif rem % 3 == 0:
			rem = rem // 3
		else:
			print(-1)
			sys.exit()
		count += 1
	print(count)


(n, m) = [int(i) for i in input().split()]
c = m / n
finish = False
ans = 0
if m % n == 0:
	while c != 1:
		if c % 2 == 0:
			c /= 2
			ans += 1
		elif c % 3 == 0:
			c /= 3
			ans += 1
		else:
			ans = -1
			break
else:
	ans = -1
print(ans)


import math

class CodeforcesTask1141ASolution:

	def __init__(self):
		self.result = ''
		self.n_m = []

	def read_input(self):
		self.n_m = [int(x) for x in input().split(' ')]

	def process_task(self):
		res = -1
		for a in range(int(math.log(self.n_m[1], 2)) + 1):
			for b in range(int(math.log(self.n_m[1], 3)) + 1):
				if self.n_m[0] * 2 ** a * 3 ** b == self.n_m[1]:
					res = a + b
					break
			if res != -1:
				break
		self.result = str(res)

	def get_result(self):
		return self.result
Solution = CodeforcesTask1141ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


def solve(a, b):
	if b % a != 0:
		return -1
	c = b // a
	steps = 0
	while c % 2 == 0:
		c //= 2
		steps += 1
	while c % 3 == 0:
		c //= 3
		steps += 1
	if c != 1:
		return -1
	return steps

def main() -> None:
	(a, b) = map(int, input().split())
	print(solve(a, b))
main()


import sys
(n, m) = list(map(int, input().split(' ')))
if m % n != 0:
	print(-1)
	sys.exit()
a = m // n
i = 0
while True:
	if a == 1:
		print(i)
		break
	elif a % 2 == 0:
		a //= 2
	elif a % 3 == 0:
		a //= 3
	else:
		print(-1)
		break
	i += 1


import sys

def getAndParseInt(num=1):
	string = sys.stdin.readline().strip()
	if num == 1:
		return int(string)
	else:
		return [int(part) for part in string.split()]

def getAndParseString(num=1, delim=''):
	string = sys.stdin.readline().strip()
	if num == 1:
		return string
	elif delim != '':
		return [part for part in string.split(delim)]
	else:
		return [part for part in string.split()]
(n, m) = getAndParseInt(2)
if m % n == 0:
	k = m // n
	(cnt_2, cnt_3) = (0, 0)
	while k % 2 == 0:
		cnt_2 += 1
		k = k // 2
	while k % 3 == 0:
		cnt_3 += 1
		k = k // 3
	if 2 ** cnt_2 * 3 ** cnt_3 * n == m:
		print(cnt_2 + cnt_3)
	else:
		print('-1')
else:
	print('-1')


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	ans = m // n
	sum = 0
	while ans % 2 == 0 or ans % 3 == 0:
		if ans % 2 == 0:
			sum += 1
			ans //= 2
		elif ans % 3 == 0:
			sum += 1
			ans //= 3
	if ans == 1:
		print(sum)
	else:
		print(-1)


(n, m) = map(int, input().split())
if m / n - m // n == 0:
	k = int(m / n)
	c = 0
	while k != 1:
		if k / 2 - k // 2 != 0:
			break
		k = k // 2
		c += 1
	while k != 1:
		if k / 3 - k // 3 != 0:
			break
		k = k // 3
		c += 1
	if k != 1:
		print(-1)
	else:
		print(c)
else:
	print(-1)


l = input().split()
a = int(l[0])
b = int(l[1])
if a == b:
	print('0')
elif b % a != 0:
	print(str('-1'))
elif a == 1 and b % 2 != 0 and (b % 3 != 0):
	print(str('-1'))
else:
	count = 0
	if b % a == 0:
		b = b / a
	while b % 2 == 0:
		count += 1
		b /= 2
	while b % 3 == 0:
		count += 1
		b /= 3
	if b != 1:
		print(str('-1'))
	else:
		print(str(count))


(b, a) = list(map(int, input().split()))
count = 0
done = False
if a % b != 0:
	print(-1)
else:
	while not done:
		if a % (b * 3) == 0:
			b = b * 3
			count += 1
		elif a % (b * 2) == 0:
			b = b * 2
			count += 1
		else:
			done = True
	if done == True and a == b:
		print(count)
	else:
		print(-1)


(n, m) = [int(i) for i in input().split()]
if m % n != 0:
	print('-1')
	exit(0)
multiple = m // n
count2 = 0
while multiple % 2 == 0:
	count2 += 1
	multiple /= 2
count3 = 0
while multiple % 3 == 0:
	count3 += 1
	multiple /= 3
csum = count2 + count3
if n == 1 and m == 1:
	print('0')
elif n == 1 and csum == 0:
	print('-1')
elif n * 2 ** count2 * 3 ** count3 == m:
	print(count2 + count3)
else:
	print('-1')


(num1, num2) = map(int, input().split(' '))
if num2 % num1 != 0:
	print(-1)
	exit(0)
d = num2 // num1
turns = 0
while True:
	if d % 2 == 0:
		d //= 2
		turns += 1
	else:
		break
while True:
	if d % 3 == 0:
		d //= 3
		turns += 1
	else:
		break
if d == 1:
	print(turns)
else:
	print(-1)


(n, m) = map(int, input().split())
c = 0
result = -1
if m % n == 0:
	result = 0
	m = m // n
	while m % 2 == 0:
		m = m // 2
		result += 1
	while m % 3 == 0:
		m = m // 3
		result += 1
	if m != 1:
		result = -1
print(result)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	bul = True
	dig = m // n
	ans = 0
	while bul and dig > 1:
		if dig % 2 != 0 and dig % 3 != 0:
			print(-1)
			bul = False
		elif dig % 2 == 0:
			dig = dig // 2
			ans += 1
		else:
			dig = dig // 3
			ans += 1
	if bul:
		print(ans)


(n, k) = map(int, input().split())
c = k // n
e = 0
if k % n != 0:
	print(-1)
elif c == 1:
	print(-0)
else:
	while c % 3 == 0:
		e += 1
		c = c // 3
	while c % 2 == 0:
		e += 1
		c = c // 2
	if c == 1:
		print(e)
	else:
		print(-1)


visited = set()

def dfs(curr, m, l):
	global visited
	visited.add(curr)
	ans = -1
	if curr == m:
		return l
	if curr > m:
		return -1
	if 2 * curr not in visited:
		ans = dfs(2 * curr, m, l + 1)
		if ans != -1:
			return ans
	if 3 * curr not in visited:
		ans = dfs(3 * curr, m, l + 1)
		if ans != -1:
			return ans
	return -1
(n, m) = (int(x) for x in input().split())
print(dfs(n, m, 0))


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict
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()
(n, m) = listIn()
if n == m:
	print(0)
elif m < n * 2:
	print(-1)
elif m % n == 0:
	c = m // n
	cnt = 0
	while 1:
		if c % 2 == 0:
			c = c // 2
			cnt += 1
		elif c % 3 == 0:
			c = c // 3
			cnt += 1
		else:
			if c == 1:
				ans = cnt
			else:
				ans = -1
			break
	print(ans)
else:
	print(-1)


import sys
(x, y) = map(int, input().split())
a = 0
if y % x:
	print(-1)
	sys.exit()
y //= x
while y % 2 == 0:
	y //= 2
	a += 1
while y % 3 == 0:
	y //= 3
	a += 1
if y == 1:
	print(a)
	sys.exit()
print(-1)


(n, m) = map(int, input().split())
moves = 0
if n > m:
	print(-1)
elif m % n != 0:
	print(-1)
else:
	t = m // n
	moves = 0
	if t == 1:
		print(moves)
	else:
		z = t
		ans = 0
		while 1:
			if z == 1:
				ans = 1
				break
			if z % 3 == 0:
				z = z // 3
				moves += 1
				continue
			if z % 2 == 0:
				z = z // 2
				moves += 1
				continue
			break
		if ans == 1:
			print(moves)
		else:
			print(-1)


(n, m) = input().split()
n = int(n)
m = int(m)
a = m / n
b = True
p = 0
while a > 1 and b == True:
	if a % 2 == 0:
		a = a / 2
		p = p + 1
	elif a % 3 == 0:
		a = a / 3
		p = p + 1
	elif a % 2 != 0 and a % 3 != 0:
		b = False
if a == 1:
	print(p)
else:
	print(-1)


from math import log
z = input()
k = [int(n) for n in z.split()]
aa = k[0]
bb = k[1]
x = bb / aa
n = 0
f = float()
u = p = z = int()
if x % 2 == 0 or x % 3 == 0:
	if x % 3 == 0:
		while x / 3 ** n == int(x / 3 ** n):
			n += 1
		z = n - 1
		p = 3
		u = 2
		f = log(x / p ** z) / log(u)
		if f == int(f):
			print(int(f + z))
		else:
			print(-1)
	elif x % 2 == 0:
		while x / 2 ** n == int(x / 2 ** n):
			n += 1
		z = n - 1
		p = 2
		u = 3
		f = log(x / p ** z) / log(u)
		if f == int(f):
			print(int(f + z))
		else:
			print(-1)
elif x == 1:
	print(0)
else:
	print(-1)


(n, m) = input().split()
(n, m) = [int(n), int(m)]
div = m / n
i = 0
while True:
	if div % 3 == 0:
		i += 1
		div = div / 3
	elif div % 2 == 0:
		i += 1
		div = div / 2
	else:
		break
if div != 1:
	i = 0
if i == 0:
	i = -1
if n == m:
	i = 0
print(i)


s = input()
s = s.split(' ')
n = int(s[0])
m = int(s[1])
if m % n != 0:
	print(-1)
else:
	x = m // n
	flag2 = 1
	flag3 = 1
	steps = 0
	while flag2 == 1:
		if x % 2 == 0:
			x = x // 2
			steps = steps + 1
		else:
			flag2 = 0
	if x == 1:
		print(steps)
	else:
		while flag3 == 1:
			if x % 3 == 0:
				x = x // 3
				steps = steps + 1
			else:
				flag3 = 0
		if x == 1:
			print(steps)
		else:
			print(-1)


(m, n) = input().split()
m = int(m)
n = int(n)
s = 0
r = 0
if n % m != 0:
	print('-1')
else:
	r = n / m
	if r == 1:
		print('0')
	else:
		while r != 1:
			if r % 3 == 0:
				s += 1
				r /= 3
			elif r % 2 == 0:
				s += 1
				r /= 2
			else:
				s = -1
				break
		print(s)


import math
(a, b) = list(map(int, input().split()))

def thing(n, m):
	if m < n:
		return -1
	else:
		return powers(m / n)

def powers(org):
	base = 3
	n = 0
	while True:
		if org % base == 0:
			n += 1
			org /= base
		else:
			break
	if math.log2(org) == int(math.log2(org)):
		return n + int(math.log2(org))
	else:
		return -1
print(thing(a, b))


x = input()
y = x.split()
y = [int(i) for i in y]
small = y[0]
big = y[1]
if small == big:
	print(0)
elif small == 0 and big != 0:
	print(-1)
elif small != 0 and big == 0:
	print(-1)
else:
	result = big / small
	count = 0
	while result % 2 == 0:
		count += 1
		result /= 2
	while result % 3 == 0:
		count += 1
		result /= 3
	if result == 1:
		print(count)
	else:
		print(-1)


(n, m) = map(int, input().split())
(two_n, three_n, two_m, three_m) = (0, 0, 0, 0)
while n % 2 == 0:
	n //= 2
	two_n += 1
while n % 3 == 0:
	three_n += 1
	n //= 3
while m % 2 == 0:
	m //= 2
	two_m += 1
while m % 3 == 0:
	m //= 3
	three_m += 1
if n == m and two_m >= two_n and (three_m >= three_n):
	print(three_m + two_m - three_n - two_n)
else:
	print('-1')


(a, b) = input().split()
a = int(a)
b = int(b)
c = 0
if b % a != 0:
	print(-1)
else:
	d = b // a
	while d % 2 == 0:
		d = d // 2
		c = c + 1
	while d % 3 == 0:
		d = d // 3
		c = c + 1
	if d != 1:
		print(-1)
	else:
		print(c)


def go():
	(n, m) = map(int, input().split(' '))
	if m % n != 0:
		return -1
	d = m / n
	c = 0
	while d % 2 == 0:
		d /= 2
		c += 1
	while d % 3 == 0:
		d /= 3
		c += 1
	if d == 1:
		return c
	return -1
print(go())


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 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 ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

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 solve():
	(n, m) = sep()
	if m % n != 0:
		print(-1)
		return
	temp = m // n
	x = 0
	y = 0
	while temp % 2 ** (x + 1) == 0:
		x += 1
	while temp % 3 ** (y + 1) == 0:
		y += 1
	if 2 ** x * 3 ** y != temp:
		print(-1)
	else:
		print(x + y)
testcase(1)


(n, m) = map(int, input().split())
count = 1
temp = m / n
if n == m:
	print(0)
else:
	for i in range(30):
		if temp % 2 == 0 and temp / 2 > 1:
			count += 1
			temp /= 2
		elif temp % 3 == 0 and temp / 3 > 1:
			count += 1
			temp /= 3
		elif temp % 3 != 0 and temp % 2 != 0 and (temp / 2 != temp // 2) and (temp / 3 != temp // 3):
			count = -1
	print(count)


import collections
(n, m) = map(int, input().split())
if n == m:
	print(0)
	exit()
if m < 2 * n:
	print(-1)
	exit()

def prime_factorize(n):
	a = []
	while n % 2 == 0:
		a.append(2)
		n //= 2
	f = 3
	while f * f <= n:
		if n % f == 0:
			a.append(f)
			n //= f
		else:
			f += 2
	if n != 1:
		a.append(n)
	return a
c = prime_factorize(n)
d = prime_factorize(m)
tc = 0
thc = 0
x = []
for i in c:
	if i == 2:
		tc += 1
	elif i == 3:
		thc += 1
	else:
		x.append(i)
td = 0
thd = 0
y = []
for i in d:
	if i == 2:
		td += 1
	elif i == 3:
		thd += 1
	else:
		y.append(i)
ans = 0
if x == y and tc <= td and (thc <= thd):
	ans = td - tc + thd - thc
else:
	ans = -1
print(ans)


(n, m) = list(map(int, input().split()))
l = m / n
if l != int(l):
	print(-1)
else:
	l = int(l)
	if l == 1:
		print(0)
	else:
		k = 0
		while l % 2 == 0:
			l //= 2
			k += 1
		while l % 3 == 0:
			l //= 3
			k += 1
		if l == 1:
			print(k)
		else:
			print(-1)


(n, m) = map(int, input().split())
dif = m // n
loop = 0
if m % n != 0:
	print(-1)
else:
	while True:
		if dif % 2 != 0:
			break
		else:
			dif //= 2
			loop += 1
	while True:
		if dif % 3 != 0:
			break
		else:
			dif //= 3
			loop += 1
	if dif != 1:
		print(-1)
	else:
		print(loop)


import sys
from functools import reduce
(n, m) = map(int, sys.stdin.readline().split())
turns = 0
if m == n:
	print(turns)
elif n == 1:
	if m % 2 != 0 and m % 3 != 0:
		print(-1)
	else:
		rest = m / n
		first = n
		while first != m:
			if rest % 2 == 0:
				turns += 1
				first *= 2
				rest /= 2
			elif rest % 3 == 0:
				turns += 1
				first *= 3
				rest /= 3
			else:
				turns = -1
				break
		print(turns)
elif m % n != 0:
	print(-1)
else:
	rest = m / n
	first = n
	while first != m:
		if rest % 2 == 0:
			turns += 1
			first *= 2
			rest /= 2
		elif rest % 3 == 0:
			turns += 1
			first *= 3
			rest /= 3
		else:
			turns = -1
			break
	print(turns)


from math import ceil
(n, m) = map(int, input().split())
moves = 0
if m % n != 0:
	print(-1)
else:
	m //= n
	while m != 1:
		if m % 3 == 0:
			m //= 3
			moves += 1
		elif m % 2 == 0:
			m //= 2
			moves += 1
		else:
			break
	if m == 1:
		print(moves)
	else:
		print(-1)


(n, m) = list(map(int, input().split()))
if m % n != 0:
	print('-1\n')
else:
	div = m // n
	cnt = 0
	while div > 1:
		cnt += 1
		if div % 3 == 0:
			div //= 3
		elif div % 2 == 0:
			div //= 2
		else:
			div = -1
			cnt = -1
	print(cnt)


(a, b) = [int(i) for i in input().split()]
result = -1
if b % a == 0:
	result = 0
	d = b // a
	while d % 2 == 0:
		d = d // 2
		result = result + 1
	while d % 3 == 0:
		d = d // 3
		result = result + 1
	if d != 1:
		result = -1
print(result)


def find(n, m):
	tmp = int(m / n)
	a = 0
	while tmp % 2 == 0:
		tmp /= 2
		a += 1
	b = 0
	while tmp % 3 == 0:
		tmp /= 3
		b += 1
	if tmp != 1:
		return -1
	else:
		return a + b
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit()
print(find(n, m))


(n, m) = map(int, input().split())
c = 0
import math, sys
while n != m:
	c += 1
	if math.gcd(n * 2, m) == n * 2:
		n *= 2
	elif math.gcd(n * 3, m) == n * 3:
		n *= 3
	else:
		print(-1)
		sys.exit()
print(c)


def divisibility_by_2(a, b):
	counter = 0
	n = b // a
	while n % 2 == 0 or n % 3 == 0:
		if n % 2 == 0:
			n = n // 2
		elif n % 3 == 0:
			n = n // 3
		counter += 1
	return (n, counter)

def main_function():
	(n, m) = [int(i) for i in input().split(' ')]
	if m % n != 0:
		print(-1)
	else:
		(final, counter) = divisibility_by_2(n, m)
		if final != 1:
			print(-1)
		else:
			print(counter)
main_function()


nums = [int(i) for i in input().split(' ')]
if nums[1] % nums[0] != 0:
	print(-1)
else:
	moves = 0
	while nums[1] % nums[0] == 0:
		nums[1] /= 2
		moves += 1
	nums[1] *= 2
	while nums[1] % nums[0] == 0:
		nums[1] /= 3
		moves += 1
	nums[1] *= 3
	if nums[1] != nums[0]:
		print(-1)
	else:
		print(moves - 2)


(n, m) = map(int, input().split())
if m % n == 0:
	v = m // n
	c = 0
	p = 0
	while v > 1:
		if v % 2 == 0:
			v = v // 2
			c += 1
		elif v % 3 == 0:
			v = v // 3
			c += 1
		else:
			p = 1
			break
	if p == 1:
		print(-1)
	else:
		print(c)
else:
	print(-1)


(init, goal) = map(int, input().split())
(i, g) = (init, goal)
ti = [0, 0]
tg = [0, 0]
while True:
	if i % 2 and i % 3:
		break
	if i % 2 == 0:
		ti[0] += 1
		i //= 2
	if i % 3 == 0:
		ti[1] += 1
		i //= 3
while True:
	if g % 2 and g % 3:
		break
	if g % 2 == 0:
		tg[0] += 1
		g //= 2
	if g % 3 == 0:
		tg[1] += 1
		g //= 3
init *= 2 ** (tg[0] - ti[0]) * 3 ** (tg[1] - ti[1])
if ti[0] > tg[0] or ti[1] > tg[1] or init != goal:
	print(-1)
else:
	print(sum(tg) - sum(ti))


(n, m) = map(int, input().split())
t = 1
if m % n != 0:
	print(-1)
	t = 0
else:
	ctr = 0
	c = 0
	a = m / n
	while c == 0:
		if a == 1 or a == 0:
			c = 1
			break
		elif a % 6 == 0:
			ctr += 2
			a = a / 6
		elif a % 2 == 0:
			ctr += 1
			a /= 2
		elif a % 3 == 0:
			ctr += 1
			a /= 3
		else:
			print(-1)
			t = 0
			c = 1
if t == 1:
	print(ctr)


(n, m) = input().split()
n = int(n)
m = int(m)
if m % n != 0:
	print(-1)
else:
	step = 0
	k = int(m / n)
	while True:
		if k % 3 == 0:
			step += 1
			k /= 3
		if k % 2 == 0:
			step += 1
			k /= 2
		if k % 3 != 0 and k % 2 != 0:
			break
	if k > 3:
		print(-1)
	else:
		print(step)


from math import *
(n, m) = map(int, input().split(' '))
if m % n != 0:
	print(-1)
	exit(0)
c = m // n
cnt = 0
while c % 2 == 0:
	c //= 2
	cnt += 1
while c % 3 == 0:
	c //= 3
	cnt += 1
if c != 1:
	print(-1)
else:
	print(cnt)


import math

def process(a, b):
	c = b // a
	if c < 1 or c != b / a:
		print(-1)
	elif c == 1:
		print(0)
	else:
		left = math.floor(math.log(c, 3))
		right = math.ceil(math.log(c, 2))
		flag = False
		res = 0
		for i in range(left, right + 1):
			for j in range(i + 1):
				if 2 ** (i - j) * 3 ** j == c:
					res = i
					flag = True
					break
		if flag:
			print(res)
		else:
			print(-1)
(a, b) = list(map(int, input().split()))
process(a, b)


(b, a) = input().split()
a = int(a)
b = int(b)
i = 0
if a % b == 0:
	c = a / b
	while c % 2 == 0 or c % 3 == 0:
		if c % 2 == 0:
			c = c / 2
			i += 1
		elif c % 3 == 0:
			c = c / 3
			i += 1
	if c == 1:
		print(i)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	s2 = 0
	s3 = 0
	t = int(m / n)
	while t % pow(2, s2) == 0:
		s2 = s2 + 1
	while t % pow(3, s3) == 0:
		s3 = s3 + 1
	if pow(2, s2 - 1) * pow(3, s3 - 1) == t:
		print(s2 + s3 - 2)
	else:
		print(-1)


(N, M) = map(int, input().split())
if M % N == 0:
	count = 0
	X = M / N
	while X % 2 == 0:
		X /= 2
		count += 1
	while X % 3 == 0:
		X /= 3
		count += 1
	if X != 1:
		print('-1')
	else:
		print(count)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n == 0:
	b = m // n
	f = True
	c = 0
	while b != 1:
		if b % 2 == 0:
			b //= 2
		elif b % 3 == 0:
			b //= 3
		else:
			f = False
			break
		c += 1
	if not f:
		print(-1)
	else:
		print(c)
else:
	print(-1)


inp = list(map(int, input().split()))
n = inp[0]
m = inp[1]
if m % n != 0:
	print(-1)
else:
	count = 0
	k = m // n
	while k % 2 == 0:
		k = k // 2
		count += 1
	while k % 3 == 0:
		k = k // 3
		count += 1
	if k != 1:
		print(-1)
	else:
		print(count)


import sys
(n, m) = map(int, sys.stdin.readline().split())

def factorize_out(i):
	(count2, count3) = (0, 0)
	while True:
		done = False
		if not i % 2:
			count2 += 1
			done = True
			i = i / 2
		if not i % 3:
			count3 += 1
			done = True
			i = i / 3
		if not done:
			break
	return (i, count2, count3)
(nr, n2, n3) = factorize_out(n)
(mr, m2, m3) = factorize_out(m)
if mr == nr and n2 <= m2 and (n3 <= m3):
	print(m2 + m3 - n2 - n3)
else:
	print(-1)


(n, m) = map(int, input().split())
if m % n:
	exit(print(-1))
z = m // n
t = 0
while z % 2 == 0:
	t += 1
	z //= 2
while z % 3 == 0:
	t += 1
	z //= 3
if z != 1:
	print(-1)
else:
	print(t)


def f(l):
	if l[1] % l[0] > 0:
		return -1
	m = l[1] // l[0]
	c = 0
	while m % 2 == 0:
		m = m // 2
		c += 1
	while m % 3 == 0:
		m = m // 3
		c += 1
	return -1 if m > 1 else c
l = list(map(int, input().split()))
print(f(l))


(a, b) = [int(x) for x in input().split()]
i = 0
j = 0
ans = -1
for i in range(30):
	for j in range(30):
		if a * 2 ** i * 3 ** j == b:
			ans = i + j
			break
print(ans)


(n, m) = map(int, input().split())
if m < n:
	print(-1)
	exit(0)
(q, r) = divmod(m, n)
if r:
	print(-1)
	exit(0)
k = 0
for i in range(2, 3 + 1):
	while q % i == 0:
		k += 1
		q //= i
if q == 1:
	print(k)
else:
	print(-1)


alpha = input('')
for i in range(len(alpha)):
	if alpha[i] == ' ':
		n = int(alpha[:i])
		m = int(alpha[i + 1:])
tf = m / n
rem = 0
tc = rc = 0
while rem == 0:
	rem = tf % 2
	if rem == 0:
		tf = tf / 2
		tc += 1
rem = 0
while rem == 0:
	rem = tf % 3
	if rem == 0:
		tf = tf / 3
		rc += 1
if tf != 1:
	print('-1')
else:
	print(tc + rc)


def main(n, m):
	if m % n != 0:
		return -1
	index = m / n
	sum = 0
	while index != 1:
		if index % 2 == 0:
			index = index / 2
			sum = sum + 1
		elif index % 3 == 0:
			index = index / 3
			sum = sum + 1
		else:
			return -1
	return sum
nm = list(map(int, input().rstrip().split()))
n = nm[0]
m = nm[1]
res = main(n, m)
print(res)


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	p = m // n
	moves = 0
	a = p
	while a % 2 == 0 and a != 0:
		moves += 1
		a = a // 2
	while a % 3 == 0 and a != 0:
		moves += 1
		a = a // 3
	if a == 1:
		print(moves)
	else:
		print(-1)


(num1, num2) = map(int, input().split())
count = 0
if num1 == num2:
	print(count)
	exit()
res = num2 / num1
if res != int(res) or res < 2:
	count = -1
while res != 1:
	if res % 2 == 0:
		res = res / 2
		count += 1
		continue
	elif res % 3 == 0:
		res = res / 3
		count += 1
		continue
	else:
		count = -1
		break
print(count)


s = input().split(' ')
n = int(s[0])
m = int(s[1])
mas = []
step = []
for i in range(30):
	for j in range(30):
		if 2 ** i * 3 ** j < 5 * 10 ** 8 + 1:
			mas.append(2 ** i * 3 ** j)
			step.append(i + j)
f = False
if m % n == 0:
	for i in range(len(mas)):
		if mas[i] == m // n:
			f = True
			break
	if f:
		print(step[i])
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
nb = 0
if m % n != 0:
	print(-1)
else:
	while True:
		if m % 2 == 0 and m // 2 % n == 0 and (m != 1):
			nb += 1
			m = m // 2
		else:
			break
	while True:
		if m % 3 == 0 and m // 3 % n == 0 and (m != 1):
			nb += 1
			m = m // 3
		else:
			break
	if m == n:
		print(nb)
	else:
		print(-1)


import sys
(n, m) = map(int, sys.stdin.readline().split())
res = m % n
if res != 0:
	print(-1)
else:
	ans = m // n
	count = 0
	while ans > 1:
		if ans % 2 == 0:
			ans = ans // 2
			count += 1
		elif ans % 3 == 0:
			ans = ans // 3
			count += 1
		else:
			count = -1
			break
	print(count)


(n, m) = map(int, input().split())
s = 0
k = m / n
if int(k) != k:
	print(-1)
	exit()
k = int(k)

def qw(k, s):
	if k != 1:
		if k % 2 == 0:
			qw(k // 2, s + 1)
		elif k % 3 == 0:
			qw(k // 3, s + 1)
		else:
			print(-1)
			exit()
	else:
		print(s)
qw(k, s)


(n, m) = list(map(int, input().split()))
if m % n != 0:
	print(-1)
elif m == n:
	print(0)
else:
	f = m // n
	two = 0
	three = 0
	if f % 2 == 0 or f % 3 == 0:
		t = f
		while t % 2 == 0 or t % 3 == 0:
			if t % 2 == 0:
				two += 1
				t = t // 2
			if t % 3 == 0:
				three += 1
				t = t // 3
		if 2 ** two * 3 ** three == f:
			print(two + three)
		else:
			print(-1)
	else:
		print(-1)


(n, m) = tuple(map(int, input().split()))
if m % n != 0:
	print(-1)
else:
	res = m // n
	steps = 0
	while res % 2 == 0:
		steps += 1
		res //= 2
	while res % 3 == 0:
		steps += 1
		res //= 3
	if res == 1:
		print(steps)
	else:
		print(-1)


from math import *
from collections import *
from itertools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(100000000)
(a, b) = map(int, input().split())
if a == b:
	print(0)
elif b % a:
	print(-1)
else:
	a = b // a
	ans = 0
	c = 3
	i = 0
	while a % c ** (i + 1) == 0:
		i += 1
	ans += i
	a //= 3 ** i
	c = 2
	i = 0
	while a % c ** (i + 1) == 0:
		i += 1
	ans += i
	a //= 2 ** i
	if a > 1:
		print(-1)
	else:
		print(ans)


(a, b) = map(int, input().split())
if b % a != 0:
	print(-1)
else:
	cou = 0
	sh = b / a
	while sh != 1:
		if sh % 3 == 0:
			sh = sh / 3
			cou += 1
		elif sh % 2 == 0:
			sh = sh / 2
			cou += 1
		else:
			cou = -1
			break
	print(cou)


import sys
(n, m) = (int(i) for i in input().split())
q = int(m / n)
r = m % n
if r != 0:
	print('-1')
	sys.exit()
m = q
twocount = 0
while m % 2 == 0 and m > 1:
	m = m / 2
	twocount += 1
threecount = 0
while m % 3 == 0 and m > 1:
	m = m / 3
	threecount += 1
if m > 1:
	print('-1')
else:
	print(twocount + threecount)


import sys
(n, m) = map(int, sys.stdin.readline().split(' '))
if m % n != 0:
	print(-1)
	quit()
div = m / n
result = 0
while div > 1:
	result = result + 1
	if div % 3 == 0:
		div = div / 3
	elif div % 2 == 0:
		div = div / 2
	else:
		print(-1)
		quit()
print(result)


k = lambda : map(int, input().split())
l = lambda : list(k())
(n, m) = k()
if m % n == 0:
	m //= n
	c = 0
	while m % 3 == 0:
		(m, c) = (m // 3, c + 1)
	while m % 2 == 0:
		(m, c) = (m // 2, c + 1)
	if m == 1:
		print(c)
		exit()
print(-1)


(n, m) = list(map(int, input().split()))
if n == m:
	print(0)
	exit(0)
if m % n != 0:
	print(-1)
	exit(0)
m = m // n
val = 0
while m % 2 == 0:
	val += 1
	m //= 2
while m % 3 == 0:
	val += 1
	m //= 3
if m == 1:
	print(val)
else:
	print(-1)


from copy import copy
nm = list(map(int, input().split()))
(n, m, ans) = (nm[0], nm[1], 0)

def pf(k):
	ret = []
	sq = k ** 0.5
	i = 2
	while i <= sq:
		if k % i == 0:
			k /= i
			ret.append(i)
		else:
			i += 1
	if k != 1:
		ret.append(int(k))
	return ret
nl = pf(n)
ml = pf(m)
sub = copy(ml)
for i in nl:
	for j in ml:
		if i == j:
			try:
				sub.remove(i)
			except:
				print(-1)
				exit()
			break
for i in nl:
	if not i in ml:
		print(-1)
		exit()
for i in sub:
	if i != 2 and i != 3:
		print(-1)
		exit()
print(len(sub))


[n, m] = list(map(int, input().split()))
if m % n == 0:
	s = 0
	x = m // n
	while x % 2 == 0:
		x /= 2
		s += 1
	while x % 3 == 0:
		x /= 3
		s += 1
	if x == 1:
		print(s)
	else:
		print(-1)
else:
	print(-1)


(n, m) = map(int, input().split())
good = True
if m % n != 0:
	good = False
m //= n
cnt = 0
while m % 2 == 0:
	m //= 2
	cnt += 1
while m % 3 == 0:
	m //= 3
	cnt += 1
if m == 1 and good:
	print(cnt)
else:
	print(-1)


(n, m) = list(map(int, input().strip().split()))
if m % n != 0:
	print(-1)
else:
	rem = int(m / n)
	count = 0
	while rem % 2 == 0:
		rem = int(rem / 2)
		count += 1
	while rem % 3 == 0:
		rem = int(rem / 3)
		count += 1
	if rem != 1:
		print(-1)
	else:
		print(count)


(a, b) = map(int, input().split())

def au(a, b, count, l):
	if a == b:
		l.append(count)
		return
	elif a < b:
		au(a * 2, b, count + 1, l)
		au(a * 3, b, count + 1, l)
count = 0
l = []
au(a, b, 0, l)
if len(l) == 0:
	print(-1)
else:
	print(l[0])


from sys import stdin
import math

def primeFactors(n):
	d = {}
	while n % 2 == 0:
		if 2 in d:
			d[2] += 1
		else:
			d[2] = 1
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			n = n // i
	if n > 2:
		d[n] = 1
	return d
(n, m) = map(int, stdin.readline().split())
if n == m:
	print(0)
elif n > m:
	print(-1)
elif m % n == 0:
	k = m // n
	d = primeFactors(k)
	arr = list(d.keys())
	if len(arr) <= 2 and len(list(filter(lambda x: x > 3, arr))) == 0:
		ans = 0
		if 2 in d:
			ans += d[2]
		if 3 in d:
			ans += d[3]
		print(ans)
	else:
		print(-1)
else:
	print(-1)


import math

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n /= 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n /= i
	if n > 2:
		l.append(n)
	return l
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit()
elif m // n == 1:
	print(0)
	exit()
else:
	l = primeFactors(m // n)
	for i in l:
		if i != 2 and i != 3:
			print(-1)
			exit()
	print(l.count(2) + l.count(3))


li = input().split()
m = int(li[1]) / int(li[0])
n = int(li[0])
cnt = int(0)
while m > 1:
	if m % 2 == 0:
		m /= 2
	elif m % 3 == 0:
		m /= 3
	else:
		break
	cnt = cnt + 1
if m != 1:
	print(-1)
else:
	print(cnt)


(a, b) = map(int, input().split())

def rec(n, m, sc):
	if n < m:
		rec(n * 2, m, sc + 1)
		rec(n * 3, m, sc + 1)
	if n == m:
		print(sc)
		exit(0)
rec(a, b, 0)
if 1:
	print('-1')


(n2, n1) = (int(_) for _ in input().split())
if n1 % n2 == 0:
	n = n1 // n2
	k = 0
	f = True
	while n != 1:
		if n % 3 == 0:
			n //= 3
			k += 1
		elif n % 2 == 0:
			n //= 2
			k += 1
		else:
			print(-1)
			f = False
			break
	if f:
		print(k)
else:
	print(-1)


from collections import *
(a, b) = list(map(int, input().split()))
(ct2, ct3) = (0, 0)
while a % 2 == 0:
	ct2 += 1
	a //= 2
while a % 3 == 0:
	ct3 += 1
	a //= 3
(CT2, CT3) = (0, 0)
while b % 2 == 0:
	CT2 += 1
	b //= 2
while b % 3 == 0:
	CT3 += 1
	b //= 3
if a != b or ct2 > CT2 or ct3 > CT3:
	print(-1)
else:
	print(CT2 + CT3 - ct2 - ct3)


(n, m) = map(int, input().split())
z = m // n
a = 0
if m % n != 0:
	print(-1)
else:
	while z != 1 and z % 2 == 0:
		z = z // 2
		a = a + 1
	while z != 1 and z % 3 == 0:
		z = z // 3
		a = a + 1
	if z != 1:
		print(-1)
	else:
		print(a)


(a, b) = map(int, input().split())
if b % a != 0:
	print(-1)
else:
	x1 = 0
	y1 = 0
	x = b // a
	while x % 2 == 0:
		x1 += 1
		x = x // 2
	while x % 3 == 0:
		y1 += 1
		x = x // 3
	if x == 1:
		print(x1 + y1)
	else:
		print(-1)


l = list(map(int, input().split()))
m = l[0]
n = l[1]
s = n / m
s2 = 0
if s * 10 == int(s) * 10:
	if s % 3 % 2 == 0 or s % 3 % 2 == 1:
		while s != 1:
			if s % 3 == 0:
				s = s // 3
				s2 = s2 + 1
				continue
			elif s % 2 == 0:
				s = s // 2
				s2 = s2 + 1
				continue
			s2 = -1
			break
		print(s2)
	else:
		print(-1)
else:
	print(-1)


num = input()
x = []

def get_the_data(count, num, arr):
	for i in range(0, len(num)):
		if num[i] == ' ':
			arr.append(int(num[count:i]))
			count = i + 1
		elif i == len(num) - 1:
			arr.append(int(num[count:len(num)]))
	return arr
arr = get_the_data(0, num, x)
a = arr[1]
b = arr[0]
count = 0
hell = False
while arr[1] != arr[0]:
	x = arr[0] * 2
	y = arr[0] * 3
	if arr[1] % y == 0:
		arr[0] = y
		count += 1
	elif arr[1] % x == 0:
		arr[0] = x
		count += 1
	else:
		hell = True
		break
if a == b:
	print(0)
elif count > 0 and hell == False:
	print(count)
else:
	print(-1)


entrada = []
entrada = input().split()
n = int(entrada[0])
m = int(entrada[1])
dois = 0
tres = 0
if m % n == 0:
	resto = m / n
	while resto % 2 == 0:
		dois = dois + 1
		resto = resto / 2
	while resto % 3 == 0:
		tres = tres + 1
		resto = resto / 3
	if resto == 1:
		print(dois + tres)
	else:
		print(-1)
else:
	print(-1)


numbers = input().split()
minNumber = int(numbers[0])
maxNumber = int(numbers[1])
counter = 0
if maxNumber % minNumber != 0:
	print(-1)
else:
	number = maxNumber // minNumber
	while number != 1:
		if number % 2 == 0 or number % 3 == 0:
			if number % 3 == 0:
				number = number // 3
				counter += 1
			else:
				number = number // 2
				counter += 1
		else:
			counter = -1
			break
	print(counter)


a = [int(s) for s in input().split()]
n = a[0]
m = a[1]
coun = 0
if m % n == 0:
	m //= n
	while m % 3 == 0:
		coun += 1
		m //= 3
	while m % 2 == 0:
		coun += 1
		m //= 2
	if m > 1:
		print(-1)
	else:
		print(coun)
else:
	print(-1)


(n, m) = tuple(map(int, input().split()))
if m % n != 0:
	print(-1)
else:
	d = m // n
	cnt = 0
	while d % 2 == 0:
		cnt += 1
		d //= 2
	while d % 3 == 0:
		cnt += 1
		d //= 3
	if d != 1:
		print(-1)
	else:
		print(cnt)


(n, m) = map(int, input().split())
cont = 0
cont1 = 0
while True:
	if cont1 == 1:
		break
	cont2 = 0
	while True:
		v = n * 2 ** cont * 3 ** cont2
		if v == m:
			print(cont2 + cont)
			cont1 = 1
			break
		elif v > m:
			break
		cont2 += 1
	if n * 2 ** cont > m:
		print(-1)
		break
	cont += 1


import math
import os
import random
import re
import sys
a = list(map(int, input().rstrip().split()))
n = a[0]
m = a[1]
if m % n != 0:
	print('-1')
else:
	k = 0
	p = 0
	d = m / n
	while d > 0:
		if d == 1:
			break
		if d % 2 == 0:
			d /= 2
			p = p + 1
		elif d % 3 == 0:
			d /= 3
			p = p + 1
		else:
			k = 1
			print('-1')
			break
	if k == 0:
		print(p)


def main():
	(n, m) = map(int, input().split())
	if m % n != 0:
		print(-1)
		return
	(k, ret) = (m // n, 0)
	while k > 1:
		if k % 2 != 0 and k % 3 != 0:
			print(-1)
			return
		if k % 2 == 0:
			k //= 2
		else:
			k //= 3
		ret += 1
	print(ret)
main()


(n, m) = map(int, input().split())
div = m / n
mod = m % n
if 1 == div:
	print(0)
	exit()
elif mod != 0 or (div % 2 != 0 and div % 3 != 0):
	print(-1)
	exit()
cnt = 0
while div != 1:
	if div % 2 == 0:
		div /= 2
		cnt += 1
	elif div % 3 == 0:
		div /= 3
		cnt += 1
	else:
		print(-1)
		break
else:
	print(cnt)


x = list(map(int, input().split()))
(n, m) = (x[0], x[1])
s = m // n

def f(x):
	if x == 1:
		return 0
	elif x % 2 == 0:
		return f(x // 2) + 1
	elif x % 3 == 0:
		return f(x // 3) + 1
	else:
		return -1000000
if s * n == m:
	if f(s) < 0:
		print(-1)
	else:
		print(f(s))
else:
	print(-1)


a = list(map(int, input().split()))
(s, d) = (a[0], a[1])
if s == d:
	print('0')
elif s > d or d / s % 1 != 0:
	print('-1')
else:
	r = d / s
	r //= 1
	v = 0
	m = 0
	while m == 0:
		if r % 2 == 0:
			r //= 2
			v += 1
		elif r % 3 == 0:
			r //= 3
			v += 1
		elif r == 1:
			print(v)
			m = 1
		else:
			m = 1
			print('-1')
			break


(n, m) = map(int, input().split())
countm2 = 0
countm3 = 0
countn2 = 0
countn3 = 0
while m % 2 == 0:
	countm2 += 1
	m //= 2
while m % 3 == 0:
	countm3 += 1
	m //= 3
while n % 2 == 0:
	countn2 += 1
	n //= 2
while n % 3 == 0:
	countn3 += 1
	n //= 3
result = countm3 - countn3 + countm2 - countn2
print(result if n == m and countm3 - countn3 >= 0 and (countm2 - countn2 >= 0) else -1)


def solve(div):
	if div != int(div):
		return -1
	count = 0
	while div % 2 == 0:
		div /= 2
		count += 1
	while div % 3 == 0:
		div /= 3
		count += 1
	if div != 1:
		return -1
	return count
(a, b) = (int(i) for i in input().split(' '))
div = b / a
print(solve(div))


import sys
(n, m) = tuple(map(int, input().split(' ')))
steps = 0
if m % n == 0:
	number = m // n
	while number > 1:
		if number % 3 == 0:
			steps = steps + 1
			number = number // 3
		elif number % 2 == 0:
			steps = steps + 1
			number = number // 2
		else:
			print(-1)
			sys.exit(0)
	print(steps)
else:
	print(-1)


(a, b) = [int(i) for i in input().split()]
num = 0
c = b / a
while c % 2 == 0:
	num += 1
	c /= 2
while c >= 3 and c % 1 == 0:
	num += 1
	c /= 3
print([-1, num][c == 1])


(n, m) = [int(x) for x in input().split()]
if m % n == 0:
	a = m // n
	counter = 0
	while a > 1:
		if a % 2 == 0:
			a //= 2
			counter += 1
		elif a % 3 == 0:
			a //= 3
			counter += 1
		else:
			print(-1)
			exit()
	print(counter)
else:
	print(-1)


(n, m) = map(int, input().split())
d = m / n
moves = 0
while d / 3 == d // 3 and d >= 1:
	d /= 3
	moves += 1
while d / 2 == d // 2 and d >= 1:
	d /= 2
	moves += 1
print(('-1', moves)[d == 1])


import sys
input = sys.stdin.readline

def inlt():
	return list(map(int, input().split()))
(sghir, kbir) = inlt()
resultat = -1
quesma = kbir / sghir
if kbir % sghir == 0:
	resultat = 0
	while 1:
		if quesma % 2 != 0:
			break
		else:
			resultat += 1
			quesma = quesma / 2
	while 1:
		if quesma % 3 != 0:
			break
		else:
			resultat += 1
			quesma = quesma / 3
	if quesma != 1:
		resultat = -1
print(resultat)


(n, m) = [int(p) for p in input().split()]
if n > m:
	temp = n
	n = m
	m = temp
if m % n != 0:
	print(-1)
else:
	c = m // n
	count = 0
	while c != 1:
		if c % 3 == 0:
			c = c // 3
			count += 1
		elif c % 2 == 0:
			c = c // 2
			count += 1
		else:
			break
	if c == 1:
		print(count)
	else:
		print(-1)


a = list(map(int, input().split(' ')))
(n, m) = (a[0], a[1])
k = m / n
if k - m // n != 0:
	print(-1)
elif k == 1:
	print(0)
elif k % 2 != 0 and k % 3 != 0:
	print(-1)
else:
	co = 0
	flag = 1
	for i in range(1000):
		if flag:
			if k % 2 == 0:
				k /= 2
				co += 1
				flag = 1
			elif k % 3 == 0:
				k /= 3
				co += 1
				flag = 1
			else:
				flag = 0
			if k <= 1:
				break
		else:
			break
	if k > 1:
		print(-1)
	else:
		print(co)


line = input()
n = int(line.split(' ')[0])
m = int(line.split(' ')[1])
if m % n != 0:
	print(-1)
else:
	r = 0
	d = m // n
	while d > 1 and d % 2 == 0:
		d = d // 2
		r += 1
	while d > 1 and d % 3 == 0:
		d = d // 3
		r += 1
	if d > 1:
		print(-1)
	else:
		print(r)


(a, b) = map(int, input().split())
c = b % a
d = 0
err = False
if c > 0:
	print(-1)
else:
	k = b // a
	while k > 1:
		if k % 2 == 0:
			d += 1
			k //= 2
		elif k % 3 == 0:
			d += 1
			k //= 3
		else:
			print(-1)
			err = True
			break
	if not err:
		print(d)


(a, b) = map(int, input().split())
cont1 = 0
flag = 0
if a == b:
	print(0)
else:
	while True:
		if flag == 1:
			break
		cont2 = 0
		while True:
			valor = a * 2 ** cont1 * 3 ** cont2
			if valor == b:
				print(cont2 + cont1)
				flag = 1
				break
			elif valor > b:
				break
			cont2 += 1
		if a * 2 ** cont1 > b:
			print(-1)
			break
		cont1 += 1


(a, b) = map(int, input().split(' '))
if b == a:
	print(0)
elif b % a:
	print(-1)
else:
	c = b // a
	i = 0
	while not c % 2:
		i += 1
		c //= 2
	while not c % 3:
		i += 1
		c //= 3
	if c == 1:
		print(i)
	else:
		print(-1)


import math
from collections import defaultdict
ml = lambda : map(int, input().split())
ll = lambda : list(map(int, input().split()))
ii = lambda : int(input())
ip = lambda : input()
(n, m) = ml()
ans = 0
a = 0
b = 0
c = 0
d = 0
p = m
q = n
s1 = set()
s2 = set()
for i in range(2, int(math.sqrt(n)) + 1):
	if n % i == 0:
		s1.add(i)
		if i == 2 or i == 3:
			s1.remove(i)
		while n % i == 0:
			if i == 2:
				a += 1
			if i == 3:
				b += 1
			n //= i
if n >= 2:
	if n != 2 and n != 3:
		s1.add(n)
	elif n == 2:
		a += 1
	elif n == 3:
		b += 1
for i in range(2, int(math.sqrt(m)) + 1):
	if m % i == 0:
		s2.add(i)
		if i == 2 or i == 3:
			s2.remove(i)
		while m % i == 0:
			if i == 2:
				c += 1
			if i == 3:
				d += 1
			m //= i
if m >= 2:
	if m != 2 and m != 3:
		s2.add(m)
	elif m == 2:
		c += 1
	elif m == 3:
		d += 1
if s1 == s2 and c >= a and (d >= b) and (p % q == 0):
	print(c - a + (d - b))
else:
	print(-1)


raw = list(map(int, input().split()))
x = raw[0]
y = raw[1]
n = y / x
count = 0
if n == 1:
	print('0')
elif n % 2 != 0 and n % 3 != 0:
	print('-1')
else:
	while n % 2 == 0 or n % 3 == 0:
		count += 1
		if n % 2 == 0:
			n = n // 2
		else:
			n = n // 3
	if n == 1:
		print(count)
	else:
		print('-1')


import sys
(m, n) = input().split()
n = int(n)
m = int(m)
if n == m:
	print(0)
	sys.exit()
if n % m == 0:
	n = n / m
	m = n
	co = 0
	while m % 2 == 0:
		m = m / 2
		co += 1
	while m % 3 == 0:
		m = m / 3
		co += 1
	if m == 1:
		print(co)
	else:
		print(-1)
else:
	print(-1)


n1 = list(map(int, input().split()))
n = n1[0]
m = n1[1]
if m % n != 0:
	print(-1)
else:
	a = m // n
	counter_2 = 0
	counter_3 = 0
	while a % 2 == 0:
		a = a // 2
		counter_2 += 1
	while a % 3 == 0:
		a = a // 3
		counter_3 += 1
	if a != 1:
		print(-1)
	else:
		print(counter_2 + counter_3)


l1 = [int(x) for x in input().split()]
smol = min(l1)
big = max(l1)
targ = big / smol
if targ != int(targ):
	print(-1)
else:
	targ = int(targ)
	ans = 0
	while not targ % 2:
		targ /= 2
		targ = int(targ)
		ans += 1
	while not targ % 3:
		targ /= 3
		targ = int(targ)
		ans += 1
	if targ != 1:
		print(-1)
	else:
		print(ans)


(n, m) = [int(x) for x in input().split()]
if m % n != 0:
	print(-1)
else:
	answer = 0
	qu = m // n
	while qu % 2 == 0:
		qu = qu // 2
		answer += 1
	while qu % 3 == 0:
		qu = qu // 3
		answer += 1
	if qu == 1:
		print(str(answer))
	else:
		print(-1)


numbers = input().split()
numbers[0] = int(numbers[0])
numbers[1] = int(numbers[1])
two_three_powers = numbers[1] / numbers[0]
(power_two, power_three) = (0, 0)
valid = True
while two_three_powers > 1 and valid:
	if two_three_powers % 2 == 0:
		power_two += 1
		two_three_powers /= 2
	elif two_three_powers % 3 == 0:
		power_three += 1
		two_three_powers /= 3
	else:
		valid = False
if valid:
	print(power_two + power_three)
else:
	print('-1')


from math import *

def prmfct(n):
	li = []
	while n % 2 == 0:
		li.append(2)
		n = n / 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			li.append(int(i))
			n = int(n / i)
	if n > 2:
		li.append(int(n))
	return li
(n, m) = input().split()
(n, m) = (int(n), int(m))
if m % n != 0:
	print(-1)
else:
	lim = prmfct(int(m / n))
	li = list(set(lim))
	if len(li) == 2 and li[0] == 2 and (li[1] == 3):
		print(len(lim))
	elif len(li) == 1 and (li[0] == 2 or li[0] == 3):
		print(len(lim))
	elif len(li) == 0:
		print(0)
	else:
		print(-1)


def f(n, m, k):
	if n == m:
		print(k)
		return 1
	elif n > m:
		return 0
	else:
		if m / n % 2 == 0:
			found = f(n * 2, m, k + 1)
			if found:
				return 1
		if m / n % 3 == 0:
			found = f(n * 3, m, k + 1)
			if found:
				return 1
		return 0
s = input()
s = s.split()
n = int(s[0])
m = int(s[1])
if m % n:
	print(-1)
else:
	found = f(1, m / n, 0)
	if not found:
		print(-1)


(n, m) = map(int, input().split())
x = m / n
step = 0
if x == 1:
	step = 0
while x % 2 == 0:
	step += 1
	x = x / 2
while x % 3 == 0:
	step += 1
	x = x / 3
if x != 1:
	step = -1
print(step)


nm = list(map(int, input().split()))
step = 0
if nm[1] / nm[0] % 2 == 0 or nm[1] / nm[0] % 3 == 0 or nm[1] == nm[0]:
	ost = nm[1] // nm[0]
	while ost % 3 == 0 and ost != 0:
		ost = ost // 3
		step += 1
	while ost % 2 == 0 and ost != 0:
		ost = ost // 2
		step += 1
	if ost > 1:
		print(-1)
	else:
		print(step)
else:
	print(-1)


from collections import deque
(n, m) = map(int, input().split())
numbers = set()
queue = deque()
queue.append((n, 0))
while queue:
	(num, moves) = queue.popleft()
	if num == m:
		print(moves)
		break
	for item in [num * 2, num * 3]:
		if item not in numbers and item <= m:
			queue.append((item, moves + 1))
			numbers.add(item)
else:
	print(-1)


import math
(a, b) = map(int, input().split())
if b % a == 0:
	c = b / a
	k = 0
	while c % 2 == 0:
		c /= 2
		k += 1
	while c % 3 == 0:
		c /= 3
		k += 1
	if c == 1:
		print(k)
	else:
		print(-1)
else:
	print(-1)


(n, m) = tuple(map(int, input().split()))
res = -1
r = 0
if m % n == 0:
	res = 0
	r = m // n
	while r > 1 and r % 2 == 0:
		r //= 2
		res += 1
	while r > 1 and r % 3 == 0:
		r //= 3
		res += 1
print(res if r == 1 else -1)


(n, m) = [int(x) for x in input().split()]
if m % n != 0:
	print(-1)
else:
	div = m / n
	count_2 = 0
	while div % 2 == 0:
		div /= 2
		count_2 += 1
	count_3 = 0
	while div % 3 == 0:
		div /= 3
		count_3 += 1
	if div == 1:
		print(count_2 + count_3)
	else:
		print(-1)


i = input()
num = i.split()
cnt2 = 0
cnt3 = 0
i1 = int(num[0])
i2 = int(num[1])
if i2 / i1 % 1 != 0:
	print('-1')
else:
	k = i2 // i1
	while k % 2 == 0:
		k = k // 2
		cnt2 = cnt2 + 1
	while k % 3 == 0:
		k = k // 3
		cnt3 = cnt3 + 1
	if k != 1:
		print('-1')
	else:
		print(cnt2 + cnt3)


(N, M) = input().split(' ')
(N, M) = (int(N), int(M))
if M == N:
	print(0)
elif M < N or M % N != 0:
	print(-1)
elif M % N == 0:
	base = M / N
	number_of_2 = 0
	number_of_3 = 0
	while base > 1 and base % 2 == 0:
		base = int(base / 2)
		number_of_2 += 1
	while base > 1 and base % 3 == 0:
		base = int(base / 3)
		number_of_3 += 1
	if base == 1:
		print(number_of_2 + number_of_3)
	else:
		print(-1)
else:
	print(-1)


(a, b) = map(int, input().split())
c = 0
for i in range(0, b):
	if b < a:
		c = -1
		break
	elif b == a:
		break
	if b % 3 == 0 and b / 3 % a == 0:
		b /= 3
		c += 1
	elif b % 2 == 0 and b / 2 % a == 0:
		b /= 2
		c += 1
	else:
		c = -1
		break
if c == 0 and a == b:
	print(0)
else:
	print(c)


(n, m) = map(int, input().split(' '))
(n2, n3, m2, m3) = [0] * 4
while n % 2 == 0:
	n //= 2
	n2 += 1
while n % 3 == 0:
	n //= 3
	n3 += 1
while m % 2 == 0:
	m //= 2
	m2 += 1
while m % 3 == 0:
	m //= 3
	m3 += 1
if n == m and m2 >= n2 and (m3 >= n3):
	print(m2 + m3 - n2 - n3)
else:
	print(-1)


(x, y) = map(int, input().split())
s = 0
if y / x == 1:
	print(0)
elif int(y / x) != y / x:
	print(-1)
else:
	d = y / x
	while d != 1:
		if int(d / 2) != d / 2 and int(d / 3) != d / 3:
			print(-1)
			exit()
		for i in [2, 3]:
			if int(d / i) != d / i:
				d = d
			else:
				d = d / i
				s = s + 1
	print(s)


def game23(n, m):
	k = m / n
	count = 0
	if k != int(k):
		return -1
	else:
		while k % 2 == 0:
			k /= 2
			count += 1
		while k % 3 == 0:
			k /= 3
			count += 1
	if k == 1:
		return count
	else:
		return -1
(n, m) = map(int, input().strip().split(' '))
print(game23(n, m))


def mp():
	return map(int, input().split())
(n, m) = mp()
if m % n != 0:
	print(-1)
else:
	c = 0
	k = m // n
	while k % 2 == 0:
		k //= 2
		c += 1
	while k % 3 == 0:
		k //= 3
		c += 1
	if k == 1:
		print(c)
	else:
		print(-1)


import math
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	x = m // n
	count = 0
	while x % 2 == 0:
		count += 1
		x //= 2
	while x % 3 == 0:
		count += 1
		x //= 3
	if x != 1:
		print(-1)
	else:
		print(count)


(n, m) = map(int, input().split())
nways = 0
if m % n == 0:
	mult = m // n
	tmult = mult
	two = 0
	three = 0
	while mult % 2 == 0:
		two += 1
		mult = mult // 2
	while mult % 3 == 0:
		three += 1
		mult = mult // 3
	if 2 ** two * 3 ** three == tmult:
		print(two + three)
	else:
		print(-1)
else:
	print(-1)


import sys
import math
input = sys.stdin.readline

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

def str_array():
	return input().strip().split()
(n, m) = int_array()
if m % n != 0:
	print(-1)
else:
	the_no = m / n
	not_the_no = the_no
	factors = []
	state = 1
	while not_the_no != 1:
		if not_the_no % 2 == 0:
			not_the_no = not_the_no / 2
			factors.append(2)
		elif not_the_no % 3 == 0:
			not_the_no = not_the_no / 3
			factors.append(3)
		else:
			state = 0
			print(-1)
			break
	if state == 1:
		print(len(factors))


def f(x):
	if x == 1:
		return 0
	else:
		t = x
		cnt = 0
		while True:
			if t % 2 == 0:
				t = t // 2
				cnt += 1
				continue
			if t % 3 == 0:
				t = t // 3
				cnt += 1
				continue
			break
		if t == 1:
			return cnt
		else:
			return -1

def g(x, y):
	if y % x != 0:
		return -1
	else:
		return f(y // x)
(x, y) = map(int, input().split())
print(g(x, y))


(x, y) = [int(i) for i in input().split()]
a = 0
b = 0
if y % x == 0:
	y //= x
else:
	y /= x
while y > 1:
	if y % 2 == 0:
		y //= 2
	elif y % 3 == 0:
		y //= 3
	else:
		y = 0
	a += 1
if y < 1:
	print(-1)
else:
	print(a)


(n, m) = map(int, input().split())

def w(m, n, count):
	if m == n:
		return 1
	if m % 3 == 0:
		r = w(int(m / 3), n, count)
		if r > 0:
			return r + 1
	if m % 2 == 0:
		r = w(int(m / 2), n, count)
		if r > 0:
			return r + 1
	return 0
print(w(m, n, 0) - 1)


(n, m) = [int(x) for x in input().split()]
if n == m:
	print(0)
else:
	res = m / n
	c = 0
	while res != 1:
		if res % 3 == 0:
			res /= 3
			c += 1
		elif res % 2 == 0:
			res /= 2
			c += 1
		else:
			print(-1)
			exit(0)
	print(c)


from math import *
(n, m) = map(int, input().split())
x = m // n
if x * n != m:
	print(-1)
	exit(0)
k = 0
while x % 3 == 0:
	x //= 3
	k += 1
if x & x - 1 == 0:
	k += int(log2(x))
	print(k)
else:
	print(-1)


import math

def primeFactors(n):
	arr = []
	while n % 2 == 0:
		arr.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			arr.append(i)
			n = n // i
	if n > 2:
		arr.append(n)
	return arr
(n, m) = map(int, input().split())
arr1 = primeFactors(n)
arr2 = primeFactors(m)
if n == 1 and m == 1:
	print(0)
elif n == 1 and m > 1:
	arr = primeFactors(m)
	arr1 = []
	for i in range(0, len(arr)):
		if arr[i] != 2 and arr[i] != 3:
			arr1.append(arr[i])
	if len(arr1) == 0:
		print(len(arr))
	else:
		print('-1')
elif n <= m and m % n == 0:
	arr11 = []
	arr22 = []
	arr11.append([2, arr1.count(2)])
	arr11.append([3, arr1.count(3)])
	arr22.append([2, arr2.count(2)])
	arr22.append([3, arr2.count(3)])
	a1 = []
	a2 = []
	for i in range(0, len(arr1)):
		if arr1[i] > 3:
			a1.append(arr1[i])
	for i in range(0, len(arr2)):
		if arr2[i] > 3:
			a2.append(arr2[i])
	if arr11[0][1] > arr22[0][1] or arr11[1][1] > arr22[1][1] or a1 != a2:
		print('-1')
	else:
		ans = arr22[1][1] + arr22[0][1] - arr11[0][1] - arr11[1][1]
		print(ans)
else:
	print('-1')


(n, k) = map(int, input().split())
if k % n:
	print(-1)
else:
	x = k // n
	c = 0
	while x % 2 == 0:
		x //= 2
		c += 1
	while x % 3 == 0:
		x //= 3
		c += 1
	if x == 1:
		print(c)
	else:
		print(-1)


(n, m) = map(int, input().split())
if m % n:
	print(-1)
else:
	cnt1 = cnt2 = 0
	ss = m / n
	while ss % 2 == 0:
		cnt1 += 1
		ss /= 2
	while ss % 3 == 0:
		cnt2 += 1
		ss /= 3
	if ss != 1:
		print(-1)
	else:
		print(cnt1 + cnt2)


(a, b) = map(int, input().split())
target = b / a
cont = 0
if int(target) != target:
	print(-1)
	exit()
elif a == b:
	print(0)
	exit()
while target % 3 == 0:
	target //= 3
	cont += 1
while target % 2 == 0:
	target //= 2
	cont += 1
if target == 1:
	print(cont)
else:
	print(-1)


cnt = lambda s, x: s.count(x)
ii = lambda : int(input())
si = lambda : input()
f = lambda : map(int, input().split())
dgl = lambda : list(map(int, input()))
il = lambda : list(map(int, input().split()))
(n, m) = f()
r = m // n
c = 0
while r > 1:
	if r % 3 == 0:
		r //= 3
		c += 1
	elif r % 2 == 0:
		r //= 2
		c += 1
	else:
		print(-1)
		exit()
if m % n != 0:
	print(-1)
else:
	print(c)


(a, b) = input().split()
a = int(a)
b = int(b)
flag = 0
count = 0
d = b / a
if a == b:
	print(0)
elif d < 2:
	print(-1)
else:
	while d != 1:
		if d % 6 == 0:
			d = int(d / 6)
			count += 2
		elif d % 2 == 0:
			d = int(d / 2)
			count += 1
		elif d % 3 == 0:
			d = int(d / 3)
			count += 1
		else:
			flag = 1
			break
	if flag == 1:
		print(-1)
	else:
		print(count)


import math

def pf(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
(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
else:
	flag = 1
	c = m // n
	s = pf(c)
	if s == []:
		print(0)
	else:
		if max(s) == 2 or max(s) == 3:
			flag = 0
		if flag == 0:
			print(len(s))
		else:
			print(-1)


import math
(n, m) = map(int, input().split())
if n == m:
	print(0)
elif (m / n).is_integer() == True and (m % 2 == 0 or m % 3 == 0):

	def primeFactors(n):
		c = 0
		while n % 2 == 0:
			c = c + 1
			n = n // 2
		while n % 3 == 0:
			c = c + 1
			n = n // 3
		if n == 1:
			return c
		else:
			return -1
	print(primeFactors(m // n))
else:
	print(-1)


(n, m) = map(int, input().split())
p = m // n
if p != m / n:
	print('-1')
else:
	c3 = 0
	c2 = 0
	while not p % 3:
		c3 += 1
		p //= 3
	while not p % 2:
		c2 += 1
		p //= 2
	if p == 1:
		print(c3 + c2)
	else:
		print('-1')


(n, m) = map(int, input().split())
if m % n == 0:
	rest = m / n
	count = 0
	while rest % 2 == 0 and rest >= 1:
		count += 1
		rest = rest / 2
	while rest % 3 == 0 and rest >= 1:
		count += 1
		rest = rest / 3
	if rest == 1:
		print(count)
	else:
		print(-1)
else:
	print(-1)


(n, m) = list(map(int, input().split()))
j = 0
i = 0
r = 0
if n == m:
	print(0)
else:
	squares2 = [2 ** x for x in range(29)]
	squares3 = [3 ** x for x in range(19)]
	while m > squares3[i] and i < len(squares3) - 1:
		i += 1
	while True:
		if j == 29:
			print(-1)
			break
		r = squares3[i] * squares2[j] * n - m
		if r == 0:
			print(i + j)
			break
		elif r > 0:
			if i == 0:
				print(-1)
				break
			i -= 1
		else:
			j += 1


(n, m) = map(int, input().split())
if m % n != 0:
	print(-1)
	exit()
tans = int(m / n)
two = 0
three = 0
while tans > 1 and tans % 2 == 0:
	two += 1
	tans = int(tans / 2)
while tans > 1 and tans % 3 == 0:
	two += 1
	tans = int(tans / 3)
if tans > 1:
	print(-1)
	exit()
print(two + three)


x = input().split()
n = int(x[0])
m = int(x[1])
if m % n != 0:
	print('-1')
elif m // n == 1:
	print(0)
else:
	p = m // n
	active = True
	i = 0
	while active:
		if p % 2 == 0:
			i += 1
			p = p // 2
		elif p % 3 == 0:
			i += 1
			p = p // 3
		else:
			active = False
	if p != 1 or i == 0:
		print('-1')
	else:
		print(i)


(n, m) = map(int, input().split())
num = m // n
result = m % n
if result != 0:
	print(-1)
else:
	while num > 1:
		if num % 6 == 0:
			num //= 6
			result += 2
		else:
			break
	while num > 1:
		if num % 3 == 0:
			num //= 3
			result += 1
		else:
			break
	while num > 1:
		if num % 2 == 0:
			num //= 2
			result += 1
		else:
			break
	print(result if num == 1 else -1)


x = input().split(' ')
n = int(x[0])
m = int(x[1])
if n == m:
	rez = 0
elif m % n != 0:
	rez = -1
else:
	rez = 1
	number = m / n
	while number not in (1, 2, 3):
		if number % 3 == 0:
			number /= 3
			rez += 1
		elif number % 2 == 0:
			number /= 2
			rez += 1
		else:
			rez = -1
			break
print(rez)


(a, b) = map(int, input().split())
i = 0
if b % a == 0:
	b = b // a
	while b != 1:
		if b % 2 == 0:
			b = b // 2
			i = i + 1
		elif b % 3 == 0:
			b = b // 3
			i = i + 1
		elif b % 2 != 0 and b % 3 != 0 and (b > 1):
			b = 1
			i = -1
		elif b < 1:
			b = 1
			i = -1
	print(i)
else:
	print(-1)


(a, b) = input().split()
(a, b) = (int(a), int(b))
h = 0
if a == b:
	print(0)
elif b % a > 0 or (b // a % 3 != 0 and b // a % 2 != 0):
	print(-1)
else:
	k = b // a
	while k != 1:
		if k % 3 == 0:
			k = k // 3
			h += 1
		elif k % 2 == 0:
			k = k // 2
			h += 1
		else:
			h = -1
			break
	print(h)


import string
(n, m) = map(int, input().split())
f = 1
if m % n != 0:
	print('-1')
else:
	m = m // n
	ans = 0
	while m != 1:
		if m % 3 == 0:
			ans += 1
			m = m // 3
		elif m % 2 == 0:
			ans += 1
			m = m // 2
		else:
			f = 0
			break
	if f == 1:
		print(ans)
	else:
		print(-1)


(n, m) = map(int, input().split())
(r, d) = (0, m // n)
for x in [2, 3]:
	while d % x < 1:
		d //= x
		r += 1
print([r, -1][d > 1 or m % n > 0])


(n, m) = map(int, input().split())
k = m // n
if m % n != 0:
	print(-1)
	exit(0)
c2 = 0
while k % 2 == 0:
	k //= 2
	c2 += 1
c3 = 0
while k % 3 == 0:
	k //= 3
	c3 += 1
if k != 1:
	print(-1)
	exit(0)
print(c2 + c3)


(n, m) = map(int, input().split())
moves = 0
if m < n or m % n or n == 0:
	moves = -1
else:
	(tmp, moves) = (m // n, 0)
	while m > n and tmp % 2 == 0:
		tmp //= 2
		moves += 1
	while m > n and tmp % 3 == 0:
		tmp //= 3
		moves += 1
	if tmp != 1:
		moves = -1
print(moves)


(n, m) = map(int, input().split())
x = 0
if m == n:
	x = 0
elif m % n == 0 and (m % 2 == 0 or m % 3 == 0):
	q = m // n
	while q != 1:
		if q % 2 == 0:
			q = q // 2
			x += 1
		elif q % 3 == 0:
			q = q // 3
			x += 1
		else:
			x = -1
			break
else:
	x = -1
print(x)


(n, m) = list(map(int, input().split()))
if m % n != 0:
	print(-1)
else:
	num = m // n
	steps = 0
	while 1:
		if num % 2 == 0:
			num = num // 2
			steps = steps + 1
		else:
			break
	while 1:
		if num % 3 == 0:
			num = num // 3
			steps = steps + 1
		else:
			break
	if num == 1:
		print(steps)
	else:
		print(-1)


def inp():
	return map(int, input().split())
c = 0

def divide(x):
	global c
	if x == 1:
		return
	if x % 2 == 0:
		divide(x // 2)
	elif x % 3 == 0:
		divide(x // 3)
	else:
		exit(print(-1))
	c += 1
from math import *
(n, m) = inp()
if m % n:
	exit(print(-1))
divide(m // n)
print(c)


import sys

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

def print(*x, sep=' ', end='\n'):
	return sys.stdout.write(sep.join(map(str, x)) + end)

def read():
	return sys.stdin.read()

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

def out(*arg):
	if type(arg[0]) == list:
		for i in arg[0]:
			print(i)
	else:
		print(arg)

def r(x):
	if x % 1 > 0.5:
		return int(x) + 1
	return int(x)

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def main():
	(a, b) = get()
	if a > b:
		(a, b) = (b, a)
	if b % a != 0:
		print(-1)
	else:
		b = b // a
		ans = 0
		while b % 2 == 0:
			b = b // 2
			ans += 1
		while b % 3 == 0:
			b = b // 3
			ans += 1
		if b == 1:
			print(ans)
		else:
			print(-1)
main()


(n1, n2) = map(int, input().split())
multiplier = n2 / n1
total = 0
while multiplier != 1:
	if multiplier % 3 == 0:
		total += 1
		multiplier = multiplier / 3
	elif multiplier % 2 == 0:
		total += 1
		multiplier = multiplier / 2
	else:
		total = -1
		break
print(total)