n , m = list(map(int, input().split(' ')))

s = [0,2,5,5,4,5,6,3,7,6]

a = list(map(int, input().split(' ')))

r = []

for i in range(len(a)):

    r.append([s[a[i]],a[i]])

r.sort()

d = [-1]*(n+1)

d[0] = 0

for i in range(1,n+1):

    j = 0

    while j < len(a):

        if i-s[r[j][1]] < 0:

            j += 1

        elif d[i-s[r[j][1]]] >= 0:

            d[i] = d[i-s[r[j][1]]] +1

            break

        else:

            j+= 1

result = []

a.sort(reverse = True)

while n > 0:

    j = 0

    while j < len(a):

        if n < s[a[j]]:

            j += 1

        elif d[n-s[a[j]]] +1 == d[n]:

            result.append(a[j])

            n -= s[a[j]]

            break

        else:

            j += 1

print((''.join(list(map(str, result)))))
def main():

    n, m = list(map(int, input().split(' ')))

    a = list(map(int, input().split(' ')))

    b = [0,2,5,5,4,5,6,3,7,6]

    c = [[b[i], -i] for i in a]

    c.sort()

    result = [[] for _ in range(n+1)]

    dp = [-1]*(n+1)

    dp[0] = 0

    for i in range(1, n+1):

        for j in c:

            if i-j[0] >= 0:

                if dp[i-j[0]] >= 0:

                    dp[i] = dp[i-j[0]] +1

                    break

    c = [[-x[1], x[0]] for x in c]

    c.sort(reverse = True)

    result = []

    i = n

    while i > 0:

        j = 0

        while j < len(c):

            if i-c[j][1] >= 0:

                if dp[i-c[j][1]] + 1 == dp[i]:

                    result.append(c[j][0])

                    i -= c[j][1]

                    j = len(c)

                else:

                    j += 1

            else:

                j += 1

    print((''.join(map(str, result))))

if __name__ =='__main__':

    main()
n, m = list(map(int, input().split(" ")))
s = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
a = list(map(int, input().split(" ")))
r = []
for i in range(len(a)):
    r.append([s[a[i]], a[i]])
r.sort()
d = [-1] * (n + 1)
d[0] = 0
for i in range(1, n + 1):
    j = 0
    while j < len(a):
        if i - s[r[j][1]] < 0:
            j += 1
        elif d[i - s[r[j][1]]] >= 0:
            d[i] = d[i - s[r[j][1]]] + 1
            break
        else:
            j += 1
result = []
a.sort(reverse=True)
while n > 0:
    j = 0
    while j < len(a):
        if n < s[a[j]]:
            j += 1
        elif d[n - s[a[j]]] + 1 == d[n]:
            result.append(a[j])
            n -= s[a[j]]
            break
        else:
            j += 1
print(("".join(list(map(str, result)))))

def main():
    n, m = list(map(int, input().split(" ")))
    a = list(map(int, input().split(" ")))
    b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    c = [[b[i], -i] for i in a]
    c.sort()
    result = [[] for _ in range(n + 1)]
    dp = [-1] * (n + 1)
    dp[0] = 0
    for i in range(1, n + 1):
        for j in c:
            if i - j[0] >= 0:
                if dp[i - j[0]] >= 0:
                    dp[i] = dp[i - j[0]] + 1
                    break
    c = [[-x[1], x[0]] for x in c]
    c.sort(reverse=True)
    result = []
    i = n
    while i > 0:
        j = 0
        while j < len(c):
            if i - c[j][1] >= 0:
                if dp[i - c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
    print(("".join(map(str, result))))


if __name__ == "__main__":
    main()

-n, m = list(map(int, input().split(" "))) -s = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] -a = list(map(int, input().split(" "))) -r = [] -for i in range(len(a)): -    r.append([s[a[i]], a[i]]) -r.sort() -d = [-1] * (n + 1) -d[0] = 0 -for i in range(1, n + 1): -    j = 0 -    while j < len(a): -        if i - s[r[j][1]] < 0: -            j += 1 -        elif d[i - s[r[j][1]]] >= 0: -            d[i] = d[i - s[r[j][1]]] + 1 -            break -        else: -            j += 1 -result = [] -a.sort(reverse=True) -while n > 0: -    j = 0 -    while j < len(a): -        if n < s[a[j]]: -            j += 1 -        elif d[n - s[a[j]]] + 1 == d[n]: -            result.append(a[j]) -            n -= s[a[j]] -            break -        else: -            j += 1 -print(("".join(list(map(str, result))))) +def main(): +    n, m = list(map(int, input().split(" "))) +    a = list(map(int, input().split(" "))) +    b = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] +    c = [[b[i], -i] for i in a] +    c.sort() +    result = [[] for _ in range(n + 1)] +    dp = [-1] * (n + 1) +    dp[0] = 0 +    for i in range(1, n + 1): +        for j in c: +            if i - j[0] >= 0: +                if dp[i - j[0]] >= 0: +                    dp[i] = dp[i - j[0]] + 1 +                    break +    c = [[-x[1], x[0]] for x in c] +    c.sort(reverse=True) +    result = [] +    i = n +    while i > 0: +        j = 0 +        while j < len(c): +            if i - c[j][1] >= 0: +                if dp[i - c[j][1]] + 1 == dp[i]: +                    result.append(c[j][0]) +                    i -= c[j][1] +                    j = len(c) +                else: +                    j += 1 +            else: +                j += 1 +    print(("".join(map(str, result)))) + + +if __name__ == "__main__": +    main()