K = int(input()) N = int(input()) def calc(n): now = n while now >= 10: a = str(now) now = 0 for i in a: now += int(i) return now def era(n): """ n以下の素数をリストで小さい順に返す。 O(nlognlogn) """ s = [1 for _ in range(n+1)] for i in range(2,n+1): if s[i] == 0: continue j = 2 while j * i <= n: s[j*i] = 0 j += 1 l = [] for i in range(2,n+1): if s[i] == 1: l.append(i) return l l = era(N) now = 0 li = [] nn = 0 for i in range(len(l)): if l[i] < K: continue s = set() k = 0 for j in range(10): ni = i + j k = j if ni >= len(l): break a = calc(l[ni]) if a in s: break s.add(a) if now <= k: now = k nn = l[i] li = [] for j in range(now): li.append(l[i + j]) print(nn)