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

A=list(map(int,input().split()))

L=[0,2,5,5,4,5,6,3,7,6]

dp=[0]+[-1]*n

for i in range(n+1):

  for a in A:

    if i+L[a]<=n:

      dp[i+L[a]]=max(dp[i+L[a]],dp[i]*10+a)

print((dp[n]))

N, M = list(map(int, input().split()))

a = [int(i) for i in input().split()]



dic = {}

dic[1] = 2

dic[2] = 5

dic[3] = 5

dic[4] = 4

dic[5] = 5

dic[6] = 6

dic[7] = 3

dic[8] = 7

dic[9] = 6



ls = [-1]*8

dp = [-1]*(N+1)

dp[0] = 0

for i in a:

  j = dic[i]

  if ls[j]<i:

    ls[j] = i



tag = []

for i in range(len(ls)):

  if ls[i]!=-1:

    tag += [i]

for i in range(N+1):

  for j in tag:

    if i+j<=N:

      dp[i+j] = max(dp[i]*10+ls[j], dp[i+j])



print((dp[N]))

n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] + [-1] * n
for i in range(n + 1):
    for a in A:
        if i + L[a] <= n:
            dp[i + L[a]] = max(dp[i + L[a]], dp[i] * 10 + a)
print((dp[n]))

N, M = list(map(int, input().split()))
a = [int(i) for i in input().split()]
dic = {}
dic[1] = 2
dic[2] = 5
dic[3] = 5
dic[4] = 4
dic[5] = 5
dic[6] = 6
dic[7] = 3
dic[8] = 7
dic[9] = 6
ls = [-1] * 8
dp = [-1] * (N + 1)
dp[0] = 0
for i in a:
    j = dic[i]
    if ls[j] < i:
        ls[j] = i
tag = []
for i in range(len(ls)):
    if ls[i] != -1:
        tag += [i]
for i in range(N + 1):
    for j in tag:
        if i + j <= N:
            dp[i + j] = max(dp[i] * 10 + ls[j], dp[i + j])
print((dp[N]))

-n, m = list(map(int, input().split())) -A = list(map(int, input().split())) -L = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] -dp = [0] + [-1] * n -for i in range(n + 1): -    for a in A: -        if i + L[a] <= n: -            dp[i + L[a]] = max(dp[i + L[a]], dp[i] * 10 + a) -print((dp[n])) +N, M = list(map(int, input().split())) +a = [int(i) for i in input().split()] +dic = {} +dic[1] = 2 +dic[2] = 5 +dic[3] = 5 +dic[4] = 4 +dic[5] = 5 +dic[6] = 6 +dic[7] = 3 +dic[8] = 7 +dic[9] = 6 +ls = [-1] * 8 +dp = [-1] * (N + 1) +dp[0] = 0 +for i in a: +    j = dic[i] +    if ls[j] < i: +        ls[j] = i +tag = [] +for i in range(len(ls)): +    if ls[i] != -1: +        tag += [i] +for i in range(N + 1): +    for j in tag: +        if i + j <= N: +            dp[i + j] = max(dp[i] * 10 + ls[j], dp[i + j]) +print((dp[N]))