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
needrestart = true
while(needrestart) do
    needrestart = false
    len = #digits
    for i = 1, len + 1 do
        val = digits[i]
        if(val == nil) then break end
        isok = (ds[val % 10] == nil)
        while(not isok)do
            needrestart = true
            for j = 1, i - 1 do digits[j] = 0 end
            val = val + 1
            isok = (ds[val % 10] == nil)
        end
        if(10 <= val) then
            needrestart = true
            for j = 1, i - 1 do digits[j] = 0 end
            val = val - 10
            if(i ~= len) then digits[i + 1] = digits[i + 1] + 1 else digits[i + 1] = 1 end
        end
        digits[i] = val
        if(needrestart) then break end
    end
end
len = #digits
for i = len , 1, -1 do
    io.write(digits[i])
end
io.write("\n")