n, m = list(map(int, input().split(' ')))
l = list(map(int, input().split(' ')))
d = {}  # key=match, val=num
d[2] = [1]
d[3] = [7]
d[4] = [4]
d[5] = [5, 3, 2]
d[6] = [9, 6]
d[7] = [8]
enable = [False] * 10
for a in l:
  enable[a] = True
memo = {}
# マッチをk(=2..7)本使う場合の一番大きい値を総当たり
def f(nokori, current):
  k = (nokori, current)
  if k in memo:
    return memo[k]
  if nokori <= 0:
    return current if nokori == 0 else -1
  temps = []
  for key, nums in list(d.items())[::-1]:
    for num in nums:
      if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
  memo[k] = max(temps)
  return memo[k]

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる
hoge = False
for key, nums in list(d.items()):
    # print(key, nums, type(nums))
    for num in nums:
      if enable[num]:
        hoge = True
        break
    if hoge:
      break
cs = []
while n > 50:
  n -= key
  cs.append(str(num))

# ヤケクソで詰めたやつと総当たりで調べたマックスを並び替えて辞書順で大きくする
num_ans = str(f(n, 0))
print(''.join(sorted(list(num_ans) + cs)[::-1])) n, m = list(map(int, input().split(' '))) n list(map(int, input().split(' '))) list map(int, input().split(' ')) map int input().split(' ') ().split () input split ' ' m l = list(map(int, input().split(' '))) l list(map(int, input().split(' '))) list map(int, input().split(' ')) map int input().split(' ') ().split () input split ' ' d = {} d {} d[2] = [1] d[2] d 2 [1] 1 d[3] = [7] d[3] d 3 [7] 7 d[4] = [4] d[4] d 4 [4] 4 d[5] = [5, 3, 2] d[5] d 5 [5, 3, 2] 5 3 2 d[6] = [9, 6] d[6] d 6 [9, 6] 9 6 d[7] = [8] d[7] d 7 [8] 8 enable = [False] * 10 enable [False] * 10 [False] False 10 for a in l:
  enable[a] = True a l enable[a] = True enable[a] enable a True memo = {} memo {} def f(nokori, current):
  k = (nokori, current)
  if k in memo:
    return memo[k]
  if nokori <= 0:
    return current if nokori == 0 else -1
  temps = []
  for key, nums in list(d.items())[::-1]:
    for num in nums:
      if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
  memo[k] = max(temps)
  return memo[k]

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる f k = (nokori, current) k (nokori, current) nokori current if k in memo:
    return memo[k]
   k in memo k memo return memo[k] memo[k] memo k if nokori <= 0:
    return current if nokori == 0 else -1
   nokori <= 0 nokori 0 return current if nokori == 0 else -1 current if nokori == 0 else -1 nokori == 0 nokori 0 current -1 temps = [] temps [] for key, nums in list(d.items())[::-1]:
    for num in nums:
      if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
   key nums list(d.items())[::-1] (d.items()) list d.items() d.items d items ::-1 -1 for num in nums:
      if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
   num nums if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
   enable[num] enable num if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
         nokori-key != 0 and current % 10 != 0 and current % 10 < num nokori-key != 0 and current % 10 != 0 nokori-key != 0 nokori-key nokori key 0 current % 10 != 0 current % 10 current 10 0 current % 10 < num current % 10 current 10 num continue temps.append(f(nokori-key, current*10+num)) temps.append temps append f(nokori-key, current*10+num) f nokori-key nokori key current*10+num current*10 current 10 num break memo[k] = max(temps) memo[k] memo k max(temps) max temps return memo[k] memo[k] memo k nokori nokori current current hoge = False hoge False for key, nums in list(d.items()):
    # print(key, nums, type(nums))
    for num in nums:
      if enable[num]:
        hoge = True
        break
    if hoge:
      break key nums list(d.items()) list d.items() d.items d items for num in nums:
      if enable[num]:
        hoge = True
        break
     num nums if enable[num]:
        hoge = True
        break
     enable[num] enable num hoge = True hoge True break if hoge:
      break hoge break cs = [] cs [] while n > 50:
  n -= key
  cs.append(str(num))

# ヤケクソで詰めたやつと総当たりで調べたマックスを並び替えて辞書順で大きくする n > 50 n 50 n -= key n key cs.append(str(num)) cs.append cs append str(num) str num num_ans = str(f(n, 0)) num_ans str(f(n, 0)) str f(n, 0) f n 0 print(''.join(sorted(list(num_ans) + cs)[::-1])) print ''.join(sorted(list(num_ans) + cs)[::-1]) ''.join '' join sorted(list(num_ans) + cs)[::-1] (list(num_ans) + cs) sorted list(num_ans) + cs list(num_ans) list num_ans cs ::-1 -1 l = list(map(int, input().split(' '))) list(map(int, input().split(' '))) l memo = {} {} memo enable = [False] * 10 [False] * 10 enable hoge = True True hoge num_ans = str(f(n, 0)) str(f(n, 0)) num_ans n, m = list(map(int, input().split(' '))) list(map(int, input().split(' '))) n m = list(map(int, input().split(' '))) list(map(int, input().split(' '))) m d = {} {} d def f(nokori, current):
  k = (nokori, current)
  if k in memo:
    return memo[k]
  if nokori <= 0:
    return current if nokori == 0 else -1
  temps = []
  for key, nums in list(d.items())[::-1]:
    for num in nums:
      if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
  memo[k] = max(temps)
  return memo[k]

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる def f(nokori, current):
  k = (nokori, current)
  if k in memo:
    return memo[k]
  if nokori <= 0:
    return current if nokori == 0 else -1
  temps = []
  for key, nums in list(d.items())[::-1]:
    for num in nums:
      if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
  memo[k] = max(temps)
  return memo[k]

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる f hoge = False False hoge n -= key key n cs = [] [] cs
n, m = list(map(int, input().split(' ')))
l = list(map(int, input().split(' ')))
d = {}  # key=match, val=num
d[2] = [1]
d[3] = [7]
d[4] = [4]
d[5] = [5, 3, 2]
d[6] = [9, 6]
d[7] = [8]
enable = [False] * 10
for a in l:
  enable[a] = True
memo = {}
# マッチをk(=2..7)本使う場合の一番大きい値を総当たり
def f(nokori, current):
  k = (nokori, current)
  if k in memo:
    return memo[k]
  if nokori <= 0:
    return current if nokori == 0 else -1
  temps = []
  for key, nums in list(d.items())[::-1]:
    for num in nums:
      if enable[num]:
        if nokori-key != 0 and current % 10 != 0 and current % 10 < num:
          # 98 ok, 89 重複するので最後の桁以外は skip
          continue
        temps.append(f(nokori-key, current*10+num))
        break
  memo[k] = max(temps)
  return memo[k]

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる
hoge = False
for key, nums in list(d.items()):
    # print(key, nums, type(nums))
    for num in nums:
      if enable[num]:
        hoge = True
        break
    if hoge:
      break
cs = []
while n > 50:
  n -= key
  cs.append(str(num))

# ヤケクソで詰めたやつと総当たりで調べたマックスを並び替えて辞書順で大きくする
num_ans = str(f(n, 0))
print(''.join(sorted(list(num_ans) + cs)[::-1]))