from sys import stderr K = int(input()) N = int(input()) isprime = [True] * (N + 1) isprime[0] = isprime[1] = False for i in range(2, N + 1): if isprime[i]: for j in range(i * 2, N + 1, i): isprime[j] = False maxlen = len = 0 bkt = [-1] * 10 ans = 0 for i in range(K, N + 1): if not isprime[i]: continue h = i % 9 if bkt[h] != -1: for j in range(10): rem = bkt[h] if 0 <= bkt[j] <= rem: bkt[j] = -1 len -= 1 bkt[h] = i len += 1 if len >= maxlen: ans = min(v for v in bkt if v >= 0) maxlen = len print(ans)