import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
As = list(mapint())
num_lis = [2, 5, 5, 4, 5, 6, 3, 7, 6]
need_m = [num_lis[a-1] for a in As]
dp = [-1]*(N+1)
dp[0] = 0

for i in range(1, N+1):
    for a, need in zip(As, need_m):
        if need>i:
            continue
        elif dp[i-need]==-1:
            continue
        elif dp[i-need]==0:
            dp[i] = max(dp[i], a)
        else:
            dp[i] = max(dp[i], dp[i-need]*10+a)

print(dp[-1]) import sys sys def input(): return sys.stdin.readline().strip() input def mapint(): return map(int, input().split()) mapint sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 N, M = mapint() N mapint() mapint M As = list(mapint()) As list(mapint()) list mapint() mapint num_lis = [2, 5, 5, 4, 5, 6, 3, 7, 6] num_lis [2, 5, 5, 4, 5, 6, 3, 7, 6] 2 5 5 4 5 6 3 7 6 num_lis[a-1] for a in As for a in As a As for a in As num_lis[a-1] num_lis a-1 a 1 need_m = [num_lis[a-1] for a in As] need_m [num_lis[a-1] for a in As] dp = [-1]*(N+1) dp [-1]*(N+1) [-1] -1 N+1 N 1 dp[0] = 0 dp[0] dp 0 0 for i in range(1, N+1):
    for a, need in zip(As, need_m):
        if need>i:
            continue
        elif dp[i-need]==-1:
            continue
        elif dp[i-need]==0:
            dp[i] = max(dp[i], a)
        else:
            dp[i] = max(dp[i], dp[i-need]*10+a) i range(1, N+1) range 1 N+1 N 1 for a, need in zip(As, need_m):
        if need>i:
            continue
        elif dp[i-need]==-1:
            continue
        elif dp[i-need]==0:
            dp[i] = max(dp[i], a)
        else:
            dp[i] = max(dp[i], dp[i-need]*10+a) a need zip(As, need_m) zip As need_m if need>i:
            continue
        elif dp[i-need]==-1:
            continue
        elif dp[i-need]==0:
            dp[i] = max(dp[i], a)
        else:
            dp[i] = max(dp[i], dp[i-need]*10+a) need>i need i continue elif dp[i-need]==-1:
            continue
         dp[i-need]==-1 dp[i-need] dp i-need i need -1 continue elif dp[i-need]==0:
            dp[i] = max(dp[i], a)
         dp[i-need]==0 dp[i-need] dp i-need i need 0 dp[i] = max(dp[i], a) dp[i] dp i max(dp[i], a) max dp[i] dp i a dp[i] = max(dp[i], dp[i-need]*10+a) dp[i] dp i max(dp[i], dp[i-need]*10+a) max dp[i] dp i dp[i-need]*10+a dp[i-need]*10 dp[i-need] dp i-need i need 10 a print(dp[-1]) print dp[-1] dp -1 dp = [-1]*(N+1) [-1]*(N+1) dp def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input N, M = mapint() mapint() N M = mapint() mapint() M num_lis = [2, 5, 5, 4, 5, 6, 3, 7, 6] [2, 5, 5, 4, 5, 6, 3, 7, 6] num_lis def mapint(): return map(int, input().split()) def mapint(): return map(int, input().split()) mapint As = list(mapint()) list(mapint()) As need_m = [num_lis[a-1] for a in As] [num_lis[a-1] for a in As] need_m
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
As = list(mapint())
num_lis = [2, 5, 5, 4, 5, 6, 3, 7, 6]
need_m = [num_lis[a-1] for a in As]
dp = [-1]*(N+1)
dp[0] = 0

for i in range(1, N+1):
    for a, need in zip(As, need_m):
        if need>i:
            continue
        elif dp[i-need]==-1:
            continue
        elif dp[i-need]==0:
            dp[i] = max(dp[i], a)
        else:
            dp[i] = max(dp[i], dp[i-need]*10+a)

print(dp[-1])