import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

const int[] cnt = [0,2,5,5,4,5,6,3,7,6];

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto A = readln.split.map!(to!int).filter!(a => cnt[a] <= N).array;
    A.sort();

    int[] ex = new int[](10);

    int[] B;
    foreach_reverse (a; A) {
        if (B.map!(b => cnt[b] == cnt[a]).sum == 0) {
            B ~= a;
            ex[cnt[a]] = a;
        }
    }

    B.sort!"a < b";
    A = B.dup;
    B.sort!((a, b) => cnt[a] < cnt[b]);

    int v = cnt[B.front];


    auto dp = new string[][](100, 100);

    foreach (i; 0..99) {
        foreach (j; 0..100) {
            if (dp[i][j] == "" && (i != 0 || j != 0)) continue;
            foreach (k; 0..A.length) {
                if (j + cnt[A[k]] - v >= 100) continue;
                if (dp[i+1][j+cnt[A[k]] - v].length > dp[i][j].length + 1) continue;
                if (dp[i+1][j+cnt[A[k]] - v].length < dp[i][j].length + 1 ||
                    dp[i+1][j+cnt[A[k]] - v] < dp[i][j] ~ A[k].to!string) {
                    dp[i+1][j+cnt[A[k]] - v] = dp[i][j] ~ A[k].to!string;
                }
            }
        }
    }

    int keta = N / v;

    foreach_reverse (k; keta-50..keta+1) {
        int used = v * k;
        dchar[] ans;
        foreach (i; 0..k) {
            ans ~= B.front.to!string.to!dchar;
        }
        if (N == used) {
            ans.writeln;
            break;
        }

        int rest = N - used;
        string best = "";

        foreach (i; 1..k+1) {
            if (i <= k && dp[i][rest] > best) best = dp[i][rest];
        }

        if (best == "Z") continue;

        int p = 0, q = 0;
        for ( ; q < best.length && best[q]-'0' >= ans[p]; ++p, ++q) {
            ans[p] = best[q];
        }

        p = k - 1;
        q = best.length.to!int - 1;
        for ( ; q >= 0 && best[q]-'0' < ans[p]; --p, --q) {
            ans[p] = best[q];
        }
        ans.writeln;
        break;
    }

}
