/*
ID: seekho
PROG: friday
LANG: C++14                 
*/
/* LANG can be C++11 or C++14 for those more recent releases */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

//find_by_order gives kth-largest element
//order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
// 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define db4(x, y, z, w) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int,int>
#define MOD (ll)(998244353)
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////
int n,m;
const int N = 10005;
int memo[N][10];
int a[10] = {0,2,5,5,4,5,6,3,7,6};

int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(0); 
    cout.tie(0);
    
    cin >> n >> m;
    vector<int> b(m);
    rep(i,m) cin >> b[i];
    int left = n;
    rep(i,m) {
        // db2(a[b[i]],b[i]);
        rep(j,left+1) {
            if(j < a[b[i]]) continue;
            int sum = 0;
            rep(k,10) sum += memo[j][k];
            int sr = j - a[b[i]];

            int sum1 = 0;
            rep(k,10) {
                sum1 += memo[sr][k];
            }
            // db2(sum,sum1);

            if(sr != 0 and sum1 == 0) continue;

            if(sum1+1 < sum) continue;
            if(sum1+1 > sum) {
                rep(k,10)
                    memo[j][k] = memo[sr][k];
                memo[j][b[i]]++;
            }
            else {
                bool ok = 1;
                repD(k,9,1) {
                    // db3(j,k,memo[j][k]);
                    if(k == b[i]) {
                        if(memo[j][k] < memo[sr][k] + 1) {
                            ok = 0;break;
                        }
                        else if(memo[j][k] > memo[sr][k] + 1) break;
                    }
                    else {
                        if(memo[j][k] < memo[sr][k]) {
                            ok=0;break;
                        }
                        else if(memo[j][k] > memo[sr][k]) break;
                    }
                }
                if(ok) continue;
                rep(k,10)
                    memo[j][k] = memo[sr][k];
                memo[j][b[i]]++;
            }
        }
    }
    string ans = "";
    repD(k,9,1) {
        // db(memo[n][k]);
        repA(i,1,memo[n][k])
            ans += (k + '0');
    }
    cout << ans << '\n';
}

Measured memory_bytes: 1024