#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <time.h>

//     MLE,TLE魔
//↓↓↓↓↓↓↓↓↓↓↓↓
//#define int long long
//↑↑↑↑↑↑↑↑↑↑↑↑
//     MLE,TLE魔
#define endre getchar();getchar();return 0
#define moder 1000000007
#define inf 5000000000000000000
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P pair<int,int>
#define all(v) v.begin(),v.end()
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define vecunique(vec) sort(vec.begin(), vec.end());decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());vec.erase(result, vec.end())
using namespace std;

bool prime(int n) {
	for (int i = 2; i <= sqrt(n); i++) {
		if (n%i == 0)return false;
	}
	return n != 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
int lcm(int x, int y) {
	return x * y / gcd(x, y);
}
int mod_pow(int x, int y, int mod) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x%mod;
		}
		x = x * x%mod;
		y >>= 1;
	}
	return res;
}
int kai(int x) {
	if (x == 0)return 1;
	return (kai(x - 1)*x) % moder;
}
int comb(int x, int y) {
	return kai(x)*mod_pow(kai(x - y), moder - 2, moder) % moder*mod_pow(kai(y), moder - 2, moder) % moder;
}
struct edge { int to, cost; };
int dx[5] = { 0,1,0,-1 }, dy[5] = { 1,0,-1,0 };
map<int, int>factor(int x) {
	map<int, int>res;
	for (int i = 2; i*i <= x; i++) {
		while (x%i == 0) {
			x /= i;
			res[i]++;
		}
	}
	if (x != 1)res[x]++;
	return res;
}
/*--------Library Zone!--------*/

int n, m;
int cnt[15];
int c[15];
//dp[i]...i本で作れる最大
string dp[10005];
signed main() {
	cin >> n >> m;
	rep(i, m) {
		int a; cin >> a;
		if (a == 1)c[2] = 1;
		if (a == 2 || a == 3 || a == 5)c[5] = max(c[5], a);
		if (a == 4)c[4] = a;
		if (a == 6 || a == 9)c[6] = max(c[6], a);
		if (a == 7)c[3] = 7;
		if (a == 8)c[7] = 8;
	}
	rep(i, n + 1)dp[i] = "-1";
	rep(i, 9) {
		if (c[i])dp[i] = (char)('0' + c[i]);
	}
	for (int i = 2; i <= n; i++) {
		if (i >= 2 && dp[i - 2] != "-1"&&c[2]) {
			if (dp[i] == "-1")dp[i] = dp[i - 2] + (char)('0' + c[2]);
			else if (dp[i - 2].size() >= dp[i].size()) {
				dp[i] = dp[i - 2] + (char)('0' + c[2]);
			}
			else if (dp[i - 2].size() == dp[i].size() - 1) {
				dp[i] = max(dp[i], dp[i - 2] + (char)('0' + c[2]));
			}
		}
		if (i >= 3 && dp[i - 3] != "-1"&&c[3]) {
			if (dp[i] == "-1")dp[i] = dp[i - 3] + (char)('0' + c[3]);
			else if (dp[i - 3].size() >= dp[i].size()) {
				dp[i] = dp[i - 3] + (char)('0' + c[3]);
			}
			else if (dp[i - 3].size() == dp[i].size() - 1) {
				dp[i] = max(dp[i], dp[i - 3] + (char)('0' + c[3]));
			}
		}
		if (i >= 4 && dp[i - 4] != "-1"&&c[4]) {
			if (dp[i] == "-1")dp[i] = dp[i - 4] + (char)('0' + c[4]);
			else if (dp[i - 4].size() >= dp[i].size()) {
				dp[i] = dp[i - 4] + (char)('0' + c[4]);
			}
			else if (dp[i - 4].size() == dp[i].size() - 1) {
				dp[i] = max(dp[i], dp[i - 4] + (char)('0' + c[4]));
			}
		}
		if (i >= 5 && dp[i - 5] != "-1"&&c[5]) {
			if (dp[i] == "-1")dp[i] = dp[i - 5] + (char)('0' + c[5]);
			else if (dp[i - 5].size() >= dp[i].size()) {
				dp[i] = dp[i - 5] + (char)('0' + c[5]);
			}
			else if (dp[i - 5].size() == dp[i].size() - 1) {
				dp[i] = max(dp[i], dp[i - 5] + (char)('0' + c[5]));
			}
		}
		if (i >= 6 && dp[i - 6] != "-1"&&c[6]) {
			if (dp[i] == "-1")dp[i] = dp[i - 6] + (char)('0' + c[6]);
			else if (dp[i - 6].size() >= dp[i].size()) {
				dp[i] = dp[i - 6] + (char)('0' + c[6]);
			}
			else if (dp[i - 6].size() == dp[i].size() - 1) {
				dp[i] = max(dp[i], dp[i - 6] + (char)('0' + c[6]));
			}
		}
		if (i >= 7 && dp[i - 7] != "-1"&&c[7]) {
			if (dp[i] == "-1")dp[i] = dp[i - 7] + (char)('0' + c[7]);
			else if (dp[i - 7].size() >= dp[i].size()) {
				dp[i] = dp[i - 7] + (char)('0' + c[7]);
			}
			else if (dp[i - 7].size() == dp[i].size() - 1) {
				dp[i] = max(dp[i], dp[i - 7] + (char)('0' + c[7]));
			}
		}
	}
	cout << dp[n] << endl;
	endre;
}

Measured memory_bytes: 39552