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

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x,y) CPP_CAT_I(x,y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x,y) x ## y

using i8  = int8_t;
using u8  = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end) for(i64 i = (start), CPP_CAT(i,xxxx_end)=(end); i < CPP_CAT(i,xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for(i64 i = 1; i <= n; ++i)
#define RREP(i, n) for(i64 i = n - 1; i >= 0 ; --i)
#define RREPS(i, n) for(i64 i = n; i >= 1; --i)

#define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c))

#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define pb push_back
#define mp make_pair
#define F first
#define S second

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

template<typename C>
i64 SIZE(const C& c) { return static_cast<i64>(c.size()); }

template<typename T, size_t N>
i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); }


struct ProconInit {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;

const i64 C[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
    i64 N, M; cin >> N >> M;
    vector<i64> A(M);
    unordered_map<i64, i64> D2C;
    unordered_map<i64, i64> C2D;
    REPS(i, 9) D2C[i] = C[i];
    vector<i64> cost;

    vector<bool> used(10, false);
    REP(i, M) {
        cin >> A[i];
        i64 c = D2C[A[i]];
        cost.pb(c);
        chmax(C2D[c], A[i]);
        used[A[i]] = true;
    }

    sort(cost.begin(), cost.end());
    UNIQUE(cost);

    vector<vector<unordered_map<i64, i64>>> memo(cost.size() + 1, vector<unordered_map<i64, i64>>(N + 1));
    vector<vector<i64>> dp(cost.size() + 1, vector<i64>(N + 1, -1));
    dp[0][0] = 0;

    auto comp = [&](unordered_map<i64, i64> &m1, unordered_map<i64, i64> &m2) {
        bool pos = true;
        RREPS(i, 9) {
            if (m1[i] > m2[i]) {
                pos = true;
                break;
            } else if (m1[i] == m2[i]) continue;
            else {
                pos = false;
                break;
            }
        }
        return pos ? m1 : m2;
    };

    REP(i, cost.size()) {
        i64 ci = cost[i];
        for (i64 n = 0; n <= N; n++) {
            if (n - ci >= 0) {
                if (dp[i + 1][n] < dp[i + 1][n - ci] + 1) {
                    dp[i + 1][n] = dp[i + 1][n - ci] + 1;
                    memo[i + 1][n] = memo[i + 1][n - ci];
                    memo[i + 1][n][C2D[ci]]++;
                } else if (dp[i + 1][n] == dp[i + 1][n - ci] + 1) {
                    auto d = memo[i + 1][n - ci];
                    d[C2D[ci]]++;
                    memo[i + 1][n] = comp(memo[i + 1][n], d);
                }
            }
            if (dp[i][n] == -1) continue;
            if (dp[i + 1][n] < dp[i][n]) {
                dp[i + 1][n] = dp[i][n];
                memo[i + 1][n] = memo[i][n];
            } else if (dp[i + 1][n] == dp[i][n]) {
                memo[i + 1][n] = comp(memo[i + 1][n], memo[i][n]);
            }
        }
    }

    RREPS(i, 9) {
        REP(j, memo[cost.size()][N][i]) {
            cout << i;
        }
    }
    cout << endl;

    return 0;
}

Measured memory_bytes: 9564