// upsolved
#include <bits/stdc++.h>
using namespace std;

using ll=long long;
using vll=vector<ll>;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vpii=vector<pii>;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vi have(10);
    for (int i=0; i<m; ++i) {
        int ai;
        cin >> ai;
        ++have[ai];
    }
    const int cnts[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
    // erase worse duplicates
    if (have[3])
        have[2] = 0;
    if (have[5])
        have[3] = 0;
    if (have[9])
        have[6] = 0;
    // erase worse multiples
    if (have[1]) {
        have[4] = 0;
        have[6] = 0;
        have[9] = 0;
    }
    if (have[7]) {
        have[6] = 0;
        have[9] = 0;
    }
    // erase worse sums
    for (int i=1; i<10; ++i) {
        if (!have[i])
            continue;
        for (int j=1; j<10; ++j) {
            if (!have[j])
                continue;
            for (int k=1; k<10; ++k) {
                if (cnts[i] + cnts[j] == cnts[k])
                    have[k] = 0;
            }
        }
    }
    // sort digits with have[i] by increasing (cnts[i], -i)
    vpii digits;
    for (int i=1; i<10; ++i) {
        if (have[i])
            digits.emplace_back(cnts[i], -i);
    }
    sort(digits.begin(), digits.end());
    // compute dp table
    vpii solvable(n+1, {0,0});
    solvable[0] = {-1,0};
    for (const auto& p: digits) {
        int ci = p.first;
        int ai = -p.second;
        for (int j=0; j<=n; ++j) {
            if (solvable[j].first) {
                // todo if we have a tie, how do we find out which is better?
                //if (j+ci<=n && (!solvable[j+ci].first || (solvable[j+ci].second == solvable[j].second+1 && ai > solvable[j+ci].first))) // use a higher digit if it allows at least as many digits in the solution
                if (j+ci>n)
                    continue;
                if (!solvable[j+ci].first)
                    solvable[j+ci] = {ai, solvable[j].second+1};
                if (solvable[j+ci].second > solvable[j].second+1)
                    continue;
                // claim: replacing is better iff {newlast, new2ndlast} > {oldlast, old2ndlast},
                //        where ">" means max(l) > max(r) || (max(l) == max(r) && min(l) > min(r))
                int oldlast = solvable[j+ci].first;
                int old2ndlast = solvable[j+ci-cnts[oldlast]].first;
                int newlast = ai;
                int new2ndlast = solvable[j].first;
                int oldmax = max(oldlast, old2ndlast);
                int oldmin = min(oldlast, old2ndlast);
                int newmax = max(newlast, new2ndlast);
                int newmin = min(newlast, new2ndlast);
                if (newmax > oldmax || (newmax == oldmax && newmin > oldmin))
                    solvable[j+ci] = {ai, solvable[j].second+1};
            }
        }
    }
    // find solution
    string res;
    for (int v=n; v>0; v-=cnts[solvable[v].first])
        res.push_back('0'+solvable[v].first);
    sort(res.begin(), res.end(), greater<char>());
    cout << res << "\n";

    /*cerr << "DBG: ";
    for (const auto& p: digits)
        cerr << "(" << p.first << ": " << p.second << "), ";
    cerr << "\n";
    for (const auto& p: solvable)
        cerr << "(" << p.first << ", " << p.second << "), ";
    cerr << "\n";*/
}


Measured memory_bytes: 384