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]+1: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()))

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

d=[0]*n*9

for i in range(n):

 for j in a:

  if d[i]or i<1: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()))
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] + 1:
            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()))
c = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)
d = [0] * n * 9
for i in range(n):
    for j in a:
        if d[i] or i < 1:
            d[i + c[j]] = max(d[i + c[j]], d[i] * 10 + j)
print((d[n]))

-d = [0] + [-1] * n * 9 +d = [0] * n * 9 -        if d[i] + 1: +        if d[i] or i < 1: