import math

n,_,*a=map(int,open(0).read().split())
match={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
a=sorted(a)

num=[0]*8

for d in a:
    num[match[d]]=d

dig=set(num)
dig.remove(0)
dig=sorted(list(dig),reverse=True)

dp=[-10**10]*(n+1)
dp[0]=0

for i in range(n+1):
    for d in dig:
        if i-match[d]<0:
            continue

        dp[i]=max(dp[i],dp[i-match[d]]+1)

ans=''

i=n
while i>0:
    for d in dig:
        if i-match[d]<0:
            continue
        if dp[i]==dp[i-match[d]]+1:
            ans=ans+str(d)
            break
    
    i-=match[d]

print(ans) import math math n,_,*a=map(int,open(0).read().split()) n map(int,open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split _ *a a match={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} match {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 a=sorted(a) a sorted(a) sorted a num=[0]*8 num [0]*8 [0] 0 8 for d in a:
    num[match[d]]=d d a num[match[d]]=d num[match[d]] num match[d] match d d dig=set(num) dig set(num) set num dig.remove(0) dig.remove dig remove 0 dig=sorted(list(dig),reverse=True) dig sorted(list(dig),reverse=True) sorted list(dig) list dig reverse=True reverse True dp=[-10**10]*(n+1) dp [-10**10]*(n+1) [-10**10] -10**10 -10 10 n+1 n 1 dp[0]=0 dp[0] dp 0 0 for i in range(n+1):
    for d in dig:
        if i-match[d]<0:
            continue

        dp[i]=max(dp[i],dp[i-match[d]]+1) i range(n+1) range n+1 n 1 for d in dig:
        if i-match[d]<0:
            continue

        dp[i]=max(dp[i],dp[i-match[d]]+1) d dig if i-match[d]<0:
            continue

         i-match[d]<0 i-match[d] i match[d] match d 0 continue dp[i]=max(dp[i],dp[i-match[d]]+1) dp[i] dp i max(dp[i],dp[i-match[d]]+1) max dp[i] dp i dp[i-match[d]]+1 dp[i-match[d]] dp i-match[d] i match[d] match d 1 ans='' ans '' i=n i n while i>0:
    for d in dig:
        if i-match[d]<0:
            continue
        if dp[i]==dp[i-match[d]]+1:
            ans=ans+str(d)
            break
    
    i-=match[d] i>0 i 0 for d in dig:
        if i-match[d]<0:
            continue
        if dp[i]==dp[i-match[d]]+1:
            ans=ans+str(d)
            break
    
     d dig if i-match[d]<0:
            continue
         i-match[d]<0 i-match[d] i match[d] match d 0 continue if dp[i]==dp[i-match[d]]+1:
            ans=ans+str(d)
            break
    
     dp[i]==dp[i-match[d]]+1 dp[i] dp i dp[i-match[d]]+1 dp[i-match[d]] dp i-match[d] i match[d] match d 1 ans=ans+str(d) ans ans+str(d) ans str(d) str d break i-=match[d] i match[d] match d print(ans) print ans _,*a=map(int,open(0).read().split()) map(int,open(0).read().split()) _ a=sorted(a) sorted(a) a dig=set(num) set(num) dig match={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} match ans='' '' ans dig=sorted(list(dig),reverse=True) sorted(list(dig),reverse=True) dig i-=match[d] match[d] i num=[0]*8 [0]*8 num dp=[-10**10]*(n+1) [-10**10]*(n+1) dp i=n n i ans=ans+str(d) ans+str(d) ans n,_,*a=map(int,open(0).read().split()) map(int,open(0).read().split()) n
import math

n,_,*a=map(int,open(0).read().split())
match={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
a=sorted(a)

num=[0]*8

for d in a:
    num[match[d]]=d

dig=set(num)
dig.remove(0)
dig=sorted(list(dig),reverse=True)

dp=[-10**10]*(n+1)
dp[0]=0

for i in range(n+1):
    for d in dig:
        if i-match[d]<0:
            continue

        dp[i]=max(dp[i],dp[i-match[d]]+1)

ans=''

i=n
while i>0:
    for d in dig:
        if i-match[d]<0:
            continue
        if dp[i]==dp[i-match[d]]+1:
            ans=ans+str(d)
            break
    
    i-=match[d]

print(ans)