import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def main(): def hash(x): if x < 10: return x if x in memo: return memo[x] res = memo[x] = hash(sum(int(c) for c in str(x))) return res memo = {} k = II() n = II() prime = [0, 1] * 100005 prime[1] = 0 prime[2] = 1 for x in range(3, 200005): if x ** 2 > 200005: break if prime[x]: for y in range(x ** 2, 200005, x): prime[y] = 0 xx = [] hh = [] for x in range(k, n + 1): if prime[x]: h = hash(x) xx.append(x) hh.append(h) #print(xx) #print(hh) mx_len = 0 ans_x = 0 last_pos = [-1] * 10 l = 0 for r, h in enumerate(hh): if last_pos[h] >= l: length = r - last_pos[h] l = last_pos[h] + 1 else: length = r - l + 1 if length >= mx_len: mx_len = length ans_x = xx[r - length + 1] last_pos[h] = r #print(mx_len) print(ans_x) main()