def check(i):
    global kmax,nmax
    cnt = 0
    for j in range(i):
        cnt += K[j]*B[j]
    if sum(B[:i])+(N-cnt)//K[i]<kmax:
        return
    for k in range((N-cnt)//K[i],-1,-1):
        if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return
C = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
N,M = map(int,input().split())
A = list(map(int,input().split()))
G = {}
for i in range(M):
    a = A[i]
    k = C[a]
    if k not in G:
        G[k] = 0
    if a>G[k]:
        G[k] = a
K = sorted(list(G.keys()))
D = sorted(list(G.values()),reverse=True)
B = [0 for _ in range(len(K))]
kmax = 0
nmax = 0
check(0)
print(nmax) def check(i):
    global kmax,nmax
    cnt = 0
    for j in range(i):
        cnt += K[j]*B[j]
    if sum(B[:i])+(N-cnt)//K[i]<kmax:
        return
    for k in range((N-cnt)//K[i],-1,-1):
        if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return check global kmax,nmax cnt = 0 cnt 0 for j in range(i):
        cnt += K[j]*B[j]
     j range(i) range i cnt += K[j]*B[j] cnt K[j]*B[j] K[j] K j B[j] B j if sum(B[:i])+(N-cnt)//K[i]<kmax:
        return
     sum(B[:i])+(N-cnt)//K[i]<kmax sum(B[:i])+(N-cnt)//K[i] sum(B[:i]) sum B[:i] B :i i (N-cnt)//K[i] N-cnt N cnt K[i] K i kmax return for k in range((N-cnt)//K[i],-1,-1):
        if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return k range((N-cnt)//K[i],-1,-1) range (N-cnt)//K[i] N-cnt N cnt K[i] K i -1 -1 if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return cnt+K[i]*k==N cnt+K[i]*k cnt K[i]*k K[i] K i k N if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
         kmax<=sum(B[:i])+k kmax sum(B[:i])+k sum(B[:i]) sum B[:i] B :i i k kmax = sum(B[:i])+k kmax sum(B[:i])+k sum(B[:i]) sum B[:i] B :i i k B[i] = k B[i] B i k num = "" num "" for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                     j range(len(K)) range len(K) len K num += str(D[j])*B[K.index(C[D[j]])] num str(D[j])*B[K.index(C[D[j]])] str(D[j]) str D[j] D j B[K.index(C[D[j]])] B K.index(C[D[j]]) K.index K index C[D[j]] C D[j] D j num = int(num) num int(num) int num nmax = max(nmax,num) nmax max(nmax,num) max nmax num if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return i<len(K)-1 i len(K)-1 len(K) len K 1 B[i] = k B[i] B i k check(i+1) check i+1 i 1 B[i] = 0 B[i] B i 0 return i i C = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} C {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 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 G = {} G {} for i in range(M):
    a = A[i]
    k = C[a]
    if k not in G:
        G[k] = 0
    if a>G[k]:
        G[k] = a i range(M) range M a = A[i] a A[i] A i k = C[a] k C[a] C a if k not in G:
        G[k] = 0
     k not in G k G G[k] = 0 G[k] G k 0 if a>G[k]:
        G[k] = a a>G[k] a G[k] G k G[k] = a G[k] G k a K = sorted(list(G.keys())) K sorted(list(G.keys())) sorted list(G.keys()) list G.keys() G.keys G keys D = sorted(list(G.values()),reverse=True) D sorted(list(G.values()),reverse=True) sorted list(G.values()) list G.values() G.values G values reverse=True reverse True 0 for _ in range(len(K)) for _ in range(len(K)) _ range(len(K)) range len(K) len K for _ in range(len(K)) 0 B = [0 for _ in range(len(K))] B [0 for _ in range(len(K))] kmax = 0 kmax 0 nmax = 0 nmax 0 check(0) check 0 print(nmax) print nmax kmax = 0 0 kmax K = sorted(list(G.keys())) sorted(list(G.keys())) K N,M = map(int,input().split()) map(int,input().split()) N def check(i):
    global kmax,nmax
    cnt = 0
    for j in range(i):
        cnt += K[j]*B[j]
    if sum(B[:i])+(N-cnt)//K[i]<kmax:
        return
    for k in range((N-cnt)//K[i],-1,-1):
        if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return def check(i):
    global kmax,nmax
    cnt = 0
    for j in range(i):
        cnt += K[j]*B[j]
    if sum(B[:i])+(N-cnt)//K[i]<kmax:
        return
    for k in range((N-cnt)//K[i],-1,-1):
        if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return check G = {} {} G k = C[a] C[a] k D = sorted(list(G.values()),reverse=True) sorted(list(G.values()),reverse=True) D C = {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} C M = map(int,input().split()) map(int,input().split()) M a = A[i] A[i] a A = list(map(int,input().split())) list(map(int,input().split())) A B = [0 for _ in range(len(K))] [0 for _ in range(len(K))] B nmax = 0 0 nmax
def check(i):
    global kmax,nmax
    cnt = 0
    for j in range(i):
        cnt += K[j]*B[j]
    if sum(B[:i])+(N-cnt)//K[i]<kmax:
        return
    for k in range((N-cnt)//K[i],-1,-1):
        if cnt+K[i]*k==N:
             if kmax<=sum(B[:i])+k:
                    kmax = sum(B[:i])+k
                    B[i] = k
                    num = ""
                    for j in range(len(K)):
                        num += str(D[j])*B[K.index(C[D[j]])]
                    num = int(num)
                    nmax = max(nmax,num)
        else:
            if i<len(K)-1:
                B[i] = k
                check(i+1)
            else:
                B[i] = 0
                return
C = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
N,M = map(int,input().split())
A = list(map(int,input().split()))
G = {}
for i in range(M):
    a = A[i]
    k = C[a]
    if k not in G:
        G[k] = 0
    if a>G[k]:
        G[k] = a
K = sorted(list(G.keys()))
D = sorted(list(G.values()),reverse=True)
B = [0 for _ in range(len(K))]
kmax = 0
nmax = 0
check(0)
print(nmax)