from collections import defaultdict def era(n): is_prime = [True] * (n+1) is_prime[0] = False is_prime[1] = False for i in range(2, int(n**0.5)+1): if not is_prime[i]: continue for j in range(2*i, n+1, i): is_prime[j] = False return [i for i in range(K, n+1) if is_prime[i]] def calc(n): if len(str(n)) == 1: return n s = 0 for ni in str(n): s += int(ni) return calc(s) K = int(input()) N = int(input()) primes = era(N) hashed = [calc(p) for p in primes] r = 0 length = 0 d = defaultdict(int) for l in range(len(hashed)): while r < len(hashed) and d[hashed[r]] == 0: d[hashed[r]] += 1 r += 1 if length <= r-l: length = r-l ans = primes[l] if l == r: r += 1 else: d[hashed[l]] -= 1 print(ans)