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
# マッチをk(=2..7)本使う場合の一番大きい値を総当たり
def f(nokori, current):
  if nokori == 0:
    return current
  if nokori < 0:
    return -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
  return max(temps)

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる
def hoge():
  for key, nums in list(d.items()):
      for num in nums:
        if enable[num]:
          return (num, key)
num, key = hoge()
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
# マッチをk(=2..7)本使う場合の一番大きい値を総当たり a l enable[a] = True enable[a] enable a True def f(nokori, current):
  if nokori == 0:
    return current
  if nokori < 0:
    return -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
  return max(temps)

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる f if nokori == 0:
    return current
   nokori == 0 nokori 0 return current current if nokori < 0:
    return -1
   nokori < 0 nokori 0 return -1 -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 return max(temps) max(temps) max temps nokori nokori current current def hoge():
  for key, nums in list(d.items()):
      for num in nums:
        if enable[num]:
          return (num, key) hoge for key, nums in list(d.items()):
      for num in nums:
        if enable[num]:
          return (num, key) key nums list(d.items()) list d.items() d.items d items for num in nums:
        if enable[num]:
          return (num, key) num nums if enable[num]:
          return (num, key) enable[num] enable num return (num, key) (num, key) num key num, key = hoge() num hoge() hoge key 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 num_ans = str(f(n, 0)) str(f(n, 0)) num_ans m = list(map(int, input().split(' '))) list(map(int, input().split(' '))) m cs = [] [] cs key = hoge() hoge() key n -= key key n l = list(map(int, input().split(' '))) list(map(int, input().split(' '))) l n, m = list(map(int, input().split(' '))) list(map(int, input().split(' '))) n enable = [False] * 10 [False] * 10 enable def hoge():
  for key, nums in list(d.items()):
      for num in nums:
        if enable[num]:
          return (num, key) def hoge():
  for key, nums in list(d.items()):
      for num in nums:
        if enable[num]:
          return (num, key) hoge d = {} {} d def f(nokori, current):
  if nokori == 0:
    return current
  if nokori < 0:
    return -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
  return max(temps)

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる def f(nokori, current):
  if nokori == 0:
    return current
  if nokori < 0:
    return -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
  return max(temps)

# 余裕があるうちは一番マッチの本数が少ないモノをヤケクソで詰めまくる f num, key = hoge() hoge() num
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
# マッチをk(=2..7)本使う場合の一番大きい値を総当たり
def f(nokori, current):
  if nokori == 0:
    return current
  if nokori < 0:
    return -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
  return max(temps)

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

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