N, M = [int(i) for i in input().split()]

a = [int(i) for i in input().split()]

a.sort()                        # 破壊的。返り値はNone



# print(N, M)

# print(a)



num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]





dp = [-999 for _ in range(N+1)]

dp[0] = 0

for i in range(N+1):

    n = [dp[i-num[a[j]]] for j in range(M) if i-num[a[j]] >= 0]

    if len(n) > 0:

        dp[i] = max(n) + 1



# print(dp)



i = N

ans = ""

for _ in range(dp[N]):

    # print("i", i)

    # c = [(a[j], dp[i-num[a[j]]], dp[i]) for j in range(M)]

    # print("c", c)

    if dp[i] > 1:

        b = max([a[j] for j in range(M)

                 if i - num[a[j]] >= 0 and dp[i-num[a[j]]] + 1 == dp[i]])

        ans += str(b)

    else:

        b = max([a[j] for j in range(M)

                 if i - num[a[j]] == 0 and dp[i-num[a[j]]] + 1 == dp[i]])

        ans += str(b)

        break

    # print("ans", ans)

    i = i-num[b]

    # print(i)

print(ans)

N, M = [int(x) for x in input().split()]

a = sorted([int(x) for x in input().split()])

INF = 10**9



num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]



keta = [-INF for _ in range(N+1)]

keta[0] = 0

for i in range(1, N+1):

    cand = [keta[i-num[aa]] for aa in a if i-num[aa] >= 0]

    if len(cand) > 0:

        keta[i] = max(cand) + 1



n = N

ans = ""

while n > 0:

    cand = [aa for aa in a if n-num[aa] >= 0 and keta[n]-1 == keta[n-num[aa]]]

    if len(cand) > 0:

        ans += str(cand[-1])    # ソートしてるので。

        n = n - num[cand[-1]]

print(ans)

N, M = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort()  # 破壊的。返り値はNone
# print(N, M)
# print(a)
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-999 for _ in range(N + 1)]
dp[0] = 0
for i in range(N + 1):
    n = [dp[i - num[a[j]]] for j in range(M) if i - num[a[j]] >= 0]
    if len(n) > 0:
        dp[i] = max(n) + 1
# print(dp)
i = N
ans = ""
for _ in range(dp[N]):
    # print("i", i)
    # c = [(a[j], dp[i-num[a[j]]], dp[i]) for j in range(M)]
    # print("c", c)
    if dp[i] > 1:
        b = max(
            [
                a[j]
                for j in range(M)
                if i - num[a[j]] >= 0 and dp[i - num[a[j]]] + 1 == dp[i]
            ]
        )
        ans += str(b)
    else:
        b = max(
            [
                a[j]
                for j in range(M)
                if i - num[a[j]] == 0 and dp[i - num[a[j]]] + 1 == dp[i]
            ]
        )
        ans += str(b)
        break
    # print("ans", ans)
    i = i - num[b]
    # print(i)
print(ans)

N, M = [int(x) for x in input().split()]
a = sorted([int(x) for x in input().split()])
INF = 10**9
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
keta = [-INF for _ in range(N + 1)]
keta[0] = 0
for i in range(1, N + 1):
    cand = [keta[i - num[aa]] for aa in a if i - num[aa] >= 0]
    if len(cand) > 0:
        keta[i] = max(cand) + 1
n = N
ans = ""
while n > 0:
    cand = [aa for aa in a if n - num[aa] >= 0 and keta[n] - 1 == keta[n - num[aa]]]
    if len(cand) > 0:
        ans += str(cand[-1])  # ソートしてるので。
        n = n - num[cand[-1]]
print(ans)

-N, M = [int(i) for i in input().split()] -a = [int(i) for i in input().split()] -a.sort()  # 破壊的。返り値はNone -# print(N, M) -# print(a) +N, M = [int(x) for x in input().split()] +a = sorted([int(x) for x in input().split()]) +INF = 10**9 -dp = [-999 for _ in range(N + 1)] -dp[0] = 0 -for i in range(N + 1): -    n = [dp[i - num[a[j]]] for j in range(M) if i - num[a[j]] >= 0] -    if len(n) > 0: -        dp[i] = max(n) + 1 -# print(dp) -i = N +keta = [-INF for _ in range(N + 1)] +keta[0] = 0 +for i in range(1, N + 1): +    cand = [keta[i - num[aa]] for aa in a if i - num[aa] >= 0] +    if len(cand) > 0: +        keta[i] = max(cand) + 1 +n = N -for _ in range(dp[N]): -    # print("i", i) -    # c = [(a[j], dp[i-num[a[j]]], dp[i]) for j in range(M)] -    # print("c", c) -    if dp[i] > 1: -        b = max( -            [ -                a[j] -                for j in range(M) -                if i - num[a[j]] >= 0 and dp[i - num[a[j]]] + 1 == dp[i] -            ] -        ) -        ans += str(b) -    else: -        b = max( -            [ -                a[j] -                for j in range(M) -                if i - num[a[j]] == 0 and dp[i - num[a[j]]] + 1 == dp[i] -            ] -        ) -        ans += str(b) -        break -    # print("ans", ans) -    i = i - num[b] -    # print(i) +while n > 0: +    cand = [aa for aa in a if n - num[aa] >= 0 and keta[n] - 1 == keta[n - num[aa]]] +    if len(cand) > 0: +        ans += str(cand[-1])  # ソートしてるので。 +        n = n - num[cand[-1]]