let rec h n a = if n = 0 then a else h (n / 10) (n mod 10 :: a)
let ns, ds = Scanf.(scanf "%d %d" @@ fun n k -> h n [], Array.(to_list @@ init k @@ fun _ -> scanf " %d" (+) 0))
let rec f n = if n > 9 then n else if not @@ List.mem n ds then n else f (n + 1)
let rec g b a = function [] -> a | n :: ns -> g (b || f n > n) (10 * a + if b then f 0 else f n) ns
let _ = g false 0 ns |> Printf.printf "%d\n"