#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <set>
#include <string>
#include <map>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int main() {
    int n,m; cin >> n >> m;
    vector<int> a(m);
    rep(i, m) {
        cin >> a[i];
    }
    sort(a.begin(),a.end(), greater<int>());
    map<int, int> num;
    num[1] = 2;
    num[2] = 5;
    num[3] = 5;
    num[4] = 4;
    num[5] = 5;
    num[6] = 6;
    num[7] = 3;
    num[8] = 7;
    num[9] = 6;

    vector<int> use;
    for(int i = 0; i < m; i++) {
        if(a[i] == 2 || a[i] == 3 || a[i] == 6) {
            bool isOK = true;
            for(int j = 0; j < use.size(); j++) {
                if(num[a[i]] == num[use[j]]) {
                    isOK = false;
                    break;
                }
            }
            if(isOK)
                use.push_back(a[i]);
        } else {
            use.push_back(a[i]);
        }
    }


    set<int> s;
    rep(i, m) {
        s.insert(num[a[i]]);
    }

    map<int,int> revmap;
    for(auto x: s) {
        for(int i = 0; i < use.size(); i++) {
            if(x == num[use[i]])
                revmap[x] = use[i];
        }
    }


    vector<vector<int> > dp(n+1,vector<int>());
    for(int i = 1; i <= n; i++) {
        int tmp = 0;
        vector<int> tmpv;
        for(auto x : s) {
            if(i-x < 0)
                continue;
            else if(i-x == 0) {
                if(tmp < dp[i-x].size() + 1) {
                    tmp = dp[i-x].size() + 1;
                    tmpv = dp[i-x];
                    tmpv.push_back(revmap[x]);
                }
            } else {
                // if(dp[i-x].size() > 0 && tmp < dp[i-x].size()) {
                //     tmp = dp[i-x].size() + 1;
                //     tmpv = dp[i-x];
                //     tmpv.push_back(x);
                // }
                if(dp[i-x].size() > 0) {
                    if(tmp < dp[i-x].size() + 1) {
                        tmp = dp[i-x].size() + 1;
                        tmpv = dp[i-x];
                        tmpv.push_back(revmap[x]);
                    } else if(tmp == dp[i-x].size() + 1){
                        vector<int> tmpvv = dp[i-x];
                        tmpvv.push_back(revmap[x]);
                        sort(tmpv.begin(),tmpv.end(), greater<int>());
                        sort(tmpvv.begin(),tmpvv.end(), greater<int>());
                        for(int i = 0; i < tmpv.size(); i++) {
                            if(tmpv[i] > tmpvv[i]) {
                                break;
                            } else if(tmpv[i] < tmpvv[i]) {
                                tmpv = tmpvv;
                                break;
                            }
                        }
                    }
                }
            }
        }
        dp[i] = tmpv;
    }
    vector<int> v = dp[n];
    // for(int x: dp[n]) {
    //     for(int i = 0; i < use.size(); i++) {
    //         if(x == num[use[i]])
    //             v.push_back(use[i]);
    //     }
    // }
    sort(v.begin(), v.end(), greater<int>());
    for(int x: v) {
        cout << x;
    }
    cout << endl;
}


Measured memory_bytes: 110464