from collections import deque def Sieve(n): #n以下の素数全列挙(O(nloglogn)) retは素数が入ってる。divlisはその数字の素因数が一つ入ってる ret = [] divlis = [-1] * (n+1) #何で割ったかのリスト(初期値は-1) flag = [True] * (n+1) flag[0] = False flag[1] = False ind = 2 while ind <= n: if flag[ind]: ret.append(ind) ind2 = ind ** 2 while ind2 <= n: flag[ind2] = False divlis[ind2] = ind ind2 += ind ind += 1 return ret,divlis def digsum(n): while n > 9: s = str(n) now = 0 for i in s: now += int(i) n = now return n K = int(input()) N = int(input()) rev,divs = Sieve(N) lis = [] for i in rev: if i >= K: lis.append(i) used = [False] * 10 useind = [None] * 10 ans = lis[0] maxlen = 0 q = deque([]) for i in range(len(lis)): ds = digsum(lis[i]) if not used[ds]: used[ds] = True useind[ds] = i q.append(lis[i]) else: while q[0] != lis[useind[ds]]: pop = q.popleft() used[digsum(pop)] = False q.popleft() useind[ds] = i q.append(lis[i]) if maxlen <= len(q): ans = q[0] maxlen = len(q) #print (q) print (ans)