import java.util.Scanner

object Main extends App {
  val sc = new Scanner(System.in)
  val n, d = sc.nextInt
  val ds = List.fill(d)(sc.nextInt)

  val set = (0 to 9).toList.filterNot(ds.contains)

  def next(n: Int): Int = {
    val ints = n.toString.split("").map(_.toInt)
    val where = ints.indexWhere(ds.contains)
    if (where == -1) {
      n
    } else {
      val m = ints(where)
      set.find(_ > m) match {
        case Some(i) => {
          ints(where) = i
          next(ints.map(_.toString).mkString("").toInt)
        }
        case None => {
          ints(where) = 0
          ints(where - 1) += 1
          next(ints.map(_.toString).mkString("").toInt)
        }
      }
    }
  }
  println(next(n))
}
