n, k = io.read("*n", "*n")
ds = {}
for i = 1, k do ds[io.read("*n")] = true end
digits = {}
while(0 < n) do
    a = n % 10
    table.insert(digits, a)
    n = math.floor(n / 10)
end
len = #digits
for i = 1, len do
    val = digits[i]
    isok = (ds[val % 10] == nil)
    while(not isok)do
        val = val + 1
        isok = (ds[val % 10] == nil)
    end
    if(10 <= val) then
        val = val - 10
        if(i ~= len) then digits[i + 1] = digits[i + 1] + 1 else digits[i + 1] = 1 end
    end
    digits[i] = val
end
if(digits[len + 1] ~= nil) then
    val = digits[len + 1]
    isok = (ds[val] == nil)
    while(not isok) do
        val = val + 1
        isok = (ds[val] == nil)
    end
    digits[len + 1] = val
end
len = #digits
for i = len , 1, -1 do
    io.write(digits[i])
end
io.write("\n")