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
    if n==1
        p 11
    else
        p 10**(2*n)-1
    end
else
    keta=2*n+1
    if n<4
        bdr=10**keta
    else
        bdr=2*10**(keta-1)
    end
    for prime in 10**(keta-1)...bdr
        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
    p res.max
end


