import io.{StdIn => in}

object Main extends App {
  val Array(sn, sk) = in.readLine.split(" ")
  val n = sn.toInt
  val k = sk.toInt
  val d = in.readLine.split(" ").map(_.toInt)
  val digits = Array.range(0, 11) diff d
  // println(digits.mkString(" "))
  val ans = Array.ofDim[Int](6)

  val res = sn.reverse./:(5)((idx,c) => {
    val i = c.toString.toInt + ans(idx)
    val isChange = d.exists(_ == i)
    val dn = if(isChange) digits.minBy(e => if(e > i) e else 11) else i
    // print(i + "," + dn + " ")
    if(isChange) {
      if(dn == 10) ans(idx-1) = (digits diff Array(0)).min
      ans(idx) = if(dn != 10) dn else 0
      if(idx != 6) ans(idx+1) = digits.min
    }
    else ans(idx) = i
    idx - 1
  })

  println(ans.mkString.toInt)
}