import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
cnt = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] * (N+1) # the max number using i matches
for i in range(N):
    if i > 0 and dp[i] == 0:
        continue
    for a in A:
        if i + cnt[a] > N:
            continue
        dp[i+cnt[a]] = max(dp[i+cnt[a]], dp[i]*10 + a)
print(dp[N]) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 cnt = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] cnt [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 dp = [0] * (N+1) dp [0] * (N+1) [0] 0 N+1 N 1 for i in range(N):
    if i > 0 and dp[i] == 0:
        continue
    for a in A:
        if i + cnt[a] > N:
            continue
        dp[i+cnt[a]] = max(dp[i+cnt[a]], dp[i]*10 + a) i range(N) range N if i > 0 and dp[i] == 0:
        continue
     i > 0 and dp[i] == 0 i > 0 i 0 dp[i] == 0 dp[i] dp i 0 continue for a in A:
        if i + cnt[a] > N:
            continue
        dp[i+cnt[a]] = max(dp[i+cnt[a]], dp[i]*10 + a) a A if i + cnt[a] > N:
            continue
         i + cnt[a] > N i + cnt[a] i cnt[a] cnt a N continue dp[i+cnt[a]] = max(dp[i+cnt[a]], dp[i]*10 + a) dp[i+cnt[a]] dp i+cnt[a] i cnt[a] cnt a max(dp[i+cnt[a]], dp[i]*10 + a) max dp[i+cnt[a]] dp i+cnt[a] i cnt[a] cnt a dp[i]*10 + a dp[i]*10 dp[i] dp i 10 a print(dp[N]) print dp[N] dp N A = list(map(int, input().split())) list(map(int, input().split())) A input = sys.stdin.readline sys.stdin.readline input cnt = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] cnt M = map(int, input().split()) map(int, input().split()) M dp = [0] * (N+1) [0] * (N+1) dp N, M = map(int, input().split()) map(int, input().split()) N
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A = list(map(int, input().split()))
cnt = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] * (N+1) # the max number using i matches
for i in range(N):
    if i > 0 and dp[i] == 0:
        continue
    for a in A:
        if i + cnt[a] > N:
            continue
        dp[i+cnt[a]] = max(dp[i+cnt[a]], dp[i]*10 + a)
print(dp[N])