K=int(input()) N=int(input()) l=N+1 P=[1]*l for i in range(2,l): if P[i]: for j in range(i+i,l,i): P[j]=0 P=[i for i in range(2,l) if P[i] and K<=i<=N] def HASH(x): if x<10: return x return HASH(sum(list(map(int,str(x))))) a=(0,0) for w in range(1,10): for l in range(len(P)-w+1): r=l+w-1 if len(set([HASH(i) for i in P[l:r+1]]))==w: a=max(a,(w,P[l])) print(a[1])