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