K = int(raw_input()) N = int(raw_input()) def foldnum(n): if n == 0: return 0 if n % 9 == 0: return 9 return n % 9 def eratos(n): list = xrange(2, n + 1) ans = [] while len(list): ans.append(list[0]) list = filter(lambda x: x % list[0] > 0, list) return ans def eratos2(n): table = [True] * (n + 1) table[0] = table[1] = False a = 2 while a * a <= n: if table[a]: k = 2 while a * k <= n: table[a * k] = False k += 1 a += 1 ans = [] for i, v in enumerate(table): if v: ans.append(i) return ans prime_map = filter(lambda x: x >= K, eratos2(N)) L = len(prime_map) a = 0 b = 0 mx = 0 total = 0 check = [False] * 10 maxIndex = 0 while a < L: while b < L: v = foldnum(prime_map[b]) if check[v]: break b += 1 check[v] = True if mx <= b - a - 1: mx = b - a - 1 maxIndex = a v = foldnum(prime_map[a]) check[v] = False a += 1 print prime_map[maxIndex]