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+int("0255456376"[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*9+[-1]*n*9

for i in range(1,n+1):d[i]=max(d[i-int("0255456376"[j])]*10+j for 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 + int("0255456376"[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 * 9 + [-1] * n * 9
for i in range(1, n + 1):
    d[i] = max(d[i - int("0255456376"[j])] * 10 + j for j in a)
print((d[n]))

-d = [0] * n * 9 -for i in range(n): -    for j in a: -        c = i + int("0255456376"[j]) -        d[c] = max(d[c], (d[i] * 10 + j) * (i < 1 or d[i] > 0))  ### +d = [0] * n * 9 + [-1] * n * 9 +for i in range(1, n + 1): +    d[i] = max(d[i - int("0255456376"[j])] * 10 + j for j in a)