def sp(p): v = str(p) while v: s = 0 for vv in v: s += int(vv) if s < 10: return int(s) v = str(s) K = int(input()) N = int(input()) primes = [] if K < 2: K = 2 for i in range(K, N + 1): for j in range(2, i): if i % j == 0: break else: primes.append(i) max = 0 max_len = 0 for i in range(len(primes)): col = [] for p in primes[i:]: x = sp(p) if x in col: break else: col.append(x) if max_len <= len(col): max_len = len(col) if max < primes[i]: max = primes[i] print(max)