///////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <typeinfo>
#include <numeric>

using namespace std;

///////////////////////////////////////////////////////////////////////////////

#define pb push_back
#define V vector
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)

struct UnionFind
{
        unsigned long *parent, *count, *rank;

        UnionFind(unsigned long n) {
                parent = new unsigned long[n+1];
                count = new unsigned long[n+1];
                rank = new unsigned long[n+1];
                for (unsigned long i = 0UL; i < n+1; ++i) {
                        parent[i] = i;
                        count[i] = 1;
                        rank[i] = 0;
                }
        }

        unsigned long root(unsigned long i) {
                if (parent[i] == i) return i;
                parent[i] = root(parent[i]);
                return parent[i];
        }

        void unite(unsigned long i, unsigned long j) {
                unsigned long rooti = root(i);
                unsigned long rootj = root(j);

                if (rooti == rootj) return;

                if (rank[rootj] < rank[rooti]) {
                        parent[i] = parent[j] = parent[rootj] = rooti;
                        count[rooti] += count[rootj];
                }
                else {
                        parent[i] = parent[j] = parent[rooti] = rootj;
                        count[rootj] += count[rooti];
                        if (rank[rootj] == rank[rooti]) rank[rootj]++;
                }
        }

        bool same(unsigned long i, unsigned long j) {
                return root(i) == root(j);
        }
};

struct T2 {
        long t0;
        long t1;
};

struct T3 {
        long t0;
        long t1;
        long t2;
};

struct T4 {
        long t0;
        long t1;
        long t2;
        long t3;
};

bool operator<(const T2 &lhs, const T2 &rhs)
{
        if (lhs.t0 != rhs.t0) return lhs.t0 < rhs.t0;
        return lhs.t1 < rhs.t1;
}

bool operator>(const T2 &lhs, const T2 &rhs)
{
        if (lhs.t0 != rhs.t0) return lhs.t0 > rhs.t0;
        return lhs.t1 > rhs.t1;
}

bool operator==(const T2 &lhs, const T2 &rhs)
{
        if (lhs.t0 != rhs.t0) return false;
        return lhs.t1 == rhs.t1;
}

bool operator!=(const T2 &lhs, const T2 &rhs)
{
        return !(lhs == rhs);
}

bool operator<(const T3 &lhs, const T3 &rhs)
{
        if (lhs.t0 != rhs.t0) return lhs.t0 < rhs.t0;
        if (lhs.t1 != rhs.t1) return lhs.t1 < rhs.t1;
        return lhs.t2 < rhs.t2;
}

bool operator>(const T3 &lhs, const T3 &rhs)
{
        if (lhs.t0 != rhs.t0) return lhs.t0 > rhs.t0;
        if (lhs.t1 != rhs.t1) return lhs.t1 > rhs.t1;
        return lhs.t2 > rhs.t2;
}

bool operator==(const T3 &lhs, const T3 &rhs)
{
        if (lhs.t0 != rhs.t0) return false;
        if (lhs.t1 != rhs.t1) return false;
        return lhs.t2 == rhs.t2;
}

bool operator!=(const T3 &lhs, const T3 &rhs)
{
        return !(lhs == rhs);
}

bool operator<(const T4 &lhs, const T4 &rhs)
{
        if (lhs.t0 != rhs.t0) return lhs.t0 < rhs.t0;
        if (lhs.t1 != rhs.t1) return lhs.t1 < rhs.t1;
        if (lhs.t2 != rhs.t2) return lhs.t2 < rhs.t2;
        return lhs.t3 < rhs.t3;
}

bool operator>(const T4 &lhs, const T4 &rhs)
{
        if (lhs.t0 != rhs.t0) return lhs.t0 > rhs.t0;
        if (lhs.t1 != rhs.t1) return lhs.t1 > rhs.t1;
        if (lhs.t2 != rhs.t2) return lhs.t2 > rhs.t2;
        return lhs.t3 > rhs.t3;
}

bool operator==(const T4 &lhs, const T4 &rhs)
{
        if (lhs.t0 != rhs.t0) return false;
        if (lhs.t1 != rhs.t1) return false;
        if (lhs.t2 != rhs.t2) return false;
        return lhs.t3 == rhs.t3;
}

bool operator!=(const T4 &lhs, const T4 &rhs)
{
        return !(lhs == rhs);
}

void longin(long &a)
{
        cin >> a;
}

void longinl1(auto &v, long count)
{
        for (long i = 0; i < count ; ++i) {
                long a;
                cin >> a;
                v.push_back(a);
        }
}

void longinl2(auto &v, long count)
{
        for (long i = 0; i < count ; ++i) {
                long a, b;
                cin >> a >> b;
                v.push_back({a, b});
        }
}

void longinl3(auto &v, long count)
{
        for (long i = 0; i < count ; ++i) {
                long a, b, c;
                cin >> a >> b >> c;
                v.push_back({a, b, c});
        }
}

void longina(auto &v, long count)
{
        longinl1(v, count);
}

long min(const vector<long> v)
{
        long ret = (long)numeric_limits<double>::infinity();
        for (auto i : v) ret = min(ret, i);
        return ret;
}

void sort(vector<long> &v)
{
        sort(v.begin(), v.end());
}

void sort_reverse(vector<long> &v)
{
        sort(v.begin(), v.end(), greater<long>());
}

void get_divisors(vector<long> &retlist, long x)
{
        for (int i = 1; i < sqrt(x) + 3; ++i) {
                if (x % i == 0) {
                        retlist.push_back(i);
                        retlist.push_back(x / i);
                }
        }
}

void intersection(const set<long> &a, const set<long> &b,
                  set<long> &result)
{
        vector<long> resultlist;

        set_intersection(a.begin(), a.end(),
                         b.begin(), b.end(),
                         back_inserter(resultlist));

        set<long> resultset(resultlist.begin(), resultlist.end());
        result = resultset;
}

unsigned long combination(long x, long y)
{
        if (y > x / 2L) y = x - y;

        unsigned long ret = 1;
        for (int i = 0; i < y; ++i) {
                ret *= x--;
                ret /= (i + 1);
        }

        return ret;
}

void debug_print(auto xlist)
{
        for (auto x : xlist) cout << "-- " << x << endl;
}

///////////////////////////////////////////////////////////////////////////////

static long counts[10] = {1000, 2, 5, 5, 4, 5, 6, 3, 7, 6};
static long nums[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};

void get_divs(V<V<long>> &vv, long n, const vector<long> &subcounts)
{
        long subcmax[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for (auto subc : subcounts) subcmax[subc] = n / subc;

        long patterns = 1;
        rep (i, 10) patterns *= (subcmax[i] + 1);

        long subc[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        rep (i, patterns) {
                long nn = 0;
                rep (j, 10) nn += j * subc[j];
                if (n == nn) {
                        V<long> v;
                        rep (j, 10) v.pb(subc[j]);
                        vv.pb(v);
                }
                rep (j, 10) {
                        if (subc[j] >= subcmax[j]) {
                                subc[j] = 0;
                        }
                        else {
                                subc[j]++;
                                break;
                        }
                }
        }
}

int main()
{
        long n, m;
        longin(n); longin(m);

        vector<long> alist;
        longina(alist, m);

        for (auto a : alist) {
                long count = counts[a];
                nums[count] = max(a, nums[count]);
        }

        long min_count = 1000;
        rep (i, 10) {
                if (nums[i] > 0) {
                        min_count = i;
                        break;
                }
        }

        vector<long> anslist;

        while (n >= min_count) {
                anslist.pb(nums[min_count]);
                n -= min_count;
        }

        vector<long> subcounts;
        rep (i, 10) {
                if (nums[i] > 0 && i > min_count) {
                        subcounts.pb(i - min_count);
                }
        }



        for ( ; ; ) {
                V<long> ans;
                V<V<long>> vv;
                get_divs(vv, n, subcounts);

                for (V<long> v : vv) {
                        V<long> anslistl(anslist);
                        rep (i, 10) {
                                rep (j, v[i]) {
                                        long tmp = anslistl[0];
                                        anslistl.erase(anslistl.begin());
                                        anslistl.pb(nums[counts[tmp] + i]);
                                }
                        }
                        sort_reverse(anslistl);
                        if (ans.size() == 0) ans = anslistl;
                        else {
                                rep (i, anslist.size()) {
                                        if (ans[i] < anslistl[i]) {
                                                ans = anslistl;
                                                break;
                                        }
                                        if (ans[i] > anslistl[i]) {
                                                break;
                                        }
                                }
                        }
                }

                if (ans.size() == 0) {
                        n += counts[anslist[0]];
                        anslist.erase(anslist.begin());
                        continue;
                }

                for (auto num : ans) cout << num;
                return 0;
        }

        return 0;
}

///////////////////////////////////////////////////////////////////////////////


Measured memory_bytes: 384