def sieve(k, n): res = [] is_prime = [True] * (n + 1) for i in xrange(2, n + 1): if is_prime[i]: if i >= k: res.append(i) for j in xrange(i * i, n + 1, i): is_prime[j] = False return res def frank_hash(n): if n < 10: return n return frank_hash(sum(map(int, str(n)))) k = input() n = input() primes = sieve(k, n) hashes = map(frank_hash, primes) exists = [False] * 10 max_length = -1 ans = -1 l = 0 for r in xrange(len(primes)): while exists[hashes[r]]: exists[hashes[l]] = False l += 1 exists[hashes[r]] = True if max_length <= r - l + 1: max_length = r - l + 1 ans = primes[l] print ans