k = int(input()) n = int(input()) siv = [True]*(n+1) siv[0] = siv[1] = False for i in range(2, int(n**0.5)+2): if not siv[i]: continue for j in range(i*2, n+1, i): siv[j] = False prm = [i for i in range(k, n+1) if siv[i]] def hsh(x): x = sum(map(int, str(x))) if x >= 10: return hsh(x) return x vis = set() mx = res = beg = 0 end = -1 while end < len(prm)-1: h = hsh(prm[end+1]) if h in vis: vis.remove(hsh(prm[beg])) beg += 1 else: vis.add(h) end += 1 if len(vis) >= mx: mx = len(vis) res = prm[beg] print(res)