fun main(arg: Array<String>) {
    val (N, K) =  listOfInt()
    val D = listOfInt()
    val OK = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).toMutableList()
    D.forEach { OK.remove(it) }
    val yen = "0$N".toCharArray().map { it - '0'}
    val pay = yen.toMutableList()
    fun getNum(n: Int): Pair<Boolean, Int> {
        if (OK.contains(n)) return true to n
        for (d in n + 1 .. 10)
            if (OK.contains(d)) return false to d
        for (d in 0 .. n - 1)
            if (OK.contains(d)) return false to 10 + d
        return false to -1
    }
    var n = 1
    while (n < yen.size) {
        val (f, y) = getNum(pay[n])
        if (f) pay[n] = y
        else {
            if (y < 10) {
                pay[n] = y
                for (m in n + 1 until yen.size) pay[m] = OK.first()
            } else {
                pay[n] = y % 10
                pay[n - 1] += y / 10
                for (m in n + 1 until yen.size) pay[m] = OK.first()
                n -= 2
            }
        }
        n += 1
    }
    if (pay.first() == 0) pay.removeAt(0)
    val ans = pay.joinToString("").toInt()
    println(ans)
}

fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map(String::toInt)
fun listOfLong() = listOfString().map(String::toLong)
fun listOfInt(delta: Int = 0) = listOfString().map { Integer.parseInt(it) + delta }

//10000
