import queue
from collections import defaultdict
import copy

N,M=map(int,input().split())
A=list(map(int,input().split()))
ma=[2,5,5,4,5,6,3,7,6]
BA=[[0,0] for _ in range(M)] #(必要本数，-1*数字)
for i in range(M):
    BA[i][1]=-1*A[i]
    BA[i][0]=ma[A[i]-1]
    
BA.sort()

#同じ本数使うものを消去
can=[]
ii=0
while ii <len(BA):
    if BA[ii][0] in can:
        BA.pop(ii)
    else:
        can.append(BA[ii][0])
        ii+=1
M=len(BA)#Mの書き換え

diff=[0]*M#数字変更の際に追加で必要な本数
for i in range(M):
    diff[i]=BA[i][0]-BA[0][0]
    BA[i][1]=BA[i][1]*-1#(必要本数，数字)に直しておく

fi=BA[0]###
#BA.sort(key=lambda x: x[1]*-1)#数字の大きいもの順


    
p=[1]*(N+1)
for i in range(N):
    p[i+1]=p[i]*10

#数字のセットL(sort済み)から作れる最大の数値を出力
def set_to_num(L):
    ans=0
    for i in range(len(L)):
        ans+=L[i]*p[i]
    return ans

#2つの配列を合わせる
def make_set(temp,L):
    ori=copy.deepcopy(temp)
    for i in range(len(L)):
        ori[i]=L[i]
    ori.sort()
    return ori
    

#dd用hash
def make_h(L):
    h=0
    for i in range(len(L)):
        h+=p[i]*L[i]
    return h
    
    
ans=-1
Max_dig=N//fi[0]

for d in range(Max_dig):#減らす桁数
    dig=Max_dig-d
    rem=N-fi[0]*dig
    temp=[fi[1]]*dig
    dd = defaultdict(int)
    q=queue.Queue()
    q.put((rem,[]))#残り本数，入れ替える数字
    
    while not q.empty():
        rem2,L=q.get()
        if rem2==0:
            #print(temp,L)
            LLL=make_set(temp,L)
            #print(LLL)
            ans=max(ans,set_to_num(LLL))
        else:
            for i in range(1,M):#変更候補の数字を見ていく.0番は自身のためのぞく
                if rem2>=diff[i]:
                    LL=copy.deepcopy(L)
                    LL.append(BA[i][1])
                    if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
    if ans!=-1:
        break
        
print(ans)     import queue queue from collections import defaultdict import copy copy 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 ma=[2,5,5,4,5,6,3,7,6] ma [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 [0,0] for _ in range(M) for _ in range(M) _ range(M) range M for _ in range(M) [0,0] 0 0 BA=[[0,0] for _ in range(M)] BA [[0,0] for _ in range(M)] for i in range(M):
    BA[i][1]=-1*A[i]
    BA[i][0]=ma[A[i]-1]
     i range(M) range M BA[i][1]=-1*A[i] BA[i][1] [i] BA i 1 -1*A[i] -1 A[i] A i BA[i][0]=ma[A[i]-1] BA[i][0] [i] BA i 0 ma[A[i]-1] ma A[i]-1 A[i] A i 1 BA.sort() BA.sort BA sort can=[] can [] ii=0 ii 0 while ii <len(BA):
    if BA[ii][0] in can:
        BA.pop(ii)
    else:
        can.append(BA[ii][0])
        ii+=1 ii <len(BA) ii len(BA) len BA if BA[ii][0] in can:
        BA.pop(ii)
    else:
        can.append(BA[ii][0])
        ii+=1 BA[ii][0] in can BA[ii][0] [ii] BA ii 0 can BA.pop(ii) BA.pop BA pop ii can.append(BA[ii][0]) can.append can append BA[ii][0] [ii] BA ii 0 ii+=1 ii 1 M=len(BA) M len(BA) len BA diff=[0]*M diff [0]*M [0] 0 M for i in range(M):
    diff[i]=BA[i][0]-BA[0][0]
    BA[i][1]=BA[i][1]*-1#(必要本数，数字)に直しておく i range(M) range M diff[i]=BA[i][0]-BA[0][0] diff[i] diff i BA[i][0]-BA[0][0] BA[i][0] [i] BA i 0 BA[0][0] [0] BA 0 0 BA[i][1]=BA[i][1]*-1 BA[i][1] [i] BA i 1 BA[i][1]*-1 BA[i][1] [i] BA i 1 -1 fi=BA[0] fi BA[0] BA 0 p=[1]*(N+1) p [1]*(N+1) [1] 1 N+1 N 1 for i in range(N):
    p[i+1]=p[i]*10

#数字のセットL(sort済み)から作れる最大の数値を出力 i range(N) range N p[i+1]=p[i]*10 p[i+1] p i+1 i 1 p[i]*10 p[i] p i 10 def set_to_num(L):
    ans=0
    for i in range(len(L)):
        ans+=L[i]*p[i]
    return ans

#2つの配列を合わせる set_to_num ans=0 ans 0 for i in range(len(L)):
        ans+=L[i]*p[i]
     i range(len(L)) range len(L) len L ans+=L[i]*p[i] ans L[i]*p[i] L[i] L i p[i] p i return ans ans L L def make_set(temp,L):
    ori=copy.deepcopy(temp)
    for i in range(len(L)):
        ori[i]=L[i]
    ori.sort()
    return ori
    

#dd用hash make_set ori=copy.deepcopy(temp) ori copy.deepcopy(temp) copy.deepcopy copy deepcopy temp for i in range(len(L)):
        ori[i]=L[i]
     i range(len(L)) range len(L) len L ori[i]=L[i] ori[i] ori i L[i] L i ori.sort() ori.sort ori sort return ori ori temp temp L L def make_h(L):
    h=0
    for i in range(len(L)):
        h+=p[i]*L[i]
    return h
    
     make_h h=0 h 0 for i in range(len(L)):
        h+=p[i]*L[i]
     i range(len(L)) range len(L) len L h+=p[i]*L[i] h p[i]*L[i] p[i] p i L[i] L i return h h L L ans=-1 ans -1 Max_dig=N//fi[0] Max_dig N//fi[0] N fi[0] fi 0 for d in range(Max_dig):#減らす桁数
    dig=Max_dig-d
    rem=N-fi[0]*dig
    temp=[fi[1]]*dig
    dd = defaultdict(int)
    q=queue.Queue()
    q.put((rem,[]))#残り本数，入れ替える数字
    
    while not q.empty():
        rem2,L=q.get()
        if rem2==0:
            #print(temp,L)
            LLL=make_set(temp,L)
            #print(LLL)
            ans=max(ans,set_to_num(LLL))
        else:
            for i in range(1,M):#変更候補の数字を見ていく.0番は自身のためのぞく
                if rem2>=diff[i]:
                    LL=copy.deepcopy(L)
                    LL.append(BA[i][1])
                    if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
    if ans!=-1:
        break
         d range(Max_dig) range Max_dig dig=Max_dig-d dig Max_dig-d Max_dig d rem=N-fi[0]*dig rem N-fi[0]*dig N fi[0]*dig fi[0] fi 0 dig temp=[fi[1]]*dig temp [fi[1]]*dig [fi[1]] fi[1] fi 1 dig dd = defaultdict(int) dd defaultdict(int) defaultdict int q=queue.Queue() q queue.Queue() queue.Queue queue Queue q.put((rem,[])) q.put q put (rem,[]) rem [] while not q.empty():
        rem2,L=q.get()
        if rem2==0:
            #print(temp,L)
            LLL=make_set(temp,L)
            #print(LLL)
            ans=max(ans,set_to_num(LLL))
        else:
            for i in range(1,M):#変更候補の数字を見ていく.0番は自身のためのぞく
                if rem2>=diff[i]:
                    LL=copy.deepcopy(L)
                    LL.append(BA[i][1])
                    if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
     not q.empty() q.empty() q.empty q empty rem2,L=q.get() rem2 q.get() q.get q get L if rem2==0:
            #print(temp,L)
            LLL=make_set(temp,L)
            #print(LLL)
            ans=max(ans,set_to_num(LLL))
        else:
            for i in range(1,M):#変更候補の数字を見ていく.0番は自身のためのぞく
                if rem2>=diff[i]:
                    LL=copy.deepcopy(L)
                    LL.append(BA[i][1])
                    if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
     rem2==0 rem2 0 LLL=make_set(temp,L) LLL make_set(temp,L) make_set temp L ans=max(ans,set_to_num(LLL)) ans max(ans,set_to_num(LLL)) max ans set_to_num(LLL) set_to_num LLL for i in range(1,M):#変更候補の数字を見ていく.0番は自身のためのぞく
                if rem2>=diff[i]:
                    LL=copy.deepcopy(L)
                    LL.append(BA[i][1])
                    if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
     i range(1,M) range 1 M if rem2>=diff[i]:
                    LL=copy.deepcopy(L)
                    LL.append(BA[i][1])
                    if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
     rem2>=diff[i] rem2 diff[i] diff i LL=copy.deepcopy(L) LL copy.deepcopy(L) copy.deepcopy copy deepcopy L LL.append(BA[i][1]) LL.append LL append BA[i][1] [i] BA i 1 if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
     len(LL)<=len(temp) len(LL) len LL len(temp) len temp LL.sort() LL.sort LL sort h=make_h(LL) h make_h(LL) make_h LL if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
     dd[h]==0 dd[h] dd h 0 q.put((rem2-diff[i],LL)) q.put q put (rem2-diff[i],LL) rem2-diff[i] rem2 diff[i] diff i LL dd[h]=1 dd[h] dd h 1 if ans!=-1:
        break
         ans!=-1 ans -1 break print(ans) print ans ii+=1 1 ii def make_set(temp,L):
    ori=copy.deepcopy(temp)
    for i in range(len(L)):
        ori[i]=L[i]
    ori.sort()
    return ori
    

#dd用hash def make_set(temp,L):
    ori=copy.deepcopy(temp)
    for i in range(len(L)):
        ori[i]=L[i]
    ori.sort()
    return ori
    

#dd用hash make_set A=list(map(int,input().split())) list(map(int,input().split())) A N,M=map(int,input().split()) map(int,input().split()) N Max_dig=N//fi[0] N//fi[0] Max_dig ma=[2,5,5,4,5,6,3,7,6] [2,5,5,4,5,6,3,7,6] ma fi=BA[0] BA[0] fi LLL=make_set(temp,L) make_set(temp,L) LLL temp=[fi[1]]*dig [fi[1]]*dig temp rem2,L=q.get() q.get() rem2 ans=-1 -1 ans can=[] [] can LL=copy.deepcopy(L) copy.deepcopy(L) LL dd = defaultdict(int) defaultdict(int) dd diff=[0]*M [0]*M diff M=map(int,input().split()) map(int,input().split()) M ans=max(ans,set_to_num(LLL)) max(ans,set_to_num(LLL)) ans p=[1]*(N+1) [1]*(N+1) p q=queue.Queue() queue.Queue() q L=q.get() q.get() L dig=Max_dig-d Max_dig-d dig BA=[[0,0] for _ in range(M)] [[0,0] for _ in range(M)] BA ii=0 0 ii M=len(BA) len(BA) M def set_to_num(L):
    ans=0
    for i in range(len(L)):
        ans+=L[i]*p[i]
    return ans

#2つの配列を合わせる def set_to_num(L):
    ans=0
    for i in range(len(L)):
        ans+=L[i]*p[i]
    return ans

#2つの配列を合わせる set_to_num rem=N-fi[0]*dig N-fi[0]*dig rem def make_h(L):
    h=0
    for i in range(len(L)):
        h+=p[i]*L[i]
    return h
    
     def make_h(L):
    h=0
    for i in range(len(L)):
        h+=p[i]*L[i]
    return h
    
     make_h h=make_h(LL) make_h(LL) h
import queue
from collections import defaultdict
import copy

N,M=map(int,input().split())
A=list(map(int,input().split()))
ma=[2,5,5,4,5,6,3,7,6]
BA=[[0,0] for _ in range(M)] #(必要本数，-1*数字)
for i in range(M):
    BA[i][1]=-1*A[i]
    BA[i][0]=ma[A[i]-1]
    
BA.sort()

#同じ本数使うものを消去
can=[]
ii=0
while ii <len(BA):
    if BA[ii][0] in can:
        BA.pop(ii)
    else:
        can.append(BA[ii][0])
        ii+=1
M=len(BA)#Mの書き換え

diff=[0]*M#数字変更の際に追加で必要な本数
for i in range(M):
    diff[i]=BA[i][0]-BA[0][0]
    BA[i][1]=BA[i][1]*-1#(必要本数，数字)に直しておく

fi=BA[0]###
#BA.sort(key=lambda x: x[1]*-1)#数字の大きいもの順


    
p=[1]*(N+1)
for i in range(N):
    p[i+1]=p[i]*10

#数字のセットL(sort済み)から作れる最大の数値を出力
def set_to_num(L):
    ans=0
    for i in range(len(L)):
        ans+=L[i]*p[i]
    return ans

#2つの配列を合わせる
def make_set(temp,L):
    ori=copy.deepcopy(temp)
    for i in range(len(L)):
        ori[i]=L[i]
    ori.sort()
    return ori
    

#dd用hash
def make_h(L):
    h=0
    for i in range(len(L)):
        h+=p[i]*L[i]
    return h
    
    
ans=-1
Max_dig=N//fi[0]

for d in range(Max_dig):#減らす桁数
    dig=Max_dig-d
    rem=N-fi[0]*dig
    temp=[fi[1]]*dig
    dd = defaultdict(int)
    q=queue.Queue()
    q.put((rem,[]))#残り本数，入れ替える数字
    
    while not q.empty():
        rem2,L=q.get()
        if rem2==0:
            #print(temp,L)
            LLL=make_set(temp,L)
            #print(LLL)
            ans=max(ans,set_to_num(LLL))
        else:
            for i in range(1,M):#変更候補の数字を見ていく.0番は自身のためのぞく
                if rem2>=diff[i]:
                    LL=copy.deepcopy(L)
                    LL.append(BA[i][1])
                    if len(LL)<=len(temp):
                        LL.sort()
                        h=make_h(LL)
                        if dd[h]==0:
                            q.put((rem2-diff[i],LL))
                            dd[h]=1
    if ans!=-1:
        break
        
print(ans)    