K = int(input()) N = int(input()) def primes(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(i * 2, n + 1, i): is_prime[j] = False return (i for i in range(n + 1) if is_prime[i]) idx_lst = [None] * 10 P = tuple(filter(lambda x: x >= K, primes(N))) ans = P[0] l = 0 M = 0 for r, p in enumerate(P): while p >= 10: p = sum(int(x) for x in str(p)) if idx_lst[p] is not None: l = max(l, idx_lst[p] + 1) idx_lst[p] = r if M <= r - l: ans = P[l] M = r - l print(ans)