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

d=[0]*n*9

for i in range(n):

 for j in a:

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

  if d[i]or i<1:d[i+c]=max(d[i+c],d[i]*10+j)

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

d=[0]*n*9

for i in range(n):

 for j in a:

  if d[i]or i<1:c=i+(0,2,5,5,4,5,6,3,7,6)[j];d[c]=max(d[c],d[i]*10+j)

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

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

-        c = (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)[j] -            d[i + c] = max(d[i + c], d[i] * 10 + j) +            c = i + (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)[j] +            d[c] = max(d[c], d[i] * 10 + j)