def solution(x, y, n):
if x == y:
return 0
def f_t(x,n,id):
if id==0:
return x+n
elif id==1:
return x*2
else:
return x*3
s = set()
s.add(x)
s_r = s.copy()
m = 1
while True:
si = set()
for xi in s:
for fi in range(3):
yi = f_t(xi,n,fi)
if yi == y:
return m
elif yi < y:
si.add(yi)
if len(si) == 0:
return -1
else:
s = si - s_r
s_r.union(s)
m += 1