def main():  
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  dp=[2,5,5,4,5,6,3,7,6]
  p=[]
  q=[]
  for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
  bp=[[0,0]for i in range(N+1)]
  for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
  print(bp[N][0])
if __name__=='__main__':
  main() def main():  
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  dp=[2,5,5,4,5,6,3,7,6]
  p=[]
  q=[]
  for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
  bp=[[0,0]for i in range(N+1)]
  for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
  print(bp[N][0]) main N,M=map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split M A=list(map(int,input().split())) A list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split dp=[2,5,5,4,5,6,3,7,6] dp [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 p=[] p [] q=[] q [] for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
   i range(M) range M p.append(dp[A[i]-1]) p.append p append dp[A[i]-1] dp A[i]-1 A[i] A i 1 q.append(A[i]) q.append q append A[i] A i [0,0]for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) [0,0] 0 0 bp=[[0,0]for i in range(N+1)] bp [[0,0]for i in range(N+1)] for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
   i range(N-1) range N-1 N 1 for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
   j range(len(p)) range len(p) len p if i+p[j]>N:
        continue
       i+p[j]>N i+p[j] i p[j] p j N continue if bp[i][0]==0:
          if i!=0:
              continue
       bp[i][0]==0 bp[i][0] [i] bp i 0 0 if i!=0:
              continue
       i!=0 i 0 continue bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j]) bp[i+p[j]][0] [i+p[j]] bp i+p[j] i p[j] p j 0 max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j]) max bp[i+p[j]][0] [i+p[j]] bp i+p[j] i p[j] p j 0 bp[i][0]+(10**bp[i][1])*q[j] bp[i][0] [i] bp i 0 (10**bp[i][1])*q[j] 10**bp[i][1] 10 bp[i][1] [i] bp i 1 q[j] q j bp[i+p[j]][1]=bp[i][1]+1 bp[i+p[j]][1] [i+p[j]] bp i+p[j] i p[j] p j 1 bp[i][1]+1 bp[i][1] [i] bp i 1 1 print(bp[N][0]) print bp[N][0] [N] bp N 0 if __name__=='__main__':
  main() __name__=='__main__' __name__ '__main__' main() main def main():  
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  dp=[2,5,5,4,5,6,3,7,6]
  p=[]
  q=[]
  for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
  bp=[[0,0]for i in range(N+1)]
  for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
  print(bp[N][0]) def main():  
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  dp=[2,5,5,4,5,6,3,7,6]
  p=[]
  q=[]
  for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
  bp=[[0,0]for i in range(N+1)]
  for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
  print(bp[N][0]) main
def main():  
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  dp=[2,5,5,4,5,6,3,7,6]
  p=[]
  q=[]
  for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
  bp=[[0,0]for i in range(N+1)]
  for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
  print(bp[N][0])
if __name__=='__main__':
  main()