import std.algorithm;
import std.bigint;
import std.concurrency;
import std.container;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.meta;
import std.numeric;
import std.random;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.typecons;


auto solve(long N, long M, long[] A) {
    string[long] nums;
    if (A.canFind(1)) nums[2] = "1";
    if (A.canFind(2)) nums[5] = "2";
    if (A.canFind(3)) nums[5] = "3";
    if (A.canFind(4)) nums[4] = "4";
    if (A.canFind(5)) nums[5] = "5";
    if (A.canFind(6)) nums[6] = "6";
    if (A.canFind(7)) nums[3] = "7";
    if (A.canFind(8)) nums[7] = "8";
    if (A.canFind(9)) nums[6] = "9";
    
    const NONE = "0";
    string[long] memo;
    string maxNumMadeOf(long matches) {
        if (matches in memo)
            return memo[matches];
        if (matches == 0)
            return "";
        if (matches < 0)
            return NONE;

        return memo[matches] = nums.byKeyValue
            .map!(t => t.value ~ maxNumMadeOf(matches - t.key))
            .filter!(s => !s.canFind(NONE))
            .chain([NONE])
            .reduce!((a, b) => lt(a, b) ? b : a);
    }

    return maxNumMadeOf(N);
}


bool lt(string a, string b) {
    if (a.length != b.length)
        return a.length < b.length;
    return a < b;
}


void main() {
    auto input = stdin.byLine.map!split.joiner;

    long N;
    N = input.front.to!long;
    input.popFront;

    long M;
    M = input.front.to!long;
    input.popFront;

    long[] A = new long[](cast(size_t) (M));
    foreach (i; 0 .. cast(size_t) (M)) {
        A[i] = input.front.to!long;
        input.popFront;
    }

    static if (is(ReturnType!solve == void))
        solve(N, M, A);
    else
        solve(N, M, A).writeln;
}
