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 : IntSet.t) (k : int -> int) (k' : int -> int) : int -> int = function
  | 0 -> k 0
  | n ->
      let m, n = n mod 10, n / 10 in
      if IntSet.mem m ds then
        solve ds (fun n -> k (10 * n + m)) (fun n -> k' (10 * n + IntSet.min_elt ds)) n
      else
        tabulate 10 (fun n -> n)
        |> List.filter (fun n -> IntSet.mem n ds && m < n)
        |> (function
            | [] ->
                let k n = k' (10 * n + IntSet.min_elt ds) in
                solve ds k k (n + 1)
            | m :: _ ->
                solve ds (fun n -> k' (10 * n + m)) (fun n -> k' (10 * n + IntSet.min_elt ds)) n) 

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 (fun n -> n) (fun n -> n) n)
