def smallest_prime_factors(n): spf = [0]*(n+1) for i in range(n+1): spf[i] = i for i in range(2,n+1): if i*i>n: break if spf[i] == i: for j in range(i*i, n+1, i): if spf[j] == j: spf[j] = i return spf K = int(input()) N = int(input()) spf = smallest_prime_factors(N) hash = [] prime = [] for i in range(K,N+1): if spf[i]==i and i!=1: num = i prime.append(num) while len(str(num))>1: num = sum([int(j) for j in str(num)]) hash.append(num) l,r = 0,0 res = 0 ans = 0 s = set() while l=res: res = r-l ans = prime[l] s.remove(hash[l]) l += 1 print(ans)