import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

set = [[1,2],[7,3],[4,4],[5,5],[3,5],[2,5],[9,6],[6,6],[8,7]]

if 3 in a:
  if 5 in a:
    a.remove(3)
  a.remove(2)

if 9 in a:
  a.remove(6)
  
exist = [i for i in set if i[0] in a]

num = 0
keta = 0
for i in exist:
  if i[0] > num and n//i[1] >= keta:
    num = i[0]
    honsu = i[1]
    keta = n//honsu
    amari = n%honsu
    break

for l in range(keta-1):
  bit = math.ceil(math.log2(m-1))
  exist_tmp = [i for i in exist if i[0] != num]
  for i in range(1,keta+1):
    kouho = []
    for j in range((2**bit)**i):
      nokori = amari + honsu * i
      change = []
      for k in range(i):
        tmp = (j >> k*bit) & (2**bit-1)
        if tmp >= m:
          continue
        nokori -= exist_tmp[tmp][1]
        change.append(exist_tmp[tmp][0])
        if nokori < 0:
          break
      if nokori == 0:
        kouho.append(change)
    if len(kouho) != 0:
      break
  if len(kouho) != 0:
    break
  else:
    keta -= 1
    num = 0
    for i in exist:
      if i[0] > num and n//i[1] >= keta:
        num = i[0]
        honsu = i[1]
        amari = n%honsu
        break

kouho = [sorted(i,reverse=True) for i in kouho]
kouho_tmp = []
for i in kouho:
  if i not in kouho_tmp:
    kouho_tmp.append(i)
kouho = kouho_tmp
kouho.sort(reverse=True)

if len(kouho) > 1:
  for i in range(len(kouho[0])):
    max_num = 0
    for j in range(len(kouho)):
      if kouho[j][i] > max_num:
        max_num = kouho[j][i]
    kouho = [k for k in kouho if k[i] == max_num]
    if len(kouho) == 1:
      break
      
ans = [num]*(keta-len(kouho[0])) + kouho[0]
ans.sort(reverse=True)

ans_txt = ''
for i in ans:
  ans_txt += str(i)
print(ans_txt)

### FIXED CODE ###

import math

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

#値と本数のセット（本数が少ない順かつ値が大きい順）
set = [[1,2],[7,3],[4,4],[5,5],[3,5],[2,5],[9,6],[6,6],[8,7]]

#同じ本数で値が小さい数字を使うことはない
if 2 in a:
  if 3 in a or 5 in a:
    a.remove(2)
    m -= 1

if 3 in a:
  if 5 in a:
    a.remove(3)
    m -= 1
  
if 6 in a:
  if 9 in a:
    a.remove(6)
    m -= 1

#上記の処理を施した上で与えられたaの数と本数の組み合わせを抽出
exist_set = [i for i in set if i[0] in a]

#本数の最大値。あとで使用
max_honsu = max([i[1] for i in exist_set])

#あとでexist_setからnumを除いた数字による探索を行うため
m -= 1

#最大桁数のうち値が大きいもの
max_keta = 0
ans_kouho = []

for exist_one in exist_set:
  keta_tmp = n//exist_one[1]
  num_tmp = exist_one[0]
  amari_tmp = n % exist_one[1]
  honsu_tmp = exist_one[1]
  
  ###print(keta_tmp,num_tmp)
  
  #余りが0出ない場合、aの本数の最大値は不採用（余りを調整できない）
  if amari_tmp != 0 and honsu_tmp == max_honsu:
    continue
  if keta_tmp >= max_keta:
    keta = keta_tmp
    num = num_tmp
    amari = amari_tmp
    honsu = honsu_tmp

    #余り0ならちょうどなので終了
    if amari == 0:
      ans_kouho.append(int(str(num)*keta))
      max_keta = keta
      continue
      
    #以下余りを調整

    #変更する値は必ずnum以外だからexist_setからnumを削除
    exist_yet = [inc for inc in exist_set if inc[0] != num]

    #1桁ずつ値を変更し、あまりを0にできるか調査
    for keta_num in range(1,min(max_honsu,keta+1)):
      
      kouho = []

      #bit全探索
      #候補に使用しないも追加
      bit = math.ceil(math.log2(m+1))
      for all_bit in range((2**m)*keta_num):
        change = []
        nokori = amari + honsu * keta_num
        for bit_conf in range(keta_num):
          bit_index =  (all_bit >> bit_conf*bit) & (2**m-1)

          #bit_indexは0~mだからm+1以上は終了
          if bit_index > m:
            break
            
          #使用しないに相当
          if bit_index == m:
            change.append(0)
            continue

          #bit_indexに従い数字を記録、残りの本数を減らす
          change.append(exist_yet[bit_index][0])
          nokori -= exist_yet[bit_index][1]

          if nokori < 0:
            break
            
        if bit_index > m or nokori < 0:
          continue

        #残りが0ならちょうど使い切る。数の組み合わせを候補に追加
        if nokori == 0:
          kouho.append(change)
      
      #候補から値の大きいものを抜き出す
      if len(kouho) != 0:

        #keta-del_lenがnumの数
        del_len = len(kouho[0])

        #0を削除
        kouho = [[inc_1 for inc_1 in inc_2 if inc_1 != 0] for inc_2 in kouho]

        #0を除いたときの最大桁が候補
        max_len = max([len(inc) for inc in kouho])
        kouho = [inc for inc in kouho if len(inc) == max_len]
        
        max_keta = keta - del_len + max_len

        #候補を降順に並び替え
        kouho = [sorted(kouho_one,reverse=True) for kouho_one in kouho]

        #同じパターンを削除
        kouho_tmp = []
        for kouho_one in kouho:
          if kouho_one not in kouho_tmp:
            kouho_tmp.append(kouho_one)
        kouho = kouho_tmp

        #最大候補の抽出
        if len(kouho) != 1:
          for kouho_one in range(len(kouho[0])):
            max_i = max([inc[kouho_one] for inc in kouho])
            kouho = [inc for inc in kouho if inc[kouho_one] == max_i]
        kouho = kouho[0]

        ans = [num]*(keta-del_len) + kouho
        ans.sort(reverse=True)
        ans_str = ''
        for ans_one in ans:
          ans_str += str(ans_one)
        ans_kouho.append(int(ans_str))

ans_kouho.sort()
###print(ans_kouho)
print(ans_kouho[-1])