#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair< int, int >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)
const ll INF=1e17;
using namespace std;
string repeat(string s, int n) {
    string s1 = "";
    for (int i=0; i<n;i++)
        s1+=s;
    return s1;
}
string getString(char x) {
    string s(1, x);
    return s;
}

void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int gcd(int a, int b){
    if (a == 0)  return b;
    return gcd(b % a, a);
}
ll ans1=0;
void recur(vector<pair<char,int>> str,int val,ll sum,int idx){
  if(val<0) return;
  else if(val==0){
    ans1=max(ans1,sum);
    return;
  }
  if(idx>=str.size()) return;
  rep(j,idx,str.size()){
    recur(str,val-str[j].s,sum*10+str[j].f-'0',idx+1);
    recur(str,val-str[j].s,sum*10+str[j].f-'0',idx);
  }
}
void  solve(){
  int n,m2;
  cin>>n>>m2;
  int a[m2];
  rep(i,0,m2) cin>>a[i];
  sort(a,a+m2);
  map<int,int> m,m1;
  m[1]=2;
  m[2]=5;
  m[3]=5;
  m[4]=4;
  m[5]=5;
  m[6]=6;
  m[7]=3;
  m[8]=7;
  m[9]=6;
  rep(i,0,m2){
    m1[m[a[i]]]=a[i];
  }
  int sma= (*m1.begin()).s;
  int rep = max(n/m[sma]-7,0);
  int rem=n%m[sma]+min(n/m[sma],7)*m[sma];

  string ans="";
  ans+=repeat(getString('0'+sma),rep);
  vector<pair<char,int>> str;
  for(auto i:m1){
    str.pb({i.s+'0',i.f});
  }
  ans1=0;
  recur(str,rem,0,0);
  vector<char> res;
  rep(i,0,ans.length()) res.pb(ans[i]);
  // cout<<ans1<<endl;
  while(ans1){
    res.pb(ans1%10+'0');
    ans1/=10;
  }
  sort(all(res),greater<char>());
  rep(i,0,res.size()) cout<<res[i];
  cout<<endl;
  // if(rem==0) cout<<ans<<endl;
  // else{
  //   int mx=-1;
  //   for(auto i:m1){
  //     if(i.f<=rem) mx=max(mx,i.s);
  //   }
  //   cout<<rem<<endl;
  //   if(mx!=-1 && sma>mx) ans+= getString(mx+'0');
  //   else if(mx!=-1 && sma<=mx) ans=getString(mx+'0')+ans;
  //   cout<<ans<<endl;
  // }
}
int main(){
    optimizeIO();
    // tc
    { solve();
    }
}

#include "bits/stdc++.h"
using namespace std;

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)
#define REP(i, vec) for(auto i = vec.begin(); i != vec.end(); ++i)

const int mod = 1000000007;
const int inf = (1 << 21);
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;
using vll = vector<pair<ll, ll>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline T GCD(T a, T b) { return b ? GCD(b, a % b) : a; }
template<class T> inline T LCM(T a, T b) { return (a + (b - 1)) / b; }

int round_int(int a, int b) { return (a + (b - 1)) / b; }

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };

// ──────────────────────────────────────────────────────────────────

constexpr array<int, 10> cost = { -1,2,5,5,4,5,6,3,7,6 };

int calc_cost(deque<int> d) {
	int res = 0;
	while (!d.empty()) {
		int v = d.front(); d.pop_front();
		res += cost[v];
	}
	return res;
}

int main() {
	int n, m;
	cin >> n >> m;

	vi a(m);
	rep(i, m) cin >> a[i];

	vector<vector<deque<int>>> dp(n + 1, vector<deque<int>>(m));

	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j < m; ++j) {
			int c = cost[a[j]];
			//cout << i << " " << j << endl;
			if (i - c >= 0 && calc_cost(dp[i - c][j]) + c == i) {
				deque<int> pre = dp[i - c][j];
				if (pre.size() == 0) pre.push_back(a[j]);
				else if (pre.front() > a[j]) pre.push_back(a[j]);
				else pre.push_front(a[j]);
				dp[i][j] = pre;
			}

			if (j != 0) {
				deque<int> pre = dp[i][j - 1];

				if (pre.size() < dp[i][j].size()) continue;
				else if (pre.size() > dp[i][j].size()) dp[i][j] = pre;
				else if (pre.size() == dp[i][j].size()) {
					if (pre.size() == 0) continue;

					deque<int> tmp1 = pre, tmp2 = dp[i][j];
					for (int k = 0; k < pre.size(); ++k) {
						int l = tmp1.front(), r = tmp2.front();
						tmp1.pop_front(), tmp2.pop_front();

						if (l > r) {
							dp[i][j] = pre;
							break;
						}
						else if (l < r) {
							break;
						}
					}
				}
			}
		}
	}

	int mx = dp[n][m - 1].size();
	rep(i, mx) {
		int v = dp[n][m - 1].front(); dp[n][m - 1].pop_front();
		cout << v;
	}
}