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

T=[0,2,5,5,4,5,6,3,7,6]
B=[]
for i in A:
    B.append(T[i])
B=sorted(list(set(B)))
dp=[0 for i in range(N+1)]
AB=[[i,T[i]] for i in A]
AB=sorted(AB,reverse=True)

for i in range(1,N+1):
    for j in B:
        if i-j>=0 and dp[i-j]!=-1:
            dp[i]=max(dp[i],dp[i-j]+1)
            #print(i,j,dp[i])
    if dp[i]==0:
        dp[i]=-1
#print(dp)
#print(AB)

c=N
U=[0 for i in range(8)]
while c>0:
    for i in range(len(AB)):
        if c-AB[i][1]>=0 and dp[c]==dp[c-AB[i][1]]+1:
            U[AB[i][1]]+=1
            c-=AB[i][1]
            break
#print(U)

ans=""
if 9 in A:
    ans+='9'*U[6]
    U[6]=0
if 8 in A:
    ans+='8'*U[7]
if 7 in A:
    ans+='7'*U[3]
if 6 in A:
    ans+='6'*U[6]
if 5 in A:
    ans+='5'*U[5]
    U[5]=0
if 4 in A:
    ans+='4'*U[4]
if 3 in A:
    ans+='3'*U[5]
    U[5]=0
if 2 in A:
    ans+='2'*U[5]
if 1 in A:
    ans+='1'*U[2]

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 T=[0,2,5,5,4,5,6,3,7,6] T [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 B=[] B [] for i in A:
    B.append(T[i]) i A B.append(T[i]) B.append B append T[i] T i B=sorted(list(set(B))) B sorted(list(set(B))) sorted list(set(B)) list set(B) set B 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 dp=[0 for i in range(N+1)] dp [0 for i in range(N+1)] [i,T[i]] for i in A for i in A i A for i in A [i,T[i]] i T[i] T i AB=[[i,T[i]] for i in A] AB [[i,T[i]] for i in A] AB=sorted(AB,reverse=True) AB sorted(AB,reverse=True) sorted AB reverse=True reverse True for i in range(1,N+1):
    for j in B:
        if i-j>=0 and dp[i-j]!=-1:
            dp[i]=max(dp[i],dp[i-j]+1)
            #print(i,j,dp[i])
    if dp[i]==0:
        dp[i]=-1
#print(dp)
#print(AB) i range(1,N+1) range 1 N+1 N 1 for j in B:
        if i-j>=0 and dp[i-j]!=-1:
            dp[i]=max(dp[i],dp[i-j]+1)
            #print(i,j,dp[i])
     j B if i-j>=0 and dp[i-j]!=-1:
            dp[i]=max(dp[i],dp[i-j]+1)
            #print(i,j,dp[i])
     i-j>=0 and dp[i-j]!=-1 i-j>=0 i-j i j 0 dp[i-j]!=-1 dp[i-j] dp i-j i j -1 dp[i]=max(dp[i],dp[i-j]+1) dp[i] dp i max(dp[i],dp[i-j]+1) max dp[i] dp i dp[i-j]+1 dp[i-j] dp i-j i j 1 if dp[i]==0:
        dp[i]=-1
#print(dp)
#print(AB) dp[i]==0 dp[i] dp i 0 dp[i]=-1 dp[i] dp i -1 c=N c N 0 for i in range(8) for i in range(8) i range(8) range 8 for i in range(8) 0 U=[0 for i in range(8)] U [0 for i in range(8)] while c>0:
    for i in range(len(AB)):
        if c-AB[i][1]>=0 and dp[c]==dp[c-AB[i][1]]+1:
            U[AB[i][1]]+=1
            c-=AB[i][1]
            break
#print(U) c>0 c 0 for i in range(len(AB)):
        if c-AB[i][1]>=0 and dp[c]==dp[c-AB[i][1]]+1:
            U[AB[i][1]]+=1
            c-=AB[i][1]
            break
#print(U) i range(len(AB)) range len(AB) len AB if c-AB[i][1]>=0 and dp[c]==dp[c-AB[i][1]]+1:
            U[AB[i][1]]+=1
            c-=AB[i][1]
            break
#print(U) c-AB[i][1]>=0 and dp[c]==dp[c-AB[i][1]]+1 c-AB[i][1]>=0 c-AB[i][1] c AB[i][1] [i] AB i 1 0 dp[c]==dp[c-AB[i][1]]+1 dp[c] dp c dp[c-AB[i][1]]+1 dp[c-AB[i][1]] dp c-AB[i][1] c AB[i][1] [i] AB i 1 1 U[AB[i][1]]+=1 U[AB[i][1]] U AB[i][1] [i] AB i 1 1 c-=AB[i][1] c AB[i][1] [i] AB i 1 break ans="" ans "" if 9 in A:
    ans+='9'*U[6]
    U[6]=0 9 in A 9 A ans+='9'*U[6] ans '9'*U[6] '9' U[6] U 6 U[6]=0 U[6] U 6 0 if 8 in A:
    ans+='8'*U[7] 8 in A 8 A ans+='8'*U[7] ans '8'*U[7] '8' U[7] U 7 if 7 in A:
    ans+='7'*U[3] 7 in A 7 A ans+='7'*U[3] ans '7'*U[3] '7' U[3] U 3 if 6 in A:
    ans+='6'*U[6] 6 in A 6 A ans+='6'*U[6] ans '6'*U[6] '6' U[6] U 6 if 5 in A:
    ans+='5'*U[5]
    U[5]=0 5 in A 5 A ans+='5'*U[5] ans '5'*U[5] '5' U[5] U 5 U[5]=0 U[5] U 5 0 if 4 in A:
    ans+='4'*U[4] 4 in A 4 A ans+='4'*U[4] ans '4'*U[4] '4' U[4] U 4 if 3 in A:
    ans+='3'*U[5]
    U[5]=0 3 in A 3 A ans+='3'*U[5] ans '3'*U[5] '3' U[5] U 5 U[5]=0 U[5] U 5 0 if 2 in A:
    ans+='2'*U[5] 2 in A 2 A ans+='2'*U[5] ans '2'*U[5] '2' U[5] U 5 if 1 in A:
    ans+='1'*U[2] 1 in A 1 A ans+='1'*U[2] ans '1'*U[2] '1' U[2] U 2 print(ans) print ans N,M=map(int,input().split()) map(int,input().split()) N ans+='7'*U[3] '7'*U[3] ans ans="" "" ans ans+='2'*U[5] '2'*U[5] ans c=N N c ans+='5'*U[5] '5'*U[5] ans ans+='3'*U[5] '3'*U[5] ans B=sorted(list(set(B))) sorted(list(set(B))) B AB=sorted(AB,reverse=True) sorted(AB,reverse=True) AB ans+='6'*U[6] '6'*U[6] ans B=[] [] B ans+='1'*U[2] '1'*U[2] ans dp=[0 for i in range(N+1)] [0 for i in range(N+1)] dp U=[0 for i in range(8)] [0 for i in range(8)] U A=list(map(int,input().split())) list(map(int,input().split())) A M=map(int,input().split()) map(int,input().split()) M T=[0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] T c-=AB[i][1] AB[i][1] c ans+='9'*U[6] '9'*U[6] ans ans+='8'*U[7] '8'*U[7] ans ans+='4'*U[4] '4'*U[4] ans AB=[[i,T[i]] for i in A] [[i,T[i]] for i in A] AB
N,M=map(int,input().split())
A=list(map(int,input().split()))

T=[0,2,5,5,4,5,6,3,7,6]
B=[]
for i in A:
    B.append(T[i])
B=sorted(list(set(B)))
dp=[0 for i in range(N+1)]
AB=[[i,T[i]] for i in A]
AB=sorted(AB,reverse=True)

for i in range(1,N+1):
    for j in B:
        if i-j>=0 and dp[i-j]!=-1:
            dp[i]=max(dp[i],dp[i-j]+1)
            #print(i,j,dp[i])
    if dp[i]==0:
        dp[i]=-1
#print(dp)
#print(AB)

c=N
U=[0 for i in range(8)]
while c>0:
    for i in range(len(AB)):
        if c-AB[i][1]>=0 and dp[c]==dp[c-AB[i][1]]+1:
            U[AB[i][1]]+=1
            c-=AB[i][1]
            break
#print(U)

ans=""
if 9 in A:
    ans+='9'*U[6]
    U[6]=0
if 8 in A:
    ans+='8'*U[7]
if 7 in A:
    ans+='7'*U[3]
if 6 in A:
    ans+='6'*U[6]
if 5 in A:
    ans+='5'*U[5]
    U[5]=0
if 4 in A:
    ans+='4'*U[4]
if 3 in A:
    ans+='3'*U[5]
    U[5]=0
if 2 in A:
    ans+='2'*U[5]
if 1 in A:
    ans+='1'*U[2]

print(ans)