import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
d=dict()
need=[0,2,5,5,4,5,6,3,7,6]
for i in range(m):
    if need[a[i]] not in d:
        d[need[a[i]]]=a[i]
    else:
        if a[i]>d[need[a[i]]]:
            d[need[a[i]]]=a[i]
d=list(d.items())
d.sort()
m=len(d)
dp=[[-1]*10 for _ in range(n+1)]
dp[0]=[0]*10
for i in range(n+1):
    for j in range(m):
        if i-d[j][0]>=0 and dp[i-d[j][0]][0]!=-1:
            if dp[i][0]==dp[i-d[j][0]][0]+1:
                ok=False
                for k in reversed(range(1,10)):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
            elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1
ans=str()
for i in reversed(range(1,10)):
    ans+=str(i)*dp[-1][i]
print(ans)
#print(d)
#print(dp[-1]) import sys sys input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 d=dict() d dict() dict need=[0,2,5,5,4,5,6,3,7,6] need [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 for i in range(m):
    if need[a[i]] not in d:
        d[need[a[i]]]=a[i]
    else:
        if a[i]>d[need[a[i]]]:
            d[need[a[i]]]=a[i] i range(m) range m if need[a[i]] not in d:
        d[need[a[i]]]=a[i]
    else:
        if a[i]>d[need[a[i]]]:
            d[need[a[i]]]=a[i] need[a[i]] not in d need[a[i]] need a[i] a i d d[need[a[i]]]=a[i] d[need[a[i]]] d need[a[i]] need a[i] a i a[i] a i if a[i]>d[need[a[i]]]:
            d[need[a[i]]]=a[i] a[i]>d[need[a[i]]] a[i] a i d[need[a[i]]] d need[a[i]] need a[i] a i d[need[a[i]]]=a[i] d[need[a[i]]] d need[a[i]] need a[i] a i a[i] a i d=list(d.items()) d list(d.items()) list d.items() d.items d items d.sort() d.sort d sort m=len(d) m len(d) len d [-1]*10 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [-1]*10 [-1] -1 10 dp=[[-1]*10 for _ in range(n+1)] dp [[-1]*10 for _ in range(n+1)] dp[0]=[0]*10 dp[0] dp 0 [0]*10 [0] 0 10 for i in range(n+1):
    for j in range(m):
        if i-d[j][0]>=0 and dp[i-d[j][0]][0]!=-1:
            if dp[i][0]==dp[i-d[j][0]][0]+1:
                ok=False
                for k in reversed(range(1,10)):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
            elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1 i range(n+1) range n+1 n 1 for j in range(m):
        if i-d[j][0]>=0 and dp[i-d[j][0]][0]!=-1:
            if dp[i][0]==dp[i-d[j][0]][0]+1:
                ok=False
                for k in reversed(range(1,10)):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
            elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1 j range(m) range m if i-d[j][0]>=0 and dp[i-d[j][0]][0]!=-1:
            if dp[i][0]==dp[i-d[j][0]][0]+1:
                ok=False
                for k in reversed(range(1,10)):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
            elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1 i-d[j][0]>=0 and dp[i-d[j][0]][0]!=-1 i-d[j][0]>=0 i-d[j][0] i d[j][0] [j] d j 0 0 dp[i-d[j][0]][0]!=-1 dp[i-d[j][0]][0] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 0 -1 if dp[i][0]==dp[i-d[j][0]][0]+1:
                ok=False
                for k in reversed(range(1,10)):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
            elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1 dp[i][0]==dp[i-d[j][0]][0]+1 dp[i][0] [i] dp i 0 dp[i-d[j][0]][0]+1 dp[i-d[j][0]][0] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 0 1 ok=False ok False for k in reversed(range(1,10)):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                 k reversed(range(1,10)) reversed range(1,10) range 1 10 if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                 k==d[j][1] k d[j][1] [j] d j 1 if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                         dp[i][k]<dp[i-d[j][0]][k]+1 dp[i][k] [i] dp i k dp[i-d[j][0]][k]+1 dp[i-d[j][0]][k] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 k 1 ok=True ok True break if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                     dp[i][k]>dp[i-d[j][0]][k]+1 dp[i][k] [i] dp i k dp[i-d[j][0]][k]+1 dp[i-d[j][0]][k] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 k 1 break if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                         dp[i][k]<dp[i-d[j][0]][k] dp[i][k] [i] dp i k dp[i-d[j][0]][k] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 k ok=True ok True break if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                 dp[i][k]>dp[i-d[j][0]][k] dp[i][k] [i] dp i k dp[i-d[j][0]][k] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 k break if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
             ok for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                     k range(10) range 10 dp[i][k]=dp[i-d[j][0]][k] dp[i][k] [i] dp i k dp[i-d[j][0]][k] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 k dp[i][d[j][1]]+=1 dp[i][d[j][1]] [i] dp i d[j][1] [j] d j 1 1 dp[i][0]+=1 dp[i][0] [i] dp i 0 1 elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1 dp[i][0]<dp[i-d[j][0]][0]+1 dp[i][0] [i] dp i 0 dp[i-d[j][0]][0]+1 dp[i-d[j][0]][0] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 0 1 for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                 k range(10) range 10 dp[i][k]=dp[i-d[j][0]][k] dp[i][k] [i] dp i k dp[i-d[j][0]][k] [i-d[j][0]] dp i-d[j][0] i d[j][0] [j] d j 0 k dp[i][d[j][1]]+=1 dp[i][d[j][1]] [i] dp i d[j][1] [j] d j 1 1 dp[i][0]+=1 dp[i][0] [i] dp i 0 1 ans=str() ans str() str for i in reversed(range(1,10)):
    ans+=str(i)*dp[-1][i] i reversed(range(1,10)) reversed range(1,10) range 1 10 ans+=str(i)*dp[-1][i] ans str(i)*dp[-1][i] str(i) str i dp[-1][i] [-1] dp -1 i print(ans) print ans ans=str() str() ans need=[0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] need input=sys.stdin.readline sys.stdin.readline input ok=False False ok d=dict() dict() d n,m=map(int,input().split()) map(int,input().split()) n ok=True True ok ans+=str(i)*dp[-1][i] str(i)*dp[-1][i] ans d=list(d.items()) list(d.items()) d m=len(d) len(d) m m=map(int,input().split()) map(int,input().split()) m dp=[[-1]*10 for _ in range(n+1)] [[-1]*10 for _ in range(n+1)] dp a=list(map(int,input().split())) list(map(int,input().split())) a ok=True True ok
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=list(map(int,input().split()))
d=dict()
need=[0,2,5,5,4,5,6,3,7,6]
for i in range(m):
    if need[a[i]] not in d:
        d[need[a[i]]]=a[i]
    else:
        if a[i]>d[need[a[i]]]:
            d[need[a[i]]]=a[i]
d=list(d.items())
d.sort()
m=len(d)
dp=[[-1]*10 for _ in range(n+1)]
dp[0]=[0]*10
for i in range(n+1):
    for j in range(m):
        if i-d[j][0]>=0 and dp[i-d[j][0]][0]!=-1:
            if dp[i][0]==dp[i-d[j][0]][0]+1:
                ok=False
                for k in reversed(range(1,10)):
                    if k==d[j][1]:
                        if dp[i][k]<dp[i-d[j][0]][k]+1:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]+1:
                            break
                    else:
                        if dp[i][k]<dp[i-d[j][0]][k]:
                            ok=True
                            break
                        if dp[i][k]>dp[i-d[j][0]][k]:
                            break
                if ok:
                    for k in range(10):
                        dp[i][k]=dp[i-d[j][0]][k]
                    dp[i][d[j][1]]+=1
                    dp[i][0]+=1
            elif dp[i][0]<dp[i-d[j][0]][0]+1:
                for k in range(10):
                    dp[i][k]=dp[i-d[j][0]][k]
                dp[i][d[j][1]]+=1
                dp[i][0]+=1
ans=str()
for i in reversed(range(1,10)):
    ans+=str(i)*dp[-1][i]
print(ans)
#print(d)
#print(dp[-1])