#!/usr/bin/env python3 from itertools import dropwhile def sqrt_floor(n): l = 5 * n r = 5 sqrt = 0 while l > r: sqrt += 1 l = l - r r += 10 return sqrt def sieve_of_eratosthenes(max): is_prime = [True] * (max + 1) is_prime[0] = False is_prime[1] = False for i in range(2, sqrt_floor(max) + 1): if not is_prime[i]: continue for j in range(i * i, max + 1, i): is_prime[j] = False return filter(lambda x: is_prime[x], range(max + 1)) def digital_root(n): if n == 0: return 0 if n % 9 == 0: return 9 return n % 9 def length(digitals, start_index): appeared = [] count = 0 for i in digitals[start_index:]: if i in appeared: break appeared.append(i) count += 1 return count K = int(input()) N = int(input()) primes = tuple(dropwhile(lambda x: x < K, sieve_of_eratosthenes(N))) digitals = tuple(map(digital_root, primes)) maxlength = 0 index = 0 for i in range(len(digitals)): tmp = length(digitals, i) if tmp >= maxlength: maxlength = tmp index = i print(primes[index])