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

match_num = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

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

for i in range(n):
    for j in range(m):
        nxt = match_num[a[j]]
        if dp[i]>=0:
            dp[i+nxt] = max(dp[i+nxt], dp[i]*10 + a[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 match_num = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6} match_num {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 dp = [-1] * (n+10) dp [-1] * (n+10) [-1] -1 n+10 n 10 dp[0] = 0 dp[0] dp 0 0 for i in range(n):
    for j in range(m):
        nxt = match_num[a[j]]
        if dp[i]>=0:
            dp[i+nxt] = max(dp[i+nxt], dp[i]*10 + a[j]) i range(n) range n for j in range(m):
        nxt = match_num[a[j]]
        if dp[i]>=0:
            dp[i+nxt] = max(dp[i+nxt], dp[i]*10 + a[j]) j range(m) range m nxt = match_num[a[j]] nxt match_num[a[j]] match_num a[j] a j if dp[i]>=0:
            dp[i+nxt] = max(dp[i+nxt], dp[i]*10 + a[j]) dp[i]>=0 dp[i] dp i 0 dp[i+nxt] = max(dp[i+nxt], dp[i]*10 + a[j]) dp[i+nxt] dp i+nxt i nxt max(dp[i+nxt], dp[i]*10 + a[j]) max dp[i+nxt] dp i+nxt i nxt dp[i]*10 + a[j] dp[i]*10 dp[i] dp i 10 a[j] a j print(dp[n]) print dp[n] dp n nxt = match_num[a[j]] match_num[a[j]] nxt dp = [-1] * (n+10) [-1] * (n+10) dp a = list(map(int, input().split())) list(map(int, input().split())) a m = map(int, input().split()) map(int, input().split()) m match_num = {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_num n, m = map(int, input().split()) map(int, input().split()) n
n, m = map(int, input().split())
a = list(map(int, input().split()))

match_num = {1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}

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

for i in range(n):
    for j in range(m):
        nxt = match_num[a[j]]
        if dp[i]>=0:
            dp[i+nxt] = max(dp[i+nxt], dp[i]*10 + a[j])
print(dp[n])