def solution(x, y, n):
answer = 0
an = [x]
if x == y:
return 0
already = {} ; already2 = {i+1:[] for i in range(1000)}
while y not in an:
answer += 1
for i in an:
if i+n not in already and x<i+n<y:
already[i+n]=answer
already2[answer].append(i+n)
elif i+n==y:
return answer
if i*2 not in already and x<i*2<y:
already[i*2]=answer
already2[answer].append(i*2)
elif i*2==y:
return answer
if i*3 not in already and x<i*3<y:
already[i*3]=answer
already2[answer].append(i*3)
elif i*3==y:
return answer
a = already2[answer]
if len(a) == 0:
return -1
else:
an = a
return answer