from bisect import bisect_left
N, M = map(int, input().split())
nums = list(map(int, input().split()))
nums.sort(reverse=True)

matchNum = {
    1 : 2,
    2 : 5,
    3 : 5,
    4 : 4,
    5 : 5,
    6 : 6,
    7 : 3,
    8 : 7,
    9 : 6,
}

makeDigit = [-float('inf')] * (N + 1)
makeDigit[0] = 0
for n in range(1, N + 1):
    d = makeDigit[n]
    for k in nums:
        cnt = n - matchNum[k]
        if cnt >= 0:
            d = max(d, makeDigit[cnt] + 1)
    if makeDigit[n] < d:
        makeDigit[n] = d

ans = []
for d in range(makeDigit[N])[:: -1]:
    for k in nums:
        cnt = N - matchNum[k]
        if cnt >= 0 and makeDigit[cnt] == d:
            ans.append(k)
            N = cnt
            break

ans.sort(reverse=True)
print(int(''.join(map(str, ans)))) from bisect import bisect_left N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M nums = list(map(int, input().split())) nums list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split nums.sort(reverse=True) nums.sort nums sort reverse=True reverse True matchNum = {
    1 : 2,
    2 : 5,
    3 : 5,
    4 : 4,
    5 : 5,
    6 : 6,
    7 : 3,
    8 : 7,
    9 : 6,
} matchNum {
    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 makeDigit = [-float('inf')] * (N + 1) makeDigit [-float('inf')] * (N + 1) [-float('inf')] -float('inf') float('inf') float 'inf' N + 1 N 1 makeDigit[0] = 0 makeDigit[0] makeDigit 0 0 for n in range(1, N + 1):
    d = makeDigit[n]
    for k in nums:
        cnt = n - matchNum[k]
        if cnt >= 0:
            d = max(d, makeDigit[cnt] + 1)
    if makeDigit[n] < d:
        makeDigit[n] = d n range(1, N + 1) range 1 N + 1 N 1 d = makeDigit[n] d makeDigit[n] makeDigit n for k in nums:
        cnt = n - matchNum[k]
        if cnt >= 0:
            d = max(d, makeDigit[cnt] + 1)
     k nums cnt = n - matchNum[k] cnt n - matchNum[k] n matchNum[k] matchNum k if cnt >= 0:
            d = max(d, makeDigit[cnt] + 1)
     cnt >= 0 cnt 0 d = max(d, makeDigit[cnt] + 1) d max(d, makeDigit[cnt] + 1) max d makeDigit[cnt] + 1 makeDigit[cnt] makeDigit cnt 1 if makeDigit[n] < d:
        makeDigit[n] = d makeDigit[n] < d makeDigit[n] makeDigit n d makeDigit[n] = d makeDigit[n] makeDigit n d ans = [] ans [] for d in range(makeDigit[N])[:: -1]:
    for k in nums:
        cnt = N - matchNum[k]
        if cnt >= 0 and makeDigit[cnt] == d:
            ans.append(k)
            N = cnt
            break d range(makeDigit[N])[:: -1] (makeDigit[N]) range makeDigit[N] makeDigit N :: -1 -1 for k in nums:
        cnt = N - matchNum[k]
        if cnt >= 0 and makeDigit[cnt] == d:
            ans.append(k)
            N = cnt
            break k nums cnt = N - matchNum[k] cnt N - matchNum[k] N matchNum[k] matchNum k if cnt >= 0 and makeDigit[cnt] == d:
            ans.append(k)
            N = cnt
            break cnt >= 0 and makeDigit[cnt] == d cnt >= 0 cnt 0 makeDigit[cnt] == d makeDigit[cnt] makeDigit cnt d ans.append(k) ans.append ans append k N = cnt N cnt break ans.sort(reverse=True) ans.sort ans sort reverse=True reverse True print(int(''.join(map(str, ans)))) print int(''.join(map(str, ans))) int ''.join(map(str, ans)) ''.join '' join map(str, ans) map str ans cnt = N - matchNum[k] N - matchNum[k] cnt M = map(int, input().split()) map(int, input().split()) M cnt = n - matchNum[k] n - matchNum[k] cnt ans = [] [] ans N = cnt cnt N makeDigit = [-float('inf')] * (N + 1) [-float('inf')] * (N + 1) makeDigit matchNum = {
    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,
} matchNum N, M = map(int, input().split()) map(int, input().split()) N d = makeDigit[n] makeDigit[n] d nums = list(map(int, input().split())) list(map(int, input().split())) nums d = max(d, makeDigit[cnt] + 1) max(d, makeDigit[cnt] + 1) d
from bisect import bisect_left
N, M = map(int, input().split())
nums = list(map(int, input().split()))
nums.sort(reverse=True)

matchNum = {
    1 : 2,
    2 : 5,
    3 : 5,
    4 : 4,
    5 : 5,
    6 : 6,
    7 : 3,
    8 : 7,
    9 : 6,
}

makeDigit = [-float('inf')] * (N + 1)
makeDigit[0] = 0
for n in range(1, N + 1):
    d = makeDigit[n]
    for k in nums:
        cnt = n - matchNum[k]
        if cnt >= 0:
            d = max(d, makeDigit[cnt] + 1)
    if makeDigit[n] < d:
        makeDigit[n] = d

ans = []
for d in range(makeDigit[N])[:: -1]:
    for k in nums:
        cnt = N - matchNum[k]
        if cnt >= 0 and makeDigit[cnt] == d:
            ans.append(k)
            N = cnt
            break

ans.sort(reverse=True)
print(int(''.join(map(str, ans))))
