n,m=map(int,input().split())
A=list(map(int,input().split()))
weight=[0,2,5,5,4,5,6,3,7,6]
dp=[-1]*(n+1)
dp[0]=0
for i in range(n+1):
    for a in A:
        if i+weight[a]<=n:
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a)
print(dp[n])
'''
    dic={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    a=sorted(list(map(int,input().split())),key=lambda x:(dic[x],-x))
    b=[0]*10
    i=0
    while(n!=0):
        b[a[i]]+=1
        n-=dic[a[i]]
        if n<dic[a[i]] and n!=0:
            while(n<dic[a[i+1]]):
                b[a[i]]-=1
                n+=dic[a[i]]
            i+=1
    print(a)
    print(b)
''' 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 weight=[0,2,5,5,4,5,6,3,7,6] weight [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 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(n+1):
    for a in A:
        if i+weight[a]<=n:
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a) i range(n+1) range n+1 n 1 for a in A:
        if i+weight[a]<=n:
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a) a A if i+weight[a]<=n:
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a) i+weight[a]<=n i+weight[a] i weight[a] weight a n dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a) dp[i+weight[a]] dp i+weight[a] i weight[a] weight a max(dp[i+weight[a]],dp[i]*10+a) max dp[i+weight[a]] dp i+weight[a] i weight[a] weight a dp[i]*10+a dp[i]*10 dp[i] dp i 10 a print(dp[n]) print dp[n] dp n '''
    dic={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    a=sorted(list(map(int,input().split())),key=lambda x:(dic[x],-x))
    b=[0]*10
    i=0
    while(n!=0):
        b[a[i]]+=1
        n-=dic[a[i]]
        if n<dic[a[i]] and n!=0:
            while(n<dic[a[i+1]]):
                b[a[i]]-=1
                n+=dic[a[i]]
            i+=1
    print(a)
    print(b)
''' m=map(int,input().split()) map(int,input().split()) m weight=[0,2,5,5,4,5,6,3,7,6] [0,2,5,5,4,5,6,3,7,6] weight dp=[-1]*(n+1) [-1]*(n+1) dp n,m=map(int,input().split()) map(int,input().split()) n A=list(map(int,input().split())) list(map(int,input().split())) A
n,m=map(int,input().split())
A=list(map(int,input().split()))
weight=[0,2,5,5,4,5,6,3,7,6]
dp=[-1]*(n+1)
dp[0]=0
for i in range(n+1):
    for a in A:
        if i+weight[a]<=n:
            dp[i+weight[a]]=max(dp[i+weight[a]],dp[i]*10+a)
print(dp[n])
'''
    dic={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
    a=sorted(list(map(int,input().split())),key=lambda x:(dic[x],-x))
    b=[0]*10
    i=0
    while(n!=0):
        b[a[i]]+=1
        n-=dic[a[i]]
        if n<dic[a[i]] and n!=0:
            while(n<dic[a[i+1]]):
                b[a[i]]-=1
                n+=dic[a[i]]
            i+=1
    print(a)
    print(b)
'''