A, P = map(int, input().split())
lst = [A]
while True:
tmp = 0
for i in str(lst[-1]):
tmp += int(i) ** P
if tmp in lst:
break
lst.append(tmp)
print(lst.index(tmp))
A, P = map(int, input().split())
lst = [A]
while True:
tmp = 0
for i in str(lst[-1]):
tmp += int(i) ** P
if tmp in lst:
break
lst.append(tmp)
print(lst.index(tmp))