#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#ifndef U_L_LONG
typedef unsigned long long U_L_LONG;
#endif
#ifndef L_LONG
typedef long long L_LONG;
#endif

typedef std::pair<int, int> IntPair;

template <class _Ty> inline _Ty tp_abs(const _Ty& a)
{
	//	usage:  tp_abs<type name>(a);

	return ((a < 0) ? (a * -1) : a);

}

template <class _Ty> inline void tp_swap(_Ty & right, _Ty & left) 
{
	//	usage:  tp_swap<type name>(a, b);

	_Ty tmp = right;
	right = left;
	left = tmp;
}


//static void ABC120D_DecayedBridges();
static void ABC118D_MatchMatching();


#if 1
int main()
{
	//ABC120D_DecayedBridges();
	ABC118D_MatchMatching();

	fflush(stdout);
	return 0;
}
#endif


class ABC118D_NumInfo
{
public:
	int number = 0;
	int needMatch = 0;
	int useCount = 0;
};

static bool ABC118D_sort_match_greater(const ABC118D_NumInfo& right, const ABC118D_NumInfo& left)
{
	bool greater = false;
	if (right.needMatch > left.needMatch) {
		greater = true;
	}
	else if (right.needMatch == left.needMatch) {
		if (right.number > left.number) {
			greater = true;
		}
	}
	return greater;
}

static bool ABC118D_sort_num_greater(const ABC118D_NumInfo& right, const ABC118D_NumInfo& left)
{
	return (right.number > left.number);
}


static int ABC118D_searchMaxDigitOne(ABC118D_NumInfo* info, int infoCount, int nowIdx, int restMatchCnt)
{
	if (restMatchCnt == 0) {
		return 0;
	}
	else if (nowIdx == infoCount) {
		return -1;
	}
	int tmpRestCnt = restMatchCnt;
	int maxDigit = -1;
	int tmpDigit = -1;
	int digit;
	for (digit = 0; tmpRestCnt > 0; digit++) {
		tmpRestCnt = restMatchCnt - (info[nowIdx].needMatch * digit);
		if (tmpRestCnt == 0) {
			tmpDigit = digit;
		}
		else if (tmpRestCnt > 0) {
			tmpDigit = ABC118D_searchMaxDigitOne(info, infoCount, nowIdx + 1, tmpRestCnt);
			if (tmpDigit >= 0) {
				tmpDigit += digit;
			}
		}
		else {
			continue;
		}

		if (maxDigit < tmpDigit) {
			maxDigit = tmpDigit;
		}
	}
	return maxDigit;

}

static int ABC118D_searchMaxDigit(ABC118D_NumInfo* info, int infoCount, int nowIdx, int restMatchCnt, int* dp)
{
	for (int useMatch = 0; useMatch <= restMatchCnt; useMatch++) {
		int dpIdx;
		int dpTmp;
		int dpNow = -1;
		for (int idx = 0; idx < infoCount; idx++) {
			dpIdx = useMatch - info[idx].needMatch;
			if ((dpIdx < 0) || (dp[dpIdx] < 0)) {
				continue;
			}

			dpTmp = dp[dpIdx] + 1;
			if (dpNow < dpTmp) {
				dpNow = dpTmp;
			}
		}

		if (dpNow >= 0) {
			dp[useMatch] = dpNow;
		}
		else {
			dp[useMatch] = ABC118D_searchMaxDigitOne(info, infoCount, 0, useMatch);
		}
	}
	return dp[restMatchCnt];
}

static bool ABC118D_decideNum(ABC118D_NumInfo* info, int infoCnt, int useMatch, int* dp)
{
	bool decided = false;
	int digit = dp[useMatch];
	int restCnt = useMatch;
	for (int i = 0; i < infoCnt; i++) {

		while ((digit > 0) && ((restCnt - info[i].needMatch) >= 0)) {
			if ((digit - 1) == dp[restCnt - info[i].needMatch]) {
				info[i].useCount++;
				digit--;
				restCnt -= info[i].needMatch;
			}
			else {
				break;
			}
		}
	}

	if (digit == 0) {
		decided = true;
	}
	return decided;
}

static void ABC118D_MatchMatching()
{
	static int dp[10004];
	::memset(dp, -1, sizeof(dp));

	int N, M;
	ABC118D_NumInfo A[11];
	int needMatchCnt[] = { 2,5,5,4,5,6,3,7,6 };

	scanf(" %d %d", &N, &M);
	
	for (int i = 0; i < M; i++) {
		scanf(" %d", &(A[i].number));
		A[i].needMatch = needMatchCnt[A[i].number - 1];
	}

	std::sort(A, &A[M], ABC118D_sort_match_greater);
	
	int digit = ABC118D_searchMaxDigit(A, M, 0, N, dp);
	//	桁数が決まらない場合というのは，問題の制約から存在しない

	
	std::sort(A, &A[M], ABC118D_sort_num_greater);
	
	if (ABC118D_decideNum(A, M, N, dp)) {
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < A[i].useCount; j++) {
				printf("%d", A[i].number);
			}
		}
	}
	else { printf("cannot find num. bug!"); }
	printf("\n");
}


Measured memory_bytes: 256