open Num;;
Scanf.scanf "%d %d" (fun n m ->
    let a = Array.init m (fun _ -> Scanf.scanf " %d" (fun a -> a)) in
    let v = [| 0; 2; 5; 5; 4; 5; 6; 3; 7; 6 |] in
    let dp = Array.make (n + 1) (Int (-1))  in
    dp.(0) <- Int 0;
    Array.sort (fun a b -> compare b a) a;
    Array.iter (fun d ->
        let c = v.(d) in
        for j = 0 to n - c do
            if dp.(j) <> Int (-1) then (
                let q = dp.(j) */ Int 10 +/ Int d in
                dp.(j + c) <- max_num dp.(j + c) q
            )
        done
    ) a;
    print_endline @@ string_of_num dp.(n)
)