N, M = map(int, input().split())
A = set(map(int, input().split()))

dp = [-1] * (N+1)
weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
# 0本のマッチで作れる最大の整数は０
dp[0] = 0

for i in range(N+1):
    for a in A:
        if i + weights[a] > N:
            continue
        dp[i + weights[a]] = max(dp[i + weights[a]], dp[i] * 10 + a)
print(dp[N])
 N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = set(map(int, input().split())) A set(map(int, input().split())) set map(int, input().split()) map int input().split() ().split () input split dp = [-1] * (N+1) dp [-1] * (N+1) [-1] -1 N+1 N 1 weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] weights [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 dp[0] = 0 dp[0] dp 0 0 for i in range(N+1):
    for a in A:
        if i + weights[a] > N:
            continue
        dp[i + weights[a]] = max(dp[i + weights[a]], dp[i] * 10 + a) i range(N+1) range N+1 N 1 for a in A:
        if i + weights[a] > N:
            continue
        dp[i + weights[a]] = max(dp[i + weights[a]], dp[i] * 10 + a) a A if i + weights[a] > N:
            continue
         i + weights[a] > N i + weights[a] i weights[a] weights a N continue dp[i + weights[a]] = max(dp[i + weights[a]], dp[i] * 10 + a) dp[i + weights[a]] dp i + weights[a] i weights[a] weights a max(dp[i + weights[a]], dp[i] * 10 + a) max dp[i + weights[a]] dp i + weights[a] i weights[a] weights a dp[i] * 10 + a dp[i] * 10 dp[i] dp i 10 a print(dp[N]) print dp[N] dp N N, M = map(int, input().split()) map(int, input().split()) N dp = [-1] * (N+1) [-1] * (N+1) dp M = map(int, input().split()) map(int, input().split()) M A = set(map(int, input().split())) set(map(int, input().split())) A weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] weights
N, M = map(int, input().split())
A = set(map(int, input().split()))

dp = [-1] * (N+1)
weights = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
# 0本のマッチで作れる最大の整数は０
dp[0] = 0

for i in range(N+1):
    for a in A:
        if i + weights[a] > N:
            continue
        dp[i + weights[a]] = max(dp[i + weights[a]], dp[i] * 10 + a)
print(dp[N])

