# coding: utf-8 import array, bisect, collections, heapq, itertools, math, random, re, string, sys, time sys.setrecursionlimit(10 ** 7) INF = 10 ** 20 MOD = 10 ** 9 + 7 def II(): return int(input()) def ILI(): return list(map(int, input().split())) def IAI(LINE): return [ILI() for __ in range(LINE)] def IDI(): return {key: value for key, value in ILI()} def make_primes(num_from, num_to): primes = [] for i in range(num_from, num_to + 1): bool_prime = True if i == 1: continue elif i == 2: primes.append(2) continue for j in range(2, math.ceil(math.sqrt(i)) + 1): if i % j == 0: bool_prime = False break if bool_prime: primes.append(i) return primes def make_one_hash(num): num_list = list(map(int, list(str(num)))) while len(num_list) != 1: hash_sum = sum(num_list) num_list = list(map(int, list(str(hash_sum)))) return num_list[0] def solve(K, N): primes = make_primes(K, N) prime_hash = list(map(make_one_hash, primes)) left = 0 max_num = 1 ans = 0 for right in range(1, len(prime_hash)): while prime_hash[right] in prime_hash[left: right]: left += 1 if (right - left + 1) >= max_num: max_num = right - left + 1 ans = primes[left] return ans def main(): K = II() N = II() print(solve(K, N)) if __name__ == "__main__": main()