let contains num num_list =
  let str = string_of_int num
  and chr_list = List.map (fun x -> (string_of_int x).[0]) num_list in
  List.fold_left (fun x y -> x || (String.contains str y)) false chr_list

let unfold f init =
  let rec unfold_inner f init ret =
    match f init with
      Some (a, b) -> unfold_inner f b (a::ret)
    | None        -> ret
  in
  unfold_inner f init []

let make_list from to_ step =
  let accum x =
    if x > to_ then None else Some (x, x + step)
  in
  List.rev @@ unfold accum from

let solve n proh =
  let cand = make_list n (10*n) 1 in
  List.fold_left (fun x y -> if contains x proh then y else x) n cand

let read_list_of_int () =
  let rec read_list_of_int_inner lst =
    try let ans = (Scanf.scanf "%d " (fun x->x)) in
        read_list_of_int_inner (ans::lst)
    with End_of_file ->
      List.rev lst
  in
  read_list_of_int_inner []

let () =
  let n, k = Scanf.scanf "%d %d\n" (fun x y -> x,y) in
  let list = read_list_of_int () in
  let ans = solve n list in
  Printf.printf "%d\n" ans
;;
