import sys
readline = sys.stdin.readline

N,M = map(int,readline().split())
A = set(list(map(int,readline().split())))

dic = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

# ある本数で作れる数
candi = [[],[],[1],[7],[4],[2,3,5],[6,9],[8]]

# たとえば2と3を両方作れるときに、2を作るメリットはない。
if 2 in A and 3 in A:
  A.remove(2)
if 2 in A and 5 in A:
  A.remove(2)
if 3 in A and 5 in A:
  A.remove(3)
if 6 in A and 9 in A:
  A.remove(6)

for i in range(len(candi)):
  for j in range(len(candi[i])-1,-1,-1):
    if candi[i][j] not in A:
      del candi[i][j]

# マッチをある本数使って作れる桁数の最大値をdp
# そのときの使用文字も記録
# 最大値が同じときは、大小比較
dp = [-1] * (N + 1)
ans = [""] * (N + 1)
dp[0] = 0
def to_num(x):
  return int("".join(sorted(x, reverse = True)))
for i in range(len(candi)):
  if not candi[i]:
    continue
  num = str(candi[i][0]) # i文字使って作る文字
  # i文字使って作る
  for j in range(len(dp)):
    if dp[j] == -1:
      continue
    #print("j",j)
    if j + i > N:
      continue
    if dp[j + i] < dp[j] + 1:
      dp[j + i] = dp[j] + 1
      ans[j + i] = ans[j] + num
    elif dp[j + i] == dp[j] + 1:
      if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans)

print(to_num(ans[N])) import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline N,M = map(int,readline().split()) N map(int,readline().split()) map int readline().split() ().split () readline split M A = set(list(map(int,readline().split()))) A set(list(map(int,readline().split()))) set list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split dic = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} dic {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 candi = [[],[],[1],[7],[4],[2,3,5],[6,9],[8]] candi [[],[],[1],[7],[4],[2,3,5],[6,9],[8]] [] [] [1] 1 [7] 7 [4] 4 [2,3,5] 2 3 5 [6,9] 6 9 [8] 8 if 2 in A and 3 in A:
  A.remove(2) 2 in A and 3 in A 2 in A 2 A 3 in A 3 A A.remove(2) A.remove A remove 2 if 2 in A and 5 in A:
  A.remove(2) 2 in A and 5 in A 2 in A 2 A 5 in A 5 A A.remove(2) A.remove A remove 2 if 3 in A and 5 in A:
  A.remove(3) 3 in A and 5 in A 3 in A 3 A 5 in A 5 A A.remove(3) A.remove A remove 3 if 6 in A and 9 in A:
  A.remove(6) 6 in A and 9 in A 6 in A 6 A 9 in A 9 A A.remove(6) A.remove A remove 6 for i in range(len(candi)):
  for j in range(len(candi[i])-1,-1,-1):
    if candi[i][j] not in A:
      del candi[i][j]

# マッチをある本数使って作れる桁数の最大値をdp
# そのときの使用文字も記録
# 最大値が同じときは、大小比較 i range(len(candi)) range len(candi) len candi for j in range(len(candi[i])-1,-1,-1):
    if candi[i][j] not in A:
      del candi[i][j]

# マッチをある本数使って作れる桁数の最大値をdp
# そのときの使用文字も記録
# 最大値が同じときは、大小比較 j range(len(candi[i])-1,-1,-1) range len(candi[i])-1 len(candi[i]) len candi[i] candi i 1 -1 -1 if candi[i][j] not in A:
      del candi[i][j]

# マッチをある本数使って作れる桁数の最大値をdp
# そのときの使用文字も記録
# 最大値が同じときは、大小比較 candi[i][j] not in A candi[i][j] [i] candi i j A del candi[i][j] candi[i][j] [i] candi i j dp = [-1] * (N + 1) dp [-1] * (N + 1) [-1] -1 N + 1 N 1 ans = [""] * (N + 1) ans [""] * (N + 1) [""] "" N + 1 N 1 dp[0] = 0 dp[0] dp 0 0 def to_num(x):
  return int("".join(sorted(x, reverse = True))) to_num return int("".join(sorted(x, reverse = True))) int("".join(sorted(x, reverse = True))) int "".join(sorted(x, reverse = True)) "".join "" join sorted(x, reverse = True) sorted x reverse = True reverse True x x for i in range(len(candi)):
  if not candi[i]:
    continue
  num = str(candi[i][0]) # i文字使って作る文字
  # i文字使って作る
  for j in range(len(dp)):
    if dp[j] == -1:
      continue
    #print("j",j)
    if j + i > N:
      continue
    if dp[j + i] < dp[j] + 1:
      dp[j + i] = dp[j] + 1
      ans[j + i] = ans[j] + num
    elif dp[j + i] == dp[j] + 1:
      if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans) i range(len(candi)) range len(candi) len candi if not candi[i]:
    continue
   not candi[i] candi[i] candi i continue num = str(candi[i][0]) num str(candi[i][0]) str candi[i][0] [i] candi i 0 for j in range(len(dp)):
    if dp[j] == -1:
      continue
    #print("j",j)
    if j + i > N:
      continue
    if dp[j + i] < dp[j] + 1:
      dp[j + i] = dp[j] + 1
      ans[j + i] = ans[j] + num
    elif dp[j + i] == dp[j] + 1:
      if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans) j range(len(dp)) range len(dp) len dp if dp[j] == -1:
      continue
    #print("j",j)
     dp[j] == -1 dp[j] dp j -1 continue if j + i > N:
      continue
     j + i > N j + i j i N continue if dp[j + i] < dp[j] + 1:
      dp[j + i] = dp[j] + 1
      ans[j + i] = ans[j] + num
    elif dp[j + i] == dp[j] + 1:
      if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans) dp[j + i] < dp[j] + 1 dp[j + i] dp j + i j i dp[j] + 1 dp[j] dp j 1 dp[j + i] = dp[j] + 1 dp[j + i] dp j + i j i dp[j] + 1 dp[j] dp j 1 ans[j + i] = ans[j] + num ans[j + i] ans j + i j i ans[j] + num ans[j] ans j num elif dp[j + i] == dp[j] + 1:
      if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans) dp[j + i] == dp[j] + 1 dp[j + i] dp j + i j i dp[j] + 1 dp[j] dp j 1 if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans) to_num(ans[j + i]) < to_num(ans[j] + num) to_num(ans[j + i]) to_num ans[j + i] ans j + i j i to_num(ans[j] + num) to_num ans[j] + num ans[j] ans j num ans[j + i] = ans[j] + num ans[j + i] ans j + i j i ans[j] + num ans[j] ans j num print(to_num(ans[N])) print to_num(ans[N]) to_num ans[N] ans N dic = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} dic M = map(int,readline().split()) map(int,readline().split()) M A = set(list(map(int,readline().split()))) set(list(map(int,readline().split()))) A candi = [[],[],[1],[7],[4],[2,3,5],[6,9],[8]] [[],[],[1],[7],[4],[2,3,5],[6,9],[8]] candi N,M = map(int,readline().split()) map(int,readline().split()) N dp = [-1] * (N + 1) [-1] * (N + 1) dp num = str(candi[i][0]) str(candi[i][0]) num readline = sys.stdin.readline sys.stdin.readline readline ans = [""] * (N + 1) [""] * (N + 1) ans def to_num(x):
  return int("".join(sorted(x, reverse = True))) def to_num(x):
  return int("".join(sorted(x, reverse = True))) to_num
import sys
readline = sys.stdin.readline

N,M = map(int,readline().split())
A = set(list(map(int,readline().split())))

dic = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

# ある本数で作れる数
candi = [[],[],[1],[7],[4],[2,3,5],[6,9],[8]]

# たとえば2と3を両方作れるときに、2を作るメリットはない。
if 2 in A and 3 in A:
  A.remove(2)
if 2 in A and 5 in A:
  A.remove(2)
if 3 in A and 5 in A:
  A.remove(3)
if 6 in A and 9 in A:
  A.remove(6)

for i in range(len(candi)):
  for j in range(len(candi[i])-1,-1,-1):
    if candi[i][j] not in A:
      del candi[i][j]

# マッチをある本数使って作れる桁数の最大値をdp
# そのときの使用文字も記録
# 最大値が同じときは、大小比較
dp = [-1] * (N + 1)
ans = [""] * (N + 1)
dp[0] = 0
def to_num(x):
  return int("".join(sorted(x, reverse = True)))
for i in range(len(candi)):
  if not candi[i]:
    continue
  num = str(candi[i][0]) # i文字使って作る文字
  # i文字使って作る
  for j in range(len(dp)):
    if dp[j] == -1:
      continue
    #print("j",j)
    if j + i > N:
      continue
    if dp[j + i] < dp[j] + 1:
      dp[j + i] = dp[j] + 1
      ans[j + i] = ans[j] + num
    elif dp[j + i] == dp[j] + 1:
      if to_num(ans[j + i]) < to_num(ans[j] + num):
        ans[j + i] = ans[j] + num
    #print(dp)
    #print(ans)

print(to_num(ans[N]))