def intlog10(n):

    if n <= 0:

        return -1

    i = 0

    while 10**i <= n:

        i += 1

    return i-1



N, M = list(map(int, input().split()))

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

    

L = [2, 5, 5, 4, 5, 6, 3, 7, 6]

B = [[A[i], L[A[i]-1]] for i in range(M)]



best = sorted(B, key = lambda x: x[1]*10-x[0])[0]



adj = 0

if N > 500:

    adj = (N - 500)//best[1]

    N -= best[1] * adj



X = [0] + [-10**9] * N

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

    if i > 50 and i < N-50:

        X[i] = X[i-best[1]]*10 + best[0]

    else:

        for b in B:

            if b[1] <= i:

                if X[i-b[1]] == 0:

                    X[i] = max(X[i], b[0])

                else:

                    X[i] = max(X[i], X[i-b[1]]*10 + b[0], X[i-b[1]] + b[0] * (10**intlog10(X[i-b[1]])))



ans = str(X[N])

if adj > 0:

    ans = ans[:50] + str(best[0]) * adj + ans[50:]



print(ans)
def intlog10(n):

    if n <= 0:

        return -1

    i = 0

    while 10**i <= n:

        i += 1

    return i-1



N, M = list(map(int, input().split()))

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

    

L = [2, 5, 5, 4, 5, 6, 3, 7, 6]

B = [[A[i], L[A[i]-1]] for i in range(M)]



best = sorted(B, key = lambda x: x[1]*10-x[0])[0]



adj = 0

if N > 500:

    adj = (N - 500)//best[1]

    N -= best[1] * adj



X = [0] + [-10**9] * N

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

    if i > 50 and i < N-50:

        X[i] = X[i-best[1]]*10 + best[0]

    else:

        for b in B:

            if b[1] <= i:

                if X[i-b[1]] == 0:

                    X[i] = max(X[i], b[0])

                else:

                    X[i] = max(X[i], X[i-b[1]]*10 + b[0])



ans = str(X[N])

if adj > 0:

    ans = ans[:50] + str(best[0]) * adj + ans[50:]



print(ans)

def intlog10(n):
    if n <= 0:
        return -1
    i = 0
    while 10**i <= n:
        i += 1
    return i - 1


N, M = list(map(int, input().split()))
A = [int(a) for a in input().split()]
L = [2, 5, 5, 4, 5, 6, 3, 7, 6]
B = [[A[i], L[A[i] - 1]] for i in range(M)]
best = sorted(B, key=lambda x: x[1] * 10 - x[0])[0]
adj = 0
if N > 500:
    adj = (N - 500) // best[1]
    N -= best[1] * adj
X = [0] + [-(10**9)] * N
for i in range(1, N + 1):
    if i > 50 and i < N - 50:
        X[i] = X[i - best[1]] * 10 + best[0]
    else:
        for b in B:
            if b[1] <= i:
                if X[i - b[1]] == 0:
                    X[i] = max(X[i], b[0])
                else:
                    X[i] = max(
                        X[i],
                        X[i - b[1]] * 10 + b[0],
                        X[i - b[1]] + b[0] * (10 ** intlog10(X[i - b[1]])),
                    )
ans = str(X[N])
if adj > 0:
    ans = ans[:50] + str(best[0]) * adj + ans[50:]
print(ans)

def intlog10(n):
    if n <= 0:
        return -1
    i = 0
    while 10**i <= n:
        i += 1
    return i - 1


N, M = list(map(int, input().split()))
A = [int(a) for a in input().split()]
L = [2, 5, 5, 4, 5, 6, 3, 7, 6]
B = [[A[i], L[A[i] - 1]] for i in range(M)]
best = sorted(B, key=lambda x: x[1] * 10 - x[0])[0]
adj = 0
if N > 500:
    adj = (N - 500) // best[1]
    N -= best[1] * adj
X = [0] + [-(10**9)] * N
for i in range(1, N + 1):
    if i > 50 and i < N - 50:
        X[i] = X[i - best[1]] * 10 + best[0]
    else:
        for b in B:
            if b[1] <= i:
                if X[i - b[1]] == 0:
                    X[i] = max(X[i], b[0])
                else:
                    X[i] = max(X[i], X[i - b[1]] * 10 + b[0])
ans = str(X[N])
if adj > 0:
    ans = ans[:50] + str(best[0]) * adj + ans[50:]
print(ans)

-                    X[i] = max( -                        X[i], -                        X[i - b[1]] * 10 + b[0], -                        X[i - b[1]] + b[0] * (10 ** intlog10(X[i - b[1]])), -                    ) +                    X[i] = max(X[i], X[i - b[1]] * 10 + b[0])