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

d=[0]*n*9

for i in range(n):

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

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

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

for i in range(n):d[i+1]=max(j+d[i-int('0144345265'[j])]*10for j in a)

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

n, m, *a = list(map(int, open(0).read().split()))
d = [0] * n + [-1] * 9
for i in range(n):
    d[i + 1] = max(j + d[i - int("0144345265"[j])] * 10 for j in a)
print((d[n]))

-d = [0] * n * 9 +d = [0] * n + [-1] * 9 -    for j in a: -        c = i + (0, 2, 5, 5, 4, 5, 6, 3, 7, 6)[j] -        d[c] = max(d[c], (d[i] * 10 + j) * (i < 1 or d[i] > 0)) +    d[i + 1] = max(j + d[i - int("0144345265"[j])] * 10 for j in a)