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, false)
  | 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 * fst (solve ds (n + 1)) + IntSet.min_elt ds, true)
          | m :: _ ->
              match solve ds n with
              | (n, false) -> (10 * n + m, false)
              | (n, true) -> (10 * n + IntSet.min_elt ds, true))


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" (fst (solve ds n))
