N, M = 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 = 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 B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] B [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 DP = [-1]*(N+1) DP [-1]*(N+1) [-1] -1 N+1 N 1 DP[0] = 0 DP[0] 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]]) i range(N) range N for j in A:
        if i+B[j] <= N:
            DP[i+B[j]] = max(DP[i]*10+j, DP[i+B[j]]) j A if i+B[j] <= N:
            DP[i+B[j]] = max(DP[i]*10+j, DP[i+B[j]]) i+B[j] <= N i+B[j] i B[j] B j N DP[i+B[j]] = max(DP[i]*10+j, DP[i+B[j]]) DP[i+B[j]] DP i+B[j] i B[j] B j max(DP[i]*10+j, DP[i+B[j]]) max DP[i]*10+j DP[i]*10 DP[i] DP i 10 j DP[i+B[j]] DP i+B[j] i B[j] B j print(DP[N]) print DP[N] DP N B = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] B M = map(int, input().split()) map(int, input().split()) M N, M = map(int, input().split()) map(int, input().split()) N DP = [-1]*(N+1) [-1]*(N+1) DP A = list(map(int, input().split())) list(map(int, input().split())) A
N, M = 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]) 