def seachPrimeNum(N): if N == 2: return [] max = int(N ** 0.5) seachList = [i for i in range(2, N + 1)] primeNum = [] while seachList[0] <= max: primeNum.append(seachList[0]) tmp = seachList[0] seachList = [i for i in seachList if i % tmp != 0] primeNum.extend(seachList) return primeNum from bisect import bisect_left K = int(input()) N = int(input()) PL = seachPrimeNum(N) k = bisect_left(PL, K) X = [-1] * 10 def f(x): while x >= 10: ret = 0 while x > 0: ret += x % 10 x //= 10 x = ret return x n = len(PL) i = n - 1 tmp2 = 0 tmp = n - 1 ans = 2 while i >= k: h = f(PL[i]) if X[h] != -1: if tmp >= X[h]: tmp = X[h] - 1 X[h] = i if tmp2 < tmp - i + 1: tmp2 = tmp - i + 1 ans = PL[i] i -= 1 print(ans)