output_description: Print the largest integer that can be formed with exactly N matchsticks under
  the conditions in the problem statement.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      A_1 A_2 ... A_M
attempt: import sys, re, os
  from collections import deque, defaultdict, Counter
  from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
  from itertools import permutations, combinations, product, accumulate
  from operator import itemgetter, mul
  from copy import deepcopy
  from string import ascii_lowercase, ascii_uppercase, digits
  from fractions import gcd
  
  
  def input():
      return sys.stdin.readline().strip()
  
  
  def STR():
      return input()
  
  
  def INT():
      return int(input())
  
  
  def MAP():
      return map(int, input().split())
  
  
  def S_MAP():
      return map(str, input().split())
  
  
  def LIST():
      return list(map(int, input().split()))
  
  
  def S_LIST():
      return list(map(str, input().split()))
  
  
  sys.setrecursionlimit(10**9)
  inf = sys.maxsize
  mod = 10**9 + 7
  
  n, m = MAP()
  a = LIST()
  b = [2, 5, 5, 4, 5, 6, 3, 7, 6]
  arr = [[1, 2], [2, 5], [3, 5], [4, 4], [5, 5], [6, 6], [7, 3], [8, 7], [9, 6]]
  arr2 = []
  for i in range(9):
      if arr[i][0] in a:
          arr2.append(arr[i])
  arr2.sort(key=lambda x: x[1])
  # print('arr2', arr2)
  larr2 = len(arr2)
  ans = []
  arr3 = []
  arr4 = []
  for i in range(larr2):
      for j in range(i + 1, larr2):
          if not arr2[i][1] + arr2[j][1] in arr3:
              arr3.append(arr2[i][1] + arr2[j][1])
              arr4.append([arr2[i][0], arr2[j][0]])
          else:
              index = arr3.index(arr2[i][1] + arr2[j][1])
              arr4[index].sort(reverse=True)
              t = sorted([arr2[i][0], arr2[j][0]], reverse=True)
              flag = False
              if a[0] > arr4[index][0] or (
                  a[0] == arr4[index][0] and t[1] > arr4[index][1]
              ):
                  arr4[index] = t
  # print('arr3', arr3)
  # print('arr4', arr4)
  maxarr3 = max(arr3)
  ii = 0
  while n > 0:
      if n in arr3 and not n - arr2[ii][1] in arr3:
          index = arr3.index(n)
          ans.append(arr4[index][0])
          ans.append(arr4[index][1])
          n = 0
      elif ii >= 0:
          n -= arr2[ii][1]
          ans.append(arr2[ii][0])
      elif n < maxarr3 or ii < 0:
          index = arr2.index([ans[len(ans) - 1], b[ans[len(ans) - 1] - 1]])
          n += arr2[index][1]
          del ans[len(ans) - 1]
          ii = index + 1
          if ii >= larr2:
              ii = -1
      # print(n)
  
  # print('ans', ans)
  ans.sort(reverse=True)
  anstr = ""
  for i in range(len(ans)):
      anstr += str(ans[i])
  print(anstr)
  """
  for i in range(len(arr2)):
      l = n // arr2[i][1] #同じ数字の桁数
      m = n % arr2[i][1] #端数
      print(l, m)
      if m != 0:
          while 
          indexes = []
          for j in range(len(arr2)):
              if m == arr2[j][1]:
                  indexes.append(j)
          print(indexes)
          index = -1
          mx = 0
          for j in indexes:
              if mx < arr2[j][0]:
                  mx = arr2[j][0]
                  index = j
          if index >= 0:
              if arr2[index][0] > arr2[i][0]:
                  ans = str(arr2[index][0]) + str(arr2[i][0]) * l
                  print('a')
                  print(ans)
                  exit()
              else:
                  ans = str(arr2[i][0]) * l + str(arr2[index][0])
                  print('b')
                  print(ans)
                  exit()
      elif m == 0:
          ans = str(arr2[index][0]) * l
          print('c')
          print(ans)
          exit()
  """
  
problem_description: Statement
  
  Find the largest integer that can be formed with exactly N matchsticks, under
  the following conditions:
  
    * Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).
    * The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.
samples: [{"input": "20 4\n    3 7 8 4", "output": "777773\n    \n\nThe integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks,\nand this is the largest integer that can be formed by 20 matchsticks under the\nconditions.\n\n* * *"}, {"input": "101 9\n    9 8 7 6 5 4 3 2 1", "output": "71111111111111111111111111111111111111111111111111\n    \n\nThe output may not fit into a 64-bit integer type.\n\n* * *"}, {"input": "15 3\n    5 4 6", "output": "654"}]