module IntSet = Set.Make (struct type t = int let compare = compare end)

let rec tabulate n f = 
  let rec loop acc m =
    if m < n then loop (f m :: acc) (m + 1)
    else List.rev acc in
  loop [] 0

let rec solve ds = function
  | 0 -> 0
  | n ->
      let m, n = n mod 10, n / 10 in
      tabulate 10 (fun n -> n)
      |> List.filter (fun n -> IntSet.mem n ds && m <= n)
      |> (function
          | [] -> 10 * solve ds (n + 1) + IntSet.min_elt ds
          | m :: _ -> 10 * solve ds n + m)


let () =
  let n, k = Scanf.scanf "%d %d\n" (fun n k -> n, k) in
  let ds =
    IntSet.diff
      (IntSet.of_list (tabulate 10 (fun k -> k)))
      (IntSet.of_list (tabulate k (fun _ -> Scanf.scanf "%d " (fun d -> d)))) in
  Printf.printf "%d\n" (solve ds n)
