from math import sqrt, ceil def Sieve(n): lst = [True] * (n + 1) S = set() for i in range(2, ceil(sqrt(n)) + 1): if lst[i]: for j in range(2 * i, n + 1, i): lst[j] = False for i in range(2, n + 1): if lst[i]: S.add(i) return sorted(list(S)) K = int(input()) N = int(input()) S = Sieve(N + 5) from collections import * Q = deque() seen = [0] * 10 def f(x): while len(str(x)) != 1: v = 0 while x: v += x % 10 x //= 10 x = v return x ans = -1 maxv = -1 for s in S: if s < K or s > N: continue q = f(s) seen[q] += 1 Q.append(s) while seen[q] == 2: seen[f(Q.popleft())] -= 1 if len(Q) >= maxv: ans = Q[0] maxv = len(Q) print(ans)