import scala.io.StdIn.readLine

object Main {
  def isPrime(n:Int):Boolean = (2 to Math.sqrt(n).toInt).forall(n%_ != 0)

  def scan(i:Int,s:Int,e:Int,len:Int,mid:Int):Int = {
    if(i < e) {
      if(mid == -1)
        (s-1)*s + (s-1).toString.reverse.toInt
      else
        (s-1)*s*10 + mid*s + (s-1).toString.reverse.toInt
    } else {
      val x = if(mid == -1) i*s + i.toString.reverse.toInt
              else  i*s*10 + mid*s + i.toString.reverse.toInt
      if(isPrime(x)) x
      else scan(i-1,s,e,len,mid)
    }
  }

  def main(args:Array[String]) = {
    val n = readLine.split(" ").map(_.toInt)
    val r = scan(Math.pow(10,n(0)).toInt-1,Math.pow(10,n(0)).toInt,Math.pow(10,n(0)-1).toInt,n(0),n(1))
    println(r)
  }
}