K = int(input()) N = int(input()) p = [0] * 2 + [1] * N for i in range(2, int(N ** 0.5) + 2): if p[i]: for j in range(2 * i, N + 1, i): p[j] = 0 P = [i for i in range(K, N + 1) if p[i]] p = len(P) for i in range(min(9, p), 0, -1): for j in range(p - i, -1, -1): x = [0] * 9 for k in range(j, j + i): x[P[k] % 9] += 1 if max(x) == 1: print(P[j]) exit()