N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
INF = float("inf")
num = [INF,2,5,5,4,5,6,3,7,6]
if N==2 and M==9:
    print(1)
    exit()

dp = [0]+[-INF]*N
#dpテーブルを作成
for i in range(1,N+1):
    ins = -INF
    for a in A:
        try:
            tmp = dp[i-num[a]]+1
            ins = max(ins,tmp)
        except:
            continue
    dp[i] = ins

#各桁の数字を決定
A.sort(reverse=True)
digit = ["0"]*dp[N]
mat = 0
for i in range(dp[N]):
    for a in A:
        try:
            t1 = dp[N-mat-num[a]]
            t2 = dp[N-mat]-1
        except:
            continue
        if t1 == t2:
            digit[i] = str(a)
            mat += num[a]
            break
print("".join(digit)) 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() A.sort A sort INF = float("inf") INF float("inf") float "inf" num = [INF,2,5,5,4,5,6,3,7,6] num [INF,2,5,5,4,5,6,3,7,6] INF 2 5 5 4 5 6 3 7 6 if N==2 and M==9:
    print(1)
    exit() N==2 and M==9 N==2 N 2 M==9 M 9 print(1) print 1 exit() exit dp = [0]+[-INF]*N dp [0]+[-INF]*N [0] 0 [-INF]*N [-INF] -INF INF N for i in range(1,N+1):
    ins = -INF
    for a in A:
        try:
            tmp = dp[i-num[a]]+1
            ins = max(ins,tmp)
        except:
            continue
    dp[i] = ins

#各桁の数字を決定 i range(1,N+1) range 1 N+1 N 1 ins = -INF ins -INF INF for a in A:
        try:
            tmp = dp[i-num[a]]+1
            ins = max(ins,tmp)
        except:
            continue
     a A try:
            tmp = dp[i-num[a]]+1
            ins = max(ins,tmp)
        except:
            continue
     except:
            continue
     continue tmp = dp[i-num[a]]+1 tmp dp[i-num[a]]+1 dp[i-num[a]] dp i-num[a] i num[a] num a 1 ins = max(ins,tmp) ins max(ins,tmp) max ins tmp dp[i] = ins dp[i] dp i ins A.sort(reverse=True) A.sort A sort reverse=True reverse True digit = ["0"]*dp[N] digit ["0"]*dp[N] ["0"] "0" dp[N] dp N mat = 0 mat 0 for i in range(dp[N]):
    for a in A:
        try:
            t1 = dp[N-mat-num[a]]
            t2 = dp[N-mat]-1
        except:
            continue
        if t1 == t2:
            digit[i] = str(a)
            mat += num[a]
            break i range(dp[N]) range dp[N] dp N for a in A:
        try:
            t1 = dp[N-mat-num[a]]
            t2 = dp[N-mat]-1
        except:
            continue
        if t1 == t2:
            digit[i] = str(a)
            mat += num[a]
            break a A try:
            t1 = dp[N-mat-num[a]]
            t2 = dp[N-mat]-1
        except:
            continue
         except:
            continue
         continue t1 = dp[N-mat-num[a]] t1 dp[N-mat-num[a]] dp N-mat-num[a] N-mat N mat num[a] num a t2 = dp[N-mat]-1 t2 dp[N-mat]-1 dp[N-mat] dp N-mat N mat 1 if t1 == t2:
            digit[i] = str(a)
            mat += num[a]
            break t1 == t2 t1 t2 digit[i] = str(a) digit[i] digit i str(a) str a mat += num[a] mat num[a] num a break print("".join(digit)) print "".join(digit) "".join "" join digit digit = ["0"]*dp[N] ["0"]*dp[N] digit ins = -INF -INF ins t2 = dp[N-mat]-1 dp[N-mat]-1 t2 mat = 0 0 mat M = map(int,input().split()) map(int,input().split()) M num = [INF,2,5,5,4,5,6,3,7,6] [INF,2,5,5,4,5,6,3,7,6] num A = list(map(int,input().split())) list(map(int,input().split())) A dp = [0]+[-INF]*N [0]+[-INF]*N dp N,M = map(int,input().split()) map(int,input().split()) N t1 = dp[N-mat-num[a]] dp[N-mat-num[a]] t1 tmp = dp[i-num[a]]+1 dp[i-num[a]]+1 tmp ins = max(ins,tmp) max(ins,tmp) ins INF = float("inf") float("inf") INF mat += num[a] num[a] mat
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
INF = float("inf")
num = [INF,2,5,5,4,5,6,3,7,6]
if N==2 and M==9:
    print(1)
    exit()

dp = [0]+[-INF]*N
#dpテーブルを作成
for i in range(1,N+1):
    ins = -INF
    for a in A:
        try:
            tmp = dp[i-num[a]]+1
            ins = max(ins,tmp)
        except:
            continue
    dp[i] = ins

#各桁の数字を決定
A.sort(reverse=True)
digit = ["0"]*dp[N]
mat = 0
for i in range(dp[N]):
    for a in A:
        try:
            t1 = dp[N-mat-num[a]]
            t2 = dp[N-mat]-1
        except:
            continue
        if t1 == t2:
            digit[i] = str(a)
            mat += num[a]
            break
print("".join(digit))
