from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline K = int(input()) N = int(input()) class pf(): def __init__(self,M=10**6): self.sieve = [-1]*(M+1) self.sieve[1] = 1 self.p = [False]*(M+1) for i in range(2,M+1): if self.sieve[i] == -1: self.p[i] = True for j in range(i,M+1,i): self.sieve[j] = i def factors(self,x): tmp = [] while self.sieve[x] != x: tmp.append(self.sieve[x]) x //= self.sieve[x] tmp.append(self.sieve[x]) return tmp def is_prime(self,x): return self.p[x] P = pf() X = [] for i in range(K,N+1): if P.is_prime(i): X.append(i) def hash(x): tx = x while len(str(tx))!=1: tx = sum(int(i) for i in list(str(tx))) return tx Y = [hash(x) for x in X] n = len(X) ans = -1 M = -1 delta = min(n,10) for d in range(1,delta+1): for i in range(n-d+1): if len(set(Y[i:i+d]))==d: M = X[i] print(M)