import sys
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
Need = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
Usable = {}
for a in A:
    if Need[a] in Usable:
        for key, value in Usable.items():
            if Need[a] == key and a > value:
                Usable[key] = a
    else:
        Usable[Need[a]] = a
Num = [-1] * (N + 1)
Num[0] = 0
for i in range(1, N+1):
    for u, v in Usable.items():
        if i < u:
            continue
        if Num[i - u] >= 0:
            if Num[i - u] == 0:
                Num[i] = max(v, Num[i])
            elif Num[i] < Num[i - u] * 10 + v:
                Num[i] = Num[i - u] * 10 + v

print(Num[N]) import sys sys input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline 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 Need = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] Need [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] -1 2 5 5 4 5 6 3 7 6 Usable = {} Usable {} for a in A:
    if Need[a] in Usable:
        for key, value in Usable.items():
            if Need[a] == key and a > value:
                Usable[key] = a
    else:
        Usable[Need[a]] = a a A if Need[a] in Usable:
        for key, value in Usable.items():
            if Need[a] == key and a > value:
                Usable[key] = a
    else:
        Usable[Need[a]] = a Need[a] in Usable Need[a] Need a Usable for key, value in Usable.items():
            if Need[a] == key and a > value:
                Usable[key] = a
     key value Usable.items() Usable.items Usable items if Need[a] == key and a > value:
                Usable[key] = a
     Need[a] == key and a > value Need[a] == key Need[a] Need a key a > value a value Usable[key] = a Usable[key] Usable key a Usable[Need[a]] = a Usable[Need[a]] Usable Need[a] Need a a Num = [-1] * (N + 1) Num [-1] * (N + 1) [-1] -1 N + 1 N 1 Num[0] = 0 Num[0] Num 0 0 for i in range(1, N+1):
    for u, v in Usable.items():
        if i < u:
            continue
        if Num[i - u] >= 0:
            if Num[i - u] == 0:
                Num[i] = max(v, Num[i])
            elif Num[i] < Num[i - u] * 10 + v:
                Num[i] = Num[i - u] * 10 + v i range(1, N+1) range 1 N+1 N 1 for u, v in Usable.items():
        if i < u:
            continue
        if Num[i - u] >= 0:
            if Num[i - u] == 0:
                Num[i] = max(v, Num[i])
            elif Num[i] < Num[i - u] * 10 + v:
                Num[i] = Num[i - u] * 10 + v u v Usable.items() Usable.items Usable items if i < u:
            continue
         i < u i u continue if Num[i - u] >= 0:
            if Num[i - u] == 0:
                Num[i] = max(v, Num[i])
            elif Num[i] < Num[i - u] * 10 + v:
                Num[i] = Num[i - u] * 10 + v Num[i - u] >= 0 Num[i - u] Num i - u i u 0 if Num[i - u] == 0:
                Num[i] = max(v, Num[i])
            elif Num[i] < Num[i - u] * 10 + v:
                Num[i] = Num[i - u] * 10 + v Num[i - u] == 0 Num[i - u] Num i - u i u 0 Num[i] = max(v, Num[i]) Num[i] Num i max(v, Num[i]) max v Num[i] Num i elif Num[i] < Num[i - u] * 10 + v:
                Num[i] = Num[i - u] * 10 + v Num[i] < Num[i - u] * 10 + v Num[i] Num i Num[i - u] * 10 + v Num[i - u] * 10 Num[i - u] Num i - u i u 10 v Num[i] = Num[i - u] * 10 + v Num[i] Num i Num[i - u] * 10 + v Num[i - u] * 10 Num[i - u] Num i - u i u 10 v print(Num[N]) print Num[N] Num N Usable = {} {} Usable Num = [-1] * (N + 1) [-1] * (N + 1) Num Need = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] Need A = list(map(int, input().split())) list(map(int, input().split())) A M = map(int, input().split()) map(int, input().split()) M input = sys.stdin.buffer.readline sys.stdin.buffer.readline input N, M = map(int, input().split()) map(int, input().split()) N
import sys
input = sys.stdin.buffer.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
Need = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
Usable = {}
for a in A:
    if Need[a] in Usable:
        for key, value in Usable.items():
            if Need[a] == key and a > value:
                Usable[key] = a
    else:
        Usable[Need[a]] = a
Num = [-1] * (N + 1)
Num[0] = 0
for i in range(1, N+1):
    for u, v in Usable.items():
        if i < u:
            continue
        if Num[i - u] >= 0:
            if Num[i - u] == 0:
                Num[i] = max(v, Num[i])
            elif Num[i] < Num[i - u] * 10 + v:
                Num[i] = Num[i - u] * 10 + v

print(Num[N])