from collections import deque def naive(x,y,a): if x==y: return 0 todo=deque() todo.append((x,0)) seen=set([x]) while todo: v,res=todo.popleft() if v==y: return res if res>10: return 10**9 if v+1 not in seen: seen.add(v+1) todo.append((v+1,res+1)) if v>0 and v-1 not in seen: seen.add(v-1) todo.append((v-1,res+1)) if v*a not in seen: seen.add(v*a) todo.append((v*a,res+1)) if v//a not in seen: seen.add(v//a) todo.append((v//a,res+1)) from sys import stdin input=lambda :stdin.readline()[:-1] memo={} def calc(y,x,a,k): if y in memo: return memo[y] ans=abs(y-x) tmpL,tmpR=y,y+1 res=0 while tmpR//a<=x: tmpL*=a tmpR*=a res+=1 if tmpL<=x