def main():  
  import sys
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines

  n,m = map(int,readline().split())
  a = list(map(int,readline().split()))
  d5 = False
  d9 = False

  if 5 in a:
    d5 = True
  if 9 in a:
    d9 = True
  chk = set()
  for i in a:
    if d5 and (i == 2 or i == 3):
      continue
    if d9 and i == 6:
      continue
    chk.add(i)

  dp = [-float('inf')]*(n+1)
  dp[0] = 0
  f = [0,2,5,5,4,5,6,3,7,6]

  for i in range(2,n+1):
    for j in chk:
      F = f[j]
      if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

  print(dp[n])

if __name__ == '__main__':
    main() def main():  
  import sys
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines

  n,m = map(int,readline().split())
  a = list(map(int,readline().split()))
  d5 = False
  d9 = False

  if 5 in a:
    d5 = True
  if 9 in a:
    d9 = True
  chk = set()
  for i in a:
    if d5 and (i == 2 or i == 3):
      continue
    if d9 and i == 6:
      continue
    chk.add(i)

  dp = [-float('inf')]*(n+1)
  dp[0] = 0
  f = [0,2,5,5,4,5,6,3,7,6]

  for i in range(2,n+1):
    for j in chk:
      F = f[j]
      if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

  print(dp[n]) main import sys sys read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines n,m = map(int,readline().split()) n map(int,readline().split()) map int readline().split() ().split () readline split m a = list(map(int,readline().split())) a list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split d5 = False d5 False d9 = False d9 False if 5 in a:
    d5 = True
   5 in a 5 a d5 = True d5 True if 9 in a:
    d9 = True
   9 in a 9 a d9 = True d9 True chk = set() chk set() set for i in a:
    if d5 and (i == 2 or i == 3):
      continue
    if d9 and i == 6:
      continue
    chk.add(i)

   i a if d5 and (i == 2 or i == 3):
      continue
     d5 and (i == 2 or i == 3) d5 i == 2 or i == 3 i == 2 i 2 i == 3 i 3 continue if d9 and i == 6:
      continue
     d9 and i == 6 d9 i == 6 i 6 continue chk.add(i) chk.add chk add i dp = [-float('inf')]*(n+1) dp [-float('inf')]*(n+1) [-float('inf')] -float('inf') float('inf') float 'inf' n+1 n 1 dp[0] = 0 dp[0] dp 0 0 f = [0,2,5,5,4,5,6,3,7,6] f [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 for i in range(2,n+1):
    for j in chk:
      F = f[j]
      if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

   i range(2,n+1) range 2 n+1 n 1 for j in chk:
      F = f[j]
      if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

   j chk F = f[j] F f[j] f j if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

   F <= i F i dp[i] = max(dp[i-F]*10+j,dp[i]) dp[i] dp i max(dp[i-F]*10+j,dp[i]) max dp[i-F]*10+j dp[i-F]*10 dp[i-F] dp i-F i F 10 j dp[i] dp i print(dp[n]) print dp[n] dp n if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():  
  import sys
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines

  n,m = map(int,readline().split())
  a = list(map(int,readline().split()))
  d5 = False
  d9 = False

  if 5 in a:
    d5 = True
  if 9 in a:
    d9 = True
  chk = set()
  for i in a:
    if d5 and (i == 2 or i == 3):
      continue
    if d9 and i == 6:
      continue
    chk.add(i)

  dp = [-float('inf')]*(n+1)
  dp[0] = 0
  f = [0,2,5,5,4,5,6,3,7,6]

  for i in range(2,n+1):
    for j in chk:
      F = f[j]
      if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

  print(dp[n]) def main():  
  import sys
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines

  n,m = map(int,readline().split())
  a = list(map(int,readline().split()))
  d5 = False
  d9 = False

  if 5 in a:
    d5 = True
  if 9 in a:
    d9 = True
  chk = set()
  for i in a:
    if d5 and (i == 2 or i == 3):
      continue
    if d9 and i == 6:
      continue
    chk.add(i)

  dp = [-float('inf')]*(n+1)
  dp[0] = 0
  f = [0,2,5,5,4,5,6,3,7,6]

  for i in range(2,n+1):
    for j in chk:
      F = f[j]
      if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

  print(dp[n]) main
def main():  
  import sys
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines

  n,m = map(int,readline().split())
  a = list(map(int,readline().split()))
  d5 = False
  d9 = False

  if 5 in a:
    d5 = True
  if 9 in a:
    d9 = True
  chk = set()
  for i in a:
    if d5 and (i == 2 or i == 3):
      continue
    if d9 and i == 6:
      continue
    chk.add(i)

  dp = [-float('inf')]*(n+1)
  dp[0] = 0
  f = [0,2,5,5,4,5,6,3,7,6]

  for i in range(2,n+1):
    for j in chk:
      F = f[j]
      if F <= i:
        dp[i] = max(dp[i-F]*10+j,dp[i])

  print(dp[n])

if __name__ == '__main__':
    main()
