n,m=map(int,input().split())
a=list(map(int,input().split()))
table={
  1:2,
  2:5,
  3:5,
  4:4,
  5:5,
  6:6,
  7:3,
  8:7,
  9:6,
}
dp=[-1]*(n+1)
dp[0]=0
for i in range(1,n+1):
  mx=-1
  for j in a:
    if not 0 <= i-table[j] < n+1:
      continue
    #print(i,i-table[j],dp[i-table[j]],j)
    if dp[i-table[j]]<0:
      continue
    if mx < dp[i-table[j]]*10+j:
      mx =dp[i-table[j]]*10+j
  dp[i]=mx
print(dp[n]) 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 table={
  1:2,
  2:5,
  3:5,
  4:4,
  5:5,
  6:6,
  7:3,
  8:7,
  9:6,
} table {
  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 dp=[-1]*(n+1) dp [-1]*(n+1) [-1] -1 n+1 n 1 dp[0]=0 dp[0] dp 0 0 for i in range(1,n+1):
  mx=-1
  for j in a:
    if not 0 <= i-table[j] < n+1:
      continue
    #print(i,i-table[j],dp[i-table[j]],j)
    if dp[i-table[j]]<0:
      continue
    if mx < dp[i-table[j]]*10+j:
      mx =dp[i-table[j]]*10+j
  dp[i]=mx i range(1,n+1) range 1 n+1 n 1 mx=-1 mx -1 for j in a:
    if not 0 <= i-table[j] < n+1:
      continue
    #print(i,i-table[j],dp[i-table[j]],j)
    if dp[i-table[j]]<0:
      continue
    if mx < dp[i-table[j]]*10+j:
      mx =dp[i-table[j]]*10+j
   j a if not 0 <= i-table[j] < n+1:
      continue
    #print(i,i-table[j],dp[i-table[j]],j)
     not 0 <= i-table[j] < n+1 0 <= i-table[j] < n+1 0 <= i-table[j] 0 i-table[j] i table[j] table j n+1 n 1 continue if dp[i-table[j]]<0:
      continue
     dp[i-table[j]]<0 dp[i-table[j]] dp i-table[j] i table[j] table j 0 continue if mx < dp[i-table[j]]*10+j:
      mx =dp[i-table[j]]*10+j
   mx < dp[i-table[j]]*10+j mx dp[i-table[j]]*10+j dp[i-table[j]]*10 dp[i-table[j]] dp i-table[j] i table[j] table j 10 j mx =dp[i-table[j]]*10+j mx dp[i-table[j]]*10+j dp[i-table[j]]*10 dp[i-table[j]] dp i-table[j] i table[j] table j 10 j dp[i]=mx dp[i] dp i mx print(dp[n]) print dp[n] dp n n,m=map(int,input().split()) map(int,input().split()) n dp=[-1]*(n+1) [-1]*(n+1) dp a=list(map(int,input().split())) list(map(int,input().split())) a mx =dp[i-table[j]]*10+j dp[i-table[j]]*10+j mx mx=-1 -1 mx table={
  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,
} table m=map(int,input().split()) map(int,input().split()) m
n,m=map(int,input().split())
a=list(map(int,input().split()))
table={
  1:2,
  2:5,
  3:5,
  4:4,
  5:5,
  6:6,
  7:3,
  8:7,
  9:6,
}
dp=[-1]*(n+1)
dp[0]=0
for i in range(1,n+1):
  mx=-1
  for j in a:
    if not 0 <= i-table[j] < n+1:
      continue
    #print(i,i-table[j],dp[i-table[j]],j)
    if dp[i-table[j]]<0:
      continue
    if mx < dp[i-table[j]]*10+j:
      mx =dp[i-table[j]]*10+j
  dp[i]=mx
print(dp[n])