import math import sys sys.setrecursionlimit(10**7) #競技プログラミング対整数問題のライブラリーです class integerlib(): def __init__(self): pass def primeset(self,N): #N以下の素数をsetで求める.エラトステネスの篩O(√Nlog(N)) lsx = [1]*(N+1) for i in range(2,int(-(-N**0.5//1))+1): if lsx[i] == 1: for j in range(i,N//i+1): lsx[j*i] = 0 setprime = set() for i in range(2,N+1): if lsx[i] == 1: setprime.add(i) return setprime def defprime(self,N):#素数かどうかの判定、エラトステネスの篩O(√Nlog(N)) return N in self.primeset(N) def gcd(self,ls):#最大公約数 ls = list(ls) ans = 0 for i in ls: ans = math.gcd(ans,i) return ans def lmc(self,ls):#最小公倍数 ls = list(ls) ans = self.gcd(ls) for i in ls: ans = self.lmcsub(ans,i) return ans def lmcsub(self,a,b): gcd = math.gcd(a,b) lmc = (a*b)//gcd return lmc def factorization(self,N):#素因数分解√N arr = [] temp = N for i in range(2, int(-(-N**0.5//1))+1): if temp%i==0: cnt=0 while temp%i==0: cnt+=1 temp //= i arr.append([i, cnt]) if temp!=1: arr.append([temp, 1]) if arr==[]: arr.append([N, 1]) return arr #[素因数、個数] def factorizationset(self,N):#素因数分解√N,含まれている素因数の種類 if N == 1: return set() ls = self.factorization(N) setf = set() for j in ls: setf.add(j[0]) return setf def divisorsnum(self,N):#約数の個数 ls = [] for i in self.factorization(N): ls.append(i[1]) d = 1 for i in ls: d *= i+1 return d def Eulerfunc(self,N):#オイラー関数正の整数Nが与えられる。1,2,…,Nのうち、Nと互いに素であるものの個数を求めよ。 ls = list(self.factorizationset(N)) ls2 = [N] for i in ls: ls2.append(ls2[-1]-ls2[-1]//i) return ls2[-1] def make_divisors(self,N):#約数列挙O(√N) lower_divisors , upper_divisors = [], [] i = 1 while i*i <= N: if N % i == 0: lower_divisors.append(i) if i != N // i: upper_divisors.append(N//i) i += 1 return lower_divisors + upper_divisors[::-1] def invmod(self,a,mod):#mod逆元 if a == 0: return 0 if a == 1: return 1 return (-self.invmod(mod % a, mod) * (mod // a)) % mod def cmbmod(self,n, r, mod):#nCr % mod inv = [0,1] for i in range(2, n + 1): inv.append((-inv[mod % i] * (mod // i)) % mod) cmd = 1 for i in range(1,min(r,n-r)+1): cmd = (cmd*(n-i+1)*inv[i])%mod return cmd def permmod(self,n, r, mod):#nPr % mod perm = 1 for i in range(n,r-1,-1): perm = (perm*i)%mod return perm def modPow(self,a,n,mod):#繰り返し二乗法 a**n % mod if n==0: return 1 if n==1: return a%mod if n % 2 == 1: return (a*self.modPow(a,n-1,mod)) % mod t = self.modPow(a,n//2,mod) return (t*t)%mod K = int(input()) N = int(input()) IT = integerlib() lsp = list(IT.primeset(N)) lsp.sort() ii = 0 for p in lsp: if p >= K: break ii += 1 lsp2 = lsp[ii:] def hash(k): if len(str(k)) == 1: return k s = list(str(k)) s = [int(i) for i in s] k2 = sum(s) return hash(k2) l = 0 r = 0 N2 = len(lsp2) seth = set() maxp = 0 maxl = 0 for i in range(N2): while r < N2 and (not (hash(lsp2[r]) in seth)): seth.add(hash(lsp2[r])) r += 1 if r >= N2: break if maxl <= len(seth): maxp = lsp2[i] maxl = len(seth) seth.remove(hash(lsp2[i])) print(maxp)