N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort(reverse=True)
honsuu=[0,2,5,5,4,5,6,3,7,6]

def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
  

ans=""
keta=ketadp(N,M,A)
remain=keta[N]
match=N
#print(keta)

while(match>0):
  for i in A:
    #print(i,match,remain,keta[match-honsuu[i]])
    if match-honsuu[i]<0:
      continue;
    if(keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break;
print(ans) 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 A.sort(reverse=True) A.sort A sort reverse=True reverse True honsuu=[0,2,5,5,4,5,6,3,7,6] honsuu [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
   ketadp dp=[0]*(N+1) dp [0]*(N+1) [0] 0 N+1 N 1 for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
   i range(N) range N for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
   j A if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
   (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0) (i+1-honsuu[j])==0 i+1-honsuu[j] i+1 i 1 honsuu[j] honsuu j 0 i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0 i+1-honsuu[j]>0 i+1-honsuu[j] i+1 i 1 honsuu[j] honsuu j 0 dp[i+1-honsuu[j]]!=0 dp[i+1-honsuu[j]] dp i+1-honsuu[j] i+1 i 1 honsuu[j] honsuu j 0 dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1) dp[i+1] dp i+1 i 1 max(dp[i+1],dp[i+1-honsuu[j]]+1) max dp[i+1] dp i+1 i 1 dp[i+1-honsuu[j]]+1 dp[i+1-honsuu[j]] dp i+1-honsuu[j] i+1 i 1 honsuu[j] honsuu j 1 return dp dp N N M M A A ans="" ans "" keta=ketadp(N,M,A) keta ketadp(N,M,A) ketadp N M A remain=keta[N] remain keta[N] keta N match=N match N while(match>0):
  for i in A:
    #print(i,match,remain,keta[match-honsuu[i]])
    if match-honsuu[i]<0:
      continue;
    if(keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break; match>0 match 0 for i in A:
    #print(i,match,remain,keta[match-honsuu[i]])
    if match-honsuu[i]<0:
      continue;
    if(keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break; i A if match-honsuu[i]<0:
      continue;
     match-honsuu[i]<0 match-honsuu[i] match honsuu[i] honsuu i 0 continue if(keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break; (keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1) keta[match-honsuu[i]]==remain-1 keta[match-honsuu[i]] keta match-honsuu[i] match honsuu[i] honsuu i remain-1 remain 1 not (match-honsuu[i]!=0 and remain==1) match-honsuu[i]!=0 and remain==1 match-honsuu[i]!=0 match-honsuu[i] match honsuu[i] honsuu i 0 remain==1 remain 1 ans+=str(i) ans str(i) str i remain-=1 remain 1 match-=honsuu[i] match honsuu[i] honsuu i break print(ans) print ans def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
   def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
   ketadp remain-=1 1 remain match-=honsuu[i] honsuu[i] match N,M=map(int,input().split()) map(int,input().split()) N keta=ketadp(N,M,A) ketadp(N,M,A) keta remain=keta[N] keta[N] remain ans+=str(i) str(i) ans honsuu=[0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] honsuu M=map(int,input().split()) map(int,input().split()) M A=list(map(int,input().split())) list(map(int,input().split())) A ans="" "" ans match=N N match
N,M=map(int,input().split())

A=list(map(int,input().split()))
A.sort(reverse=True)
honsuu=[0,2,5,5,4,5,6,3,7,6]

def ketadp(N,M,A):
  dp=[0]*(N+1)
  for i in range(N):
    for j in A:
      if (i+1-honsuu[j])==0 or (i+1-honsuu[j]>0 and dp[i+1-honsuu[j]]!=0):
        dp[i+1]=max(dp[i+1],dp[i+1-honsuu[j]]+1)
  return dp
  

ans=""
keta=ketadp(N,M,A)
remain=keta[N]
match=N
#print(keta)

while(match>0):
  for i in A:
    #print(i,match,remain,keta[match-honsuu[i]])
    if match-honsuu[i]<0:
      continue;
    if(keta[match-honsuu[i]]==remain-1) and not (match-honsuu[i]!=0 and remain==1):
      ans+=str(i)
      remain-=1
      match-=honsuu[i]
      break;
print(ans)