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

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

for i in range(n):

 for j,k in enumerate((0,2,5,5,4,5,6,3,7,6)):

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

+c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] -    for j, k in enumerate((0, 2, 5, 5, 4, 5, 6, 3, 7, 6)): -        if d[i] >= 0 and j in a: -            d[i + k] = max(d[i + k], d[i] * 10 + j) +    for j in a: +        if d[i] >= 0: +            d[i + c[j]] = max(d[i + c[j]], d[i] * 10 + j)