def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[-n-n]*(n+1)
    dp[0]=0
    ans=[]
    for i in range(1,n+1):
        for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
    cnt=n
    while cnt:
        for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    for j in ans:
        print(j, end="")
    print("")

if __name__ == '__main__':
    main() def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[-n-n]*(n+1)
    dp[0]=0
    ans=[]
    for i in range(1,n+1):
        for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
    cnt=n
    while cnt:
        for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    for j in ans:
        print(j, end="")
    print("") main 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 cost=[0,2,5,5,4,5,6,3,7,6] cost [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 dp=[-n-n]*(n+1) dp [-n-n]*(n+1) [-n-n] -n-n -n n n n+1 n 1 dp[0]=0 dp[0] dp 0 0 ans=[] ans [] for i in range(1,n+1):
        for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
     i range(1,n+1) range 1 n+1 n 1 for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
     j a if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
     i>=cost[j] i cost[j] cost j dp[i]=max(dp[i],dp[i-cost[j]]+1) dp[i] dp i max(dp[i],dp[i-cost[j]]+1) max dp[i] dp i dp[i-cost[j]]+1 dp[i-cost[j]] dp i-cost[j] i cost[j] cost j 1 cnt=n cnt n while cnt:
        for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
     cnt for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
     j a if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
     cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1 cnt>=cost[j] cnt cost[j] cost j dp[cnt-cost[j]]==dp[cnt]-1 dp[cnt-cost[j]] dp cnt-cost[j] cnt cost[j] cost j dp[cnt]-1 dp[cnt] dp cnt 1 ans.append(j) ans.append ans append j cnt-=cost[j] cnt cost[j] cost j break for j in ans:
        print(j, end="")
     j ans print(j, end="") print j end="" end "" print("") print "" if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[-n-n]*(n+1)
    dp[0]=0
    ans=[]
    for i in range(1,n+1):
        for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
    cnt=n
    while cnt:
        for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    for j in ans:
        print(j, end="")
    print("") def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[-n-n]*(n+1)
    dp[0]=0
    ans=[]
    for i in range(1,n+1):
        for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
    cnt=n
    while cnt:
        for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    for j in ans:
        print(j, end="")
    print("") main
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort(reverse=True)
    cost=[0,2,5,5,4,5,6,3,7,6]
    dp=[-n-n]*(n+1)
    dp[0]=0
    ans=[]
    for i in range(1,n+1):
        for j in a:
            if i>=cost[j]:
                dp[i]=max(dp[i],dp[i-cost[j]]+1)
    cnt=n
    while cnt:
        for j in a:
            if cnt>=cost[j] and dp[cnt-cost[j]]==dp[cnt]-1:
                ans.append(j)
                cnt-=cost[j]
                break
    for j in ans:
        print(j, end="")
    print("")

if __name__ == '__main__':
    main()