#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <deque>

long long mod = 1e9+7;
class Mint {
public:
  long long x;
  Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく
  Mint(long long a){
    x = a%mod;
    while(x<0){
      x += mod;
    }
  };
  Mint& operator += (const Mint &a){
    x+=a.x;
    x%=mod;
    return *this;
  }
  Mint& operator -= (const Mint &a){
    x += (mod - a.x);
    x%=mod;
    return *this;
  }
  Mint& operator *= (const Mint &a){
    x*=a.x;
    x%=mod;
    return *this;
  }

  // a^n mod を計算する
  // Useaeg: Mint z = y.pow(y, n);
  Mint pow(Mint a, long long n) const {
    Mint y(1);
    Mint b(a);
    while (n > 0) {
      if (n & 1) y *= b;
      b *= b;
      n >>= 1;
    }
    return y;
  }

  // a^{-1} mod を計算する
  Mint modinv(const Mint &a) const {
    return pow(a, mod-2);
  }

  Mint& operator /= (const Mint &a) {
    x *= modinv(a).x;
    x %= mod;
    return *this;
  }

  Mint operator + (Mint &a) const {
    Mint y(*this);
    y += a;
    return y;
  }

  Mint operator - (Mint &a) const {
    Mint y(*this);
    y -= a;
    return y;
  }

  Mint operator * (Mint &a) const {
    Mint y(*this);
    y *= a;
    return y;
  }

  Mint operator / (Mint &a) const {
    Mint y(*this);
    y/=a;
    return y.x;
  }

  // nCk @mod を計算する
  Mint nCk(Mint &n, const long long k) const {
    Mint y = Mint(1);
    for (Mint i(0); (i.x)<k; i.x++){
      y *= (n-i);
      y /= i;
    }
    return y;
  }

  // nPk @mod を計算する
  Mint nPk(Mint &n, long long k) const {
    Mint y(1);
    for (Mint i(0); (i.x)<k; i.x++){
      y *= (n-i);
    }
    return y;
  }
};

std::vector<int > bit(500005, 0);
int N, Q;

void add(int pos, int val) {
  while(pos<=N) {
    //printf("pos=%d\n", pos);
    bit[pos]+=val;
    //printf("bit[%d]+=%d\n", pos, val);
    pos += pos & (-pos);
  }
  return;
}

int sum(int pos) {
  int retval = 0;
  while(pos>0) {
    //printf("pos=%d\n", pos);
    retval += bit[pos];
    pos -= pos & (-pos);
  }
  return retval;
}

void print(){
  for (int i=1; i<=N; i++) {
    printf("bit[%d]=%d\n", i, bit[i]);
  }
  return;
}

std::map<int, int> mp[10004];
int M;
std::vector<int > A;

int getsum(int index) {
  int retval = 0;
  for (int a : A) {
    retval += mp[index][a];
  }
  return retval;
}

void print(int index) {
  for (int a : A) {
    printf("map[%d][%d]=%d\n", index, a, mp[index][a]);
  }
}

int need[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

const int INF = 1001001001;
int main(){
  int N, M;
  std::cin >> N >> M;
  A = std::vector<int>(M);
  for (int i=0; i<M; i++) {
    std::cin >> A[i];
  }
  //printf("readn end\n");
  std::sort(A.rbegin(), A.rend());
  for (int i=0; i<=10000; i++) {
    //printf("i=%d\n", i);
    //print(i);
    if (i>0 && getsum(i)==0) continue;
    for (int a : A) {
      bool replace = false;
      if (i+need[a]>10000) continue;
      if (getsum(i)+1>getsum(i+need[a])) {
        replace = true;
      } else if (getsum(i)+1<getsum(i+need[a])){
        replace = false;
      } else {
        for (int b : A) {
          int cand = mp[i][b];
          if (b==a) cand++;
          int target = mp[i+need[a]][b];
          if (cand>target) {
            replace = true;
            break;
          } else if (target>cand){
            replace = false;
            break;
          }
        }
      }
      //printf("a=%d, replace=%d, target=%d\n", a, replace, i+need[a]);
      if (replace) {
        for (int b : A) {
          if (b==a) mp[i+need[a]][b] = mp[i][b]+1;
          else mp[i+need[a]][b] = mp[i][b];
        }
      }
    }
  }
  std::string ans;
  for (int a : A) {
    for (int i=0; i<mp[N][a]; i++) {
      ans += '0'+a;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

Measured memory_bytes: 9128