#include <bits/stdc++.h>

using namespace std;

#define countof(a) (sizeof(a)/sizeof(*a))

#define vi vector<int>
#define vvi vector<vector<int> >
#define vpi vector<pi >
#define pi pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define all(n) n.begin(), n.end()

#define FROMTO(var, from, to) for (register int var = (from), var##down = ((int)(to)) < ((int)(from));var##down ? (var >= (int)(to)) : (var <= (int)(to));var##down ? var-- : var++)
#define UPTO(var, from, to)   for (register int var = (from); var <= ((int)to); var++)
#define DOWNTO(var, from, to) for (register int var = (from); var >= ((int)to); var--)
#define FOR(var, to)          UPTO(var, 0, (to)-1)
#define DOWN(var, from)       DOWNTO(var, (from)-1, 0)

#define INIT(var, val) FOR(i,countof(var)) var[i] = val
#define INPUT(var) FOR(i,countof(var)) cin >> var[i]
#define INPUT1(var) FOR(i,countof(var)) cin >> var[i], var[i]--

#define SORT(v) qsort(v,countof(v),sizeof(*v),int_less)
#define SORTT(v) qsort(v,countof(v),sizeof(*v),int_greater)
#define QSORT(v,b) qsort(v,countof(v),sizeof(*v),b)

#define MOD 1000000007
#define INF ((1 << 30)-1)
#define LINF ((1LL << 62)-1)

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

struct Comb {
    vector<vector<s64> > data;
    Comb(int n) { // O(n^2)
        data = vector<vector<s64> >(n+1,vector<s64>(n+1,1));
        UPTO(i,1,n) {
            FOR(j,i+1) {
                if (!j || j == i) data[i][j] = 1;
                else data[i][j] = data[i-1][j-1] + data[i-1][j];
            }
        }
    }

    s64 ncr(int n, int r) {
        return data[n][r];
    }
};

static inline int ri() {
  int a;
  scanf("%d", &a);
  return a;
}

int int_less(const void *a, const void *b) {
  return (*((const int*)a) - *((const int*)b));
}
int int_greater(const void *a, const void *b) {
  return (*((const int*)b) - *((const int*)a));
}

bool greater_my(vector<int> a, vector<int> b) {
	int a_sum = accumulate(all(a), 0);
	int b_sum = accumulate(all(b), 0);
	if (a_sum != b_sum) return a_sum > b_sum;

	return a > b;
}

int x[] = {2,5,5,4,5,6,3,7,6};
int main() {
	int n = ri();
	int m = ri();

	set<int, greater<int> > a;
	FOR(i,m) a.insert(ri());

	vector<int> v;
	for (auto i : a) v.push_back(i);

	vector<vector<vector<int> > > dp(m+1, vvi(n+1, vi()));
	vector<vector<bool> > av(m+1, vector<bool>(n+1, false));
	av[0][n] = true;
	vector<int> first(m,0);
	dp[0][n] = first;

	FOR(i,m) {
		int cur_cost = x[v[i]-1];
		// dp[i][j] : j left
		DOWN(j,n+1) {
			if (!av[i][j]) continue;
			// use cur
			if (j >= cur_cost) {
				vector<int> next = dp[i][j];
				next[i]++;
				if (av[i][j-cur_cost]) {
					if (greater_my(next, dp[i][j-cur_cost]))
						dp[i][j-cur_cost] = next;
				} else {
					dp[i][j-cur_cost] = next;
					av[i][j-cur_cost] = true;
				}
			}
			// skip next
			if (av[i+1][j]) {
				if (greater_my(dp[i][j], dp[i+1][j]))
					dp[i+1][j] = dp[i][j];
			} else {
				av[i+1][j] = true;
				dp[i+1][j] = dp[i][j];
			}
		}
	}

	assert(av[m][0]);
	vi res = dp[m][0];
	FOR(i,m) {
		FOR(j,res[i]) cout << v[i];
	}
	cout << endl;

    return 0;
}


Measured memory_bytes: 6932