k = int(input()) n = int(input()) primes = [] l = [] for i in range(2,n+1): check = True for p in primes: if p**2 > i: break if i%p == 0: check = False break if check: primes.append(i) if k <= i <= n: l.append(i) def hash(x): now = x while True: now = sum([int(i) for i in str(now)]) if now < 10: return now count = [0]*10 now = 0 ans = [] hashl = [hash(i) for i in l] for i,h in enumerate(hashl): while now < len(l) and count[hashl[now]] == 0: count[hashl[now]] += 1 now += 1 ans.append(now-i) count[hashl[i]] -= 1 m = max(ans) for i in range(len(ans))[::-1]: if ans[i] == m: print(l[i]) exit()