let fs, ns, ans = Array.(make 10 true, make 5 0, ref max_int)
let n, _ = Scanf.(scanf " %d %d" @@ fun n k -> n, for _ = 1 to k do fs.(scanf " %d" (+) 0) <- false done)
let g s = if s >= n then ans := min !ans s
let rec f i z = if i >= 5 then g @@ Array.fold_left (fun a n -> 10 * a + n) 0 ns else for j = 0 to 9 do if fs.(j) || z && j = 0 then (ns.(i) <- j; f (i + 1) (z && j = 0)) done
let _ = f 0 true; Printf.printf "%d\n" !ans