#!/usr/bin/env python



from collections import deque

import itertools as it

import sys

import math



sys.setrecursionlimit(1000000)



INF = 10 ** 18



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

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

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



D = [0] + [-1] * N * 9

a = [0, 0] * N



for i in range(1, N + 1):

 for j in A:p=i-c[j];D[i],a[i]=[[D[i],a[i]],[D[p]+1,j]][min(p,D[p],D[p]+1-D[i])>=0]



S, p = "", N

while p:S,p=S+str(a[p]),p-c[a[p]]

print(S)
r=raw_input

N,M=r().split()

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

A=sorted(r().split())

N=int(N)

D,a=[0]+[-1]*N*9,[0,0]*N

for i in range(1, N + 1):

 for j in A:

  p=i-c[int(j)]

  if min(p,D[p],D[p]+1-D[i])>=0:D[i],a[i]=D[p]+1,j

S=""

while N:S,N=S+a[N],N-c[int(a[N])]

print(S)
#!/usr/bin/env python
from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)
INF = 10**18
N, M = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = sorted(map(int, input().split()))
D = [0] + [-1] * N * 9
a = [0, 0] * N
for i in range(1, N + 1):
    for j in A:
        p = i - c[j]
        D[i], a[i] = [[D[i], a[i]], [D[p] + 1, j]][min(p, D[p], D[p] + 1 - D[i]) >= 0]
S, p = "", N
while p:
    S, p = S + str(a[p]), p - c[a[p]]
print(S)

r = raw_input
N, M = r().split()
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = sorted(r().split())
N = int(N)
D, a = [0] + [-1] * N * 9, [0, 0] * N
for i in range(1, N + 1):
    for j in A:
        p = i - c[int(j)]
        if min(p, D[p], D[p] + 1 - D[i]) >= 0:
            D[i], a[i] = D[p] + 1, j
S = ""
while N:
    S, N = S + a[N], N - c[int(a[N])]
print(S)

-#!/usr/bin/env python -from collections import deque -import itertools as it -import sys -import math - -sys.setrecursionlimit(1000000) -INF = 10**18 -N, M = list(map(int, input().split())) +r = raw_input +N, M = r().split() -A = sorted(map(int, input().split())) -D = [0] + [-1] * N * 9 -a = [0, 0] * N +A = sorted(r().split()) +N = int(N) +D, a = [0] + [-1] * N * 9, [0, 0] * N -        p = i - c[j] -        D[i], a[i] = [[D[i], a[i]], [D[p] + 1, j]][min(p, D[p], D[p] + 1 - D[i]) >= 0] -S, p = "", N -while p: -    S, p = S + str(a[p]), p - c[a[p]] +        p = i - c[int(j)] +        if min(p, D[p], D[p] + 1 - D[i]) >= 0: +            D[i], a[i] = D[p] + 1, j +S = "" +while N: +    S, N = S + a[N], N - c[int(a[N])]