N, M, *A = map(int, open(0).read().split())

match = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [[0] * M for _ in range(N + 1)]

for i in range(N + 1):
    for j in range(M):
        for k in range(M):
            if (i - match[A[k]] == 0) or \
                   (i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0):
                dp[i][k] = max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k])

print(max(dp[-1]))
 N, M, *A = map(int, open(0).read().split()) N map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split M *A A match = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} match {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 [0] * M for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [0] * M [0] 0 M dp = [[0] * M for _ in range(N + 1)] dp [[0] * M for _ in range(N + 1)] for i in range(N + 1):
    for j in range(M):
        for k in range(M):
            if (i - match[A[k]] == 0) or \
                   (i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0):
                dp[i][k] = max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k]) i range(N + 1) range N + 1 N 1 for j in range(M):
        for k in range(M):
            if (i - match[A[k]] == 0) or \
                   (i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0):
                dp[i][k] = max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k]) j range(M) range M for k in range(M):
            if (i - match[A[k]] == 0) or \
                   (i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0):
                dp[i][k] = max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k]) k range(M) range M if (i - match[A[k]] == 0) or \
                   (i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0):
                dp[i][k] = max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k]) (i - match[A[k]] == 0) or \
                   (i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0) i - match[A[k]] == 0 i - match[A[k]] i match[A[k]] match A[k] A k 0 i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0 i - match[A[k]] > 0 i - match[A[k]] i match[A[k]] match A[k] A k 0 dp[i - match[A[k]]][j] != 0 dp[i - match[A[k]]][j] [i - match[A[k]]] dp i - match[A[k]] i match[A[k]] match A[k] A k j 0 dp[i][k] = max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k]) dp[i][k] [i] dp i k max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k]) max dp[i][k] [i] dp i k dp[i - match[A[k]]][j] * 10 + A[k] dp[i - match[A[k]]][j] * 10 dp[i - match[A[k]]][j] [i - match[A[k]]] dp i - match[A[k]] i match[A[k]] match A[k] A k j 10 A[k] A k print(max(dp[-1])) print max(dp[-1]) max dp[-1] dp -1 dp = [[0] * M for _ in range(N + 1)] [[0] * M for _ in range(N + 1)] dp N, M, *A = map(int, open(0).read().split()) map(int, open(0).read().split()) N M, *A = map(int, open(0).read().split()) map(int, open(0).read().split()) M match = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} match
N, M, *A = map(int, open(0).read().split())

match = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
dp = [[0] * M for _ in range(N + 1)]

for i in range(N + 1):
    for j in range(M):
        for k in range(M):
            if (i - match[A[k]] == 0) or \
                   (i - match[A[k]] > 0 and dp[i - match[A[k]]][j] != 0):
                dp[i][k] = max(dp[i][k], 
                               dp[i - match[A[k]]][j] * 10 + A[k])

print(max(dp[-1]))

