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 10 * f 1 + if f 0 = 0 then 0 else f 1 else if List.mem n ds then f (n + 1) else n
let rec g b a = function [] -> a | n :: ns -> g (b || f n > n) ((if b then f 0 else f n) :: a) ns
let rec k b a = function [] -> a | n :: ns -> let m = if b then f (n + 1) else n in k (m > 9) ((if m > 9 && ns <> [] then f 0 else m) :: a) ns
let ns = g false [] ns |> k false [] |> List.fold_left (fun a n -> 10 * a + n) 0 |> Printf.printf "%d\n"