from functools import lru_cache def prime_ls(k, n): ls = [False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(n)] ls[0] = ls[1] = False ls[2] = ls[3] = ls[5] = True for i in range(3, int(n**0.5) + 1, 2): for j in range(i**2, n, i): ls[j] = False return [p for p, is_prime in enumerate(ls[k:], k) if is_prime] @lru_cache(maxsize=None) def hash(n): s = str(n) return hash(sum([int(c) for c in s])) if len(s) >= 2 else n k = int(input()) n = int(input()) p_ls = prime_ls(k, n) h_ls = [hash(p) for p in p_ls] l, r = 0, 0 max_len = 0 memo = [False] * 10 while r < len(h_ls): hl, hr = h_ls[l], h_ls[r] if memo[hr]: memo[hl] = False l += 1 else: memo[hr] = True r += 1 lr_len = r - l if lr_len >= max_len: max_len = lr_len ans = p_ls[l] print(ans)