P, N = map(int, input().split()) import sys sys.setrecursionlimit(10**9) def f(x): if x==0 or x==6 or x==9: return 6 elif x==1: return 2 elif x==2 or x==3 or x==5: return 5 elif x==4 or x==7: return 4 elif x == 8: return 7 elif x >= 10: return f(x//10)+f(x%10) else: return 1 + f(-x) n = [list() for _ in range(10)] for i in range(10): x = i n[i].append(x) while 1: y = f(x) if y == x: break else: n[i].append(y) x = y for i in range(min(N, 5*10**6)): if len(str(P)) == 1: id = i break P = f(P) else: print(P) exit() left = N - id print(n[P][i] if N-id<=(len(n[P])-1) else n[P][-1])