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

A = list(map(int, input().split()))



dp = [[-1 for i in range(10)] for j in range(N+1)]



B = [0 for i in range(10)]

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

for i in range(10):

    if i in A:

        B[i] = C[i]



for i in range(10):

    dp[N][i] = 0

    

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

    for j in range(10):

        for k in range(j+1):

            if B[k] > 0 and i+B[k] <= N:

                # print(i, j, k, i+B[k])

                dp[i][k] = max(dp[i][k], dp[i+B[k]][j] * 10 + k)



# print(dp)

                

ans = 0

for i in range(10):

    ans = max(ans, dp[0][i])



print(ans)
n, m = list(map(int, input().split())) 

a = list(map(int, input().split()))

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



dp = [-1] * (n + 1)

dp[n] = 0



for i in range(n, -1, -1):

    for j in range(m):

        x = b[a[j]]

        if dp[i] != -1 and i - x >= 0:

            dp[i - x] = max(dp[i - x], 10 * dp[i] + a[j])

            

print((dp[0]))
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
dp = [[-1 for i in range(10)] for j in range(N + 1)]
B = [0 for i in range(10)]
C = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(10):
    if i in A:
        B[i] = C[i]
for i in range(10):
    dp[N][i] = 0
for i in range(N, -1, -1):
    for j in range(10):
        for k in range(j + 1):
            if B[k] > 0 and i + B[k] <= N:
                # print(i, j, k, i+B[k])
                dp[i][k] = max(dp[i][k], dp[i + B[k]][j] * 10 + k)
# print(dp)
ans = 0
for i in range(10):
    ans = max(ans, dp[0][i])
print(ans)

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [-1] * (n + 1)
dp[n] = 0
for i in range(n, -1, -1):
    for j in range(m):
        x = b[a[j]]
        if dp[i] != -1 and i - x >= 0:
            dp[i - x] = max(dp[i - x], 10 * dp[i] + a[j])
print((dp[0]))

-N, M = list(map(int, input().split())) -A = list(map(int, input().split())) -dp = [[-1 for i in range(10)] for j in range(N + 1)] -B = [0 for i in range(10)] -C = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] -for i in range(10): -    if i in A: -        B[i] = C[i] -for i in range(10): -    dp[N][i] = 0 -for i in range(N, -1, -1): -    for j in range(10): -        for k in range(j + 1): -            if B[k] > 0 and i + B[k] <= N: -                # print(i, j, k, i+B[k]) -                dp[i][k] = max(dp[i][k], dp[i + B[k]][j] * 10 + k) -# print(dp) -ans = 0 -for i in range(10): -    ans = max(ans, dp[0][i]) -print(ans) +n, m = list(map(int, input().split())) +a = list(map(int, input().split())) +b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] +dp = [-1] * (n + 1) +dp[n] = 0 +for i in range(n, -1, -1): +    for j in range(m): +        x = b[a[j]] +        if dp[i] != -1 and i - x >= 0: +            dp[i - x] = max(dp[i - x], 10 * dp[i] + a[j]) +print((dp[0]))