//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#include <stdexcept>

using namespace std;

//conversion
//------------------------------------------
inline int toint(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (l" << __line__ << ")" << " " << __file__ << endl

// type alias
using ll = long long;
using ull = unsigned long long;

template<class ll>
inline ll getCeilExponent(ll a)
{
	if (a <= 0)
	{
		throw invalid_argument("vaule equal to or less than 0 should not be valid");
	}

	ll index = 0;
	--a;
	for (; a != 0; a >>= 1)
	{
		++index;
	}
	return index;
}

template<class ll>
inline ll getFloorExponent(ll a)
{
	if (a <= 0)
	{
		throw invalid_argument("vaule equal to or less than 0 should not be valid");
	}

	ll index = 0;
	a >>= 1;
	for (; a != 0; a >>= 1)
	{
		++index;
	}
	return index;
}

// computational complexity: o(log(max(a, b))) 
template<class ll>
inline ll getGcd(ll a, ll b)
{
	if (b == 0)
	{
		return a;
	}
	return getGcd(b, a % b);
}

// computational complexity: o(log(max(a, b))) 
template<class ll>
inline pair<ll, ll> getBezoutsIdentitySolution(ll a, ll b)
{
	if (b == 0)
	{
		return { 1, 0 };
	}
	auto sol = getBezoutsIdentitySolution(b, a % b);
	return { sol.second, sol.first - (a / b) * sol.second };
}

template<class Integer>
inline Integer getPower(Integer base, unsigned exponential)
{
	Integer res = 1;
	while (exponential >= 1)
	{
		if (exponential & 1)
		{
			res *= base;
		}
		base = base * base;
		exponential >>= 1;
	}

	return res;
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);


	vector<ll> matchCount(10);
	matchCount[1] = 2;
	matchCount[2] = 5;
	matchCount[3] = 5;
	matchCount[4] = 4;
	matchCount[5] = 5;
	matchCount[6] = 6;
	matchCount[7] = 3;
	matchCount[8] = 7;
	matchCount[9] = 6;

	ll N, M;
	cin >> N >> M;

	vector<ll> As(M);
	for (ll i = 0; i < M; ++i)
	{
		cin >> As[i];
	}

	auto comp = [](const vector<ll>& a, const vector<ll>& b)
	{
		auto sum_a = accumulate(a.begin() + 1, a.end(), 0);
		auto sum_b = accumulate(b.begin() + 1, b.end(), 0);

		if (sum_a < sum_b)
		{
			return true;
		}
		if (sum_a > sum_b)
		{
			return false;
		}

		for (ll i = 9; i >= 1; --i)
		{
			if (a[i] < b[i])
			{
				return true;
			}
			if (a[i] > b[i])
			{
				return false;
			}
		}
		return false;
	};

	vector<vector<ll>> integerCounts(N + 1);
	integerCounts[0] = vector<ll>(10, 0);
	for (ll i = 1; i <= N; ++i)
	{
		vector<vector<ll>> cands;
		cands.reserve(9);
		for (ll j = 0; j < As.size(); ++j)
		{
			if (i - matchCount[As[j]] >= 0 && integerCounts[i - matchCount[As[j]]][0] != -1)
			{
				auto cand = integerCounts[i - matchCount[As[j]]];
				++cand[As[j]];
				cands.push_back(cand);
			}
		}
		if (cands.size() == 0)
		{
			integerCounts[i] = vector<ll>(10, -1);
		}
		else
		{
			integerCounts[i] = *max_element(cands.begin(), cands.end(), comp);
		}
	}

	for (ll i = 9; i >= 1; --i)
	{
		for (ll j = 0; j < integerCounts[N][i]; ++j)
		{
			cout << i;
		}
	}
	cout << endl;

	return 0;
}


Measured memory_bytes: 1408