n,m,*a=list(map(int,open(0).read().split()))

d=[0]*-~n

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

 for j,k in zip(list(range(9)),(2,5,5,4,5,6,3,7,6)):

  if i-k>=0and(i-k<1or d[i-k])and j+1in a:d[i]=max(d[i],d[i-k]*10+j+1)

print((d[n]))
n,m,*a=list(map(int,open(0).read().split()))

c=(0,2,5,5,4,5,6,3,7,6)

d=[0]+[-1]*n*9

for i in range(n):

 for j in a:

  if d[i]>=0:d[i+c[j]]=max(d[i+c[j]],d[i]*10+j)

print((d[n]))
n, m, *a = list(map(int, open(0).read().split()))
d = [0] * -~n
for i in range(1, n + 1):
    for j, k in zip(list(range(9)), (2, 5, 5, 4, 5, 6, 3, 7, 6)):
        if i - k >= 0 and (i - k < 1 or d[i - k]) and j + 1 in a:
            d[i] = max(d[i], d[i - k] * 10 + j + 1)
print((d[n]))

n, m, *a = list(map(int, open(0).read().split()))
c = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
d = [0] + [-1] * n * 9
for i in range(n):
    for j in a:
        if d[i] >= 0:
            d[i + c[j]] = max(d[i + c[j]], d[i] * 10 + j)
print((d[n]))

-d = [0] * -~n -for i in range(1, n + 1): -    for j, k in zip(list(range(9)), (2, 5, 5, 4, 5, 6, 3, 7, 6)): -        if i - k >= 0 and (i - k < 1 or d[i - k]) and j + 1 in a: -            d[i] = max(d[i], d[i - k] * 10 + j + 1) +c = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6) +d = [0] + [-1] * n * 9 +for i in range(n): +    for j in a: +        if d[i] >= 0: +            d[i + c[j]] = max(d[i + c[j]], d[i] * 10 + j)