from collections import defaultdict def SieveofEratosthenes(N): P = [0] * (N+1) P[0] = P[1] = -1 for i in range(2,N+1): if P[i] == 0: k = i while k <= N: P[k] = i k += i P[i] = 0 return P def hash(N): if N in dic: return dic[N] x = N S = 0 while x: S += x % 10 x //= 10 dic[N] = hash(S) return dic[N] K = int(input()) N = int(input()) P = SieveofEratosthenes(N) C = [] for i in range(K,N+1): if P[i] == 0: C.append(i) dic = {} for i in range(10): dic[i] = i M = len(C) F = [] for i in range(M): F.append(hash(C[i])) dic = defaultdict(int) Left = 0 Length = 0 ans = 0 for Right in range(M): dic[F[Right]] += 1 while dic[F[Right]] >= 2: dic[F[Left]] -= 1 Left += 1 if Right - Left + 1 >= Length: Length = Right - Left + 1 ans = C[Left] print(ans)