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[0]=0  

for i in range(N+1):

    for j in A:

        if i + B[j] <= N:

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

print((dp[N]))
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[0] = 0 



for i in range(N):

    for j in A:

        if i+B[j] <= N:

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



print((DP[N])) 
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[0] = 0
for i in range(N + 1):
    for j in A:
        if i + B[j] <= N:
            dp[i + B[j]] = max(dp[i] * 10 + j, dp[i + B[j]])
print((dp[N]))

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[0] = 0
for i in range(N):
    for j in A:
        if i + B[j] <= N:
            DP[i + B[j]] = max(DP[i] * 10 + j, DP[i + B[j]])
print((DP[N]))

-dp = [-1] * (N + 1) -dp[0] = 0 -for i in range(N + 1): +DP = [-1] * (N + 1) +DP[0] = 0 +for i in range(N): -            dp[i + B[j]] = max(dp[i] * 10 + j, dp[i + B[j]]) -print((dp[N])) +            DP[i + B[j]] = max(DP[i] * 10 + j, DP[i + B[j]]) +print((DP[N]))