def mark(s, x): for i in range(x + x, len(s), x): s[i] = False def sieve(n): s = [True] * n for x in range(2, int(n ** 0.5) + 1): if s[x]: mark(s, x) return [i for i in range(n) if s[i] and i > 1] def dr(n): if n == 0: return 0 if n % 9 == 0: return 9 else: return n % 9 def solve(): K = int(input()) N = int(input()) hc = [0] * 10 tc = 0 ps = [p for p in sieve(N + 1) if p >= K] i = j = 0 ma = 0 mi = 0 while i < len(ps): if tc == 0 and j < len(ps): h = dr(ps[j]) hc[h] += 1 if hc[h] >= 2: tc += 1 j += 1 else: h = dr(ps[i]) hc[h] -= 1 if hc[h] == 1: tc -= 1 i += 1 if tc == 0 and j - i >= ma: ma = j - i mi = i print(ps[mi]) if __name__ == '__main__': solve()