import numpy as np K = int(input()) N = int(input()) p_list = [] for i in range(2, N+1): flg = 0 for p in p_list: if np.sqrt(i) < p: break if i % p == 0: flg = 1 break if flg == 0: p_list.append(i) k_list = [p for p in p_list if p >= K] hash_list = list() for k in k_list: if k < 10: hash = k % 10 else: while k >= 10: hash = 0 while k >= 10: hash += k % 10 k //= 10 hash += k % 10 k = hash hash_list.append(hash) result = 0 max_size = 0 j = 0 for i in range(len(hash_list)): while hash_list[i] in hash_list[j:i]: j += 1 if max_size <= len(hash_list[j:i+1]): max_size = len(hash_list[j:i+1]) result = j print (k_list[result])