from collections import deque def list_primes(limit): primes = [] is_prime = [True] * (limit + 1) is_prime[0] = False is_prime[1] = False for p in range(0, limit + 1): if not is_prime[p]: continue primes.append(p) for i in range(p * p, limit + 1, p): is_prime[i] = False return primes def digits_sum(num): res = str(num) while len(res) != 1: res = sum(int(x) for x in res) res = str(res) return res K = int(input()) N = int(input()) mx = 0 used = deque() ans = 2 for p in list_primes(N)[::-1]: if p < K: break x = digits_sum(p) if x in used: while x != used.popleft(): pass used.append(x) if len(used) > mx: mx = len(used) ans = p print(ans)