K = int(input()) N = int(input()) dat = [0] * (N + 1) prime = [] for i in range(2,N+1): if dat[i] == 0: for j in range(i * 2,N+1,i): dat[j] = 1 if i >= K: prime.append(i) def hash(x): if x <= 9:return x a = 0 while x: a += x % 10 x //= 10 return hash(a) N = len(prime) right = 0 s = set() _max = 0 index = -1 for left in range(N): while right < N and hash(prime[right]) not in s: s.add(hash(prime[right])) right += 1 if right - left >= _max: _max = right - left index = prime[left] s.remove(hash(prime[left])) print(index)