import heapq from collections import deque import re def is_prime(n): i = 2 while i * i <=n: if n % i == 0: return False i += 1 return True def sp(n): v = str(n) while v: s = 0 for vv in v: s += int(vv) if s < 10: return int(s) v = str(s) K = int(input()) N = int(input()) if K == 1: K += 1 a = [] b = [] mx = [] box = deque() start = 0 end = 0 for i in range(K, N + 1): if is_prime(i): t = sp(i) a.append(i) b.append(t) while (start <= end < len(b)): if not (b[end] in box): box.append(b[end]) end += 1 else: heapq.heappush(mx, [-(len(box)), -(a[start])]) box.clear() start += 1 end = start heapq.heappush(mx, [-(len(box)), -(a[start])]) print(-(heapq.heappop(mx)[1]))