import math from collections import defaultdict as ddict def sieve (n): check = [1]*(n+1) check[0] = check[1] = 0 for d in xrange(2,int(math.sqrt(n))+1): if check[d] == 0: continue for comp in xrange(d*2,n+1,d): check[comp] = 0 return check def convert(n): while n > 9: tmp = 0 while n > 0: tmp += n%10 n /= 10 n = tmp return n K = int(raw_input()) N = int(raw_input()) prime = sieve(N) l = [i for i in xrange(K,N+1) if prime[i]] ulhash = ddict(int) ans = l[0] maxlen = 1 ulhash[convert(l[0])] = lpos = 0 for rpos in xrange(1,len(l)): r = convert(l[rpos]) if ulhash[r] >= lpos: lpos = ulhash[r] + 1 ulhash[r] = rpos if maxlen <= rpos - lpos + 1: ans = l[lpos] maxlen = rpos - lpos + 1 print ans