def primes2(limit): ''' returns a list of prime numbers upto limit. source: Rossetta code: Sieve of Eratosthenes http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Odds-only_version_of_the_array_sieve_above ''' if limit < 2: return [] if limit < 3: return [2] lmtbf = (limit - 3) // 2 buf = [True] * (lmtbf + 1) for i in range((int(limit ** 0.5) - 3) // 2 + 1): if buf[i]: p = i + i + 3 s = p * (i + 1) + i buf[s::p] = [False] * ((lmtbf - s) // p + 1) return [2] + [i + i + 3 for i, v in enumerate(buf) if v] def h(p): p, r = divmod(p, 10) cump = r while p >= 10: p, r = divmod(p, 10) cump += r cump += p if cump < 10: return cump else: return h(cump) def solve(K, N): hashed = [(h(p), p) for p in primes2(N) if p >= K] head = 0 tail = 0 freq = [0] * 10 freq[hashed[0][0]] = 1 record = 1 mark = hashed[0][1] goal = len(hashed) - 1 score = 1 while tail < goal: tail += 1 c, p = hashed[tail] freq[c] += 1 while freq[c] >= 2: cc, pp = hashed[head] freq[cc] -= 1 score -= 1 head += 1 score += 1 if score >= record: record = score mark = hashed[head][1] return mark K = int(input()) N = int(input()) print(solve(K, N))