def ispal(s)
    n=s.size
    if n%2==0
        return s[0...n/2]==s[n/2...n].reverse
    else
        return s[0...n/2]==s[n/2+1...n].reverse
    end
end

require 'prime'
n,c=gets.split.map(&:to_i)
res=[]
if c<0
    keta=2*n
else
    keta=2*n+1
end

for prime in 10**(keta-1)...10**(keta)
    p_s=prime.to_s
    p_l=p_s.size
    if prime.prime? && ispal(p_s)
        if c>=0 && p_s[n]!=c.to_s then next end
        res.push(prime)
    end
end
if res.size>0 
    p res.max
else
    p 10**keta-1
end

