https://www.acmicpc.net/problem/2331
import math
def make(a,p):
sum=0
while a>0:
sum+=int(math.pow(a%10,p))
a//=10
return sum
arr=[]
a,p=map(int,input().split())
arr.append(a)
while 1:
m=arr[len(arr)-1]
num=make(m,p)
if arr.__contains__(num):
ans=arr.index(num)
print(ans)
break
arr.append(num)