#!/usr/bin/env python3 import sys input = sys.stdin.readline # O(nloglogn) def primes(k, 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(k, n + 1) if is_prime[i]] def hash(n): while n >= 10: t = 0 while n > 0: t += n % 10 n //= 10 n = t return n k = int(input()) n = int(input()) p = primes(k, n) d = [] for item in p: digits = sum([int(dgt) for dgt in str(item)]) d.append(hash(item)) max_length = 0 ans = 0 for i, item in enumerate(d): cnt = [0] * 10 cnt[item] += 1 for j in range(i + 1, len(d) + 1): if j == len(d) or cnt[d[j]] > 0: break else: cnt[d[j]] += 1 if j - i >= max_length: ans = i max_length = j - i print(p[ans])