#!/mnt/c/Users/moiki/bash/env/bin/python

N,M = list(map(int, input().split()))



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

A =list(map(int, input().split()))

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

dp[0] = 0



for i in range(N):

    if dp[i] == -1:

        continue

    for c,a in zip( [cost[a] for a in A], A):

        if i + c <= N:

            dp[i+c] = dp[i]+1



# print(dp)

i = N

ans = ""

A = sorted(A, reverse=True)

while i > 0:

    for c,a in zip([cost[a] for a in A], A):

        if i-c >= 0 and dp[i-c] == dp[i]-1:

            ans += str(a)

            i -= c

            break # difficult !! 



print(ans)

        

costs = [2,5,5,4,5,6,3,7,6]



N, M = list(map(int, input().split()))

A = list(map(int, input().split()))

neginf = -1 * 1e20

D = [neginf]*(N+1)



def dp(n_match):

    D[0] = 0

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

        arr = [D[n-costs[a-1]] if n-costs[a-1]>=0 else neginf for a in A]

        if max(arr) < 0:

            continue



        D[n] = max(arr)+1



def main():

    dp(N)

    n = N

    ans = ''

    while n>0:

        arr = [ a for a in A if n-costs[a-1] >= 0 and D[n-costs[a-1]]==D[n]-1 ]

        if len(arr)>0:

            a = max(arr)

            ans += str(a)

            n -= costs[a-1]

        # else:

        #     break

    print(ans)



main()



#!/mnt/c/Users/moiki/bash/env/bin/python
N, M = list(map(int, input().split()))
cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
A = list(map(int, input().split()))
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N):
    if dp[i] == -1:
        continue
    for c, a in zip([cost[a] for a in A], A):
        if i + c <= N:
            dp[i + c] = dp[i] + 1
# print(dp)
i = N
ans = ""
A = sorted(A, reverse=True)
while i > 0:
    for c, a in zip([cost[a] for a in A], A):
        if i - c >= 0 and dp[i - c] == dp[i] - 1:
            ans += str(a)
            i -= c
            break  # difficult !!
print(ans)

costs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
neginf = -1 * 1e20
D = [neginf] * (N + 1)


def dp(n_match):
    D[0] = 0
    for n in range(1, n_match + 1):
        arr = [D[n - costs[a - 1]] if n - costs[a - 1] >= 0 else neginf for a in A]
        if max(arr) < 0:
            continue
        D[n] = max(arr) + 1


def main():
    dp(N)
    n = N
    ans = ""
    while n > 0:
        arr = [
            a for a in A if n - costs[a - 1] >= 0 and D[n - costs[a - 1]] == D[n] - 1
        ]
        if len(arr) > 0:
            a = max(arr)
            ans += str(a)
            n -= costs[a - 1]
        # else:
        #     break
    print(ans)


main()

-#!/mnt/c/Users/moiki/bash/env/bin/python +costs = [2, 5, 5, 4, 5, 6, 3, 7, 6] -cost = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] -dp = [-1] * (N + 1) -dp[0] = 0 -for i in range(N): -    if dp[i] == -1: -        continue -    for c, a in zip([cost[a] for a in A], A): -        if i + c <= N: -            dp[i + c] = dp[i] + 1 -# print(dp) -i = N -ans = "" -A = sorted(A, reverse=True) -while i > 0: -    for c, a in zip([cost[a] for a in A], A): -        if i - c >= 0 and dp[i - c] == dp[i] - 1: +neginf = -1 * 1e20 +D = [neginf] * (N + 1) + + +def dp(n_match): +    D[0] = 0 +    for n in range(1, n_match + 1): +        arr = [D[n - costs[a - 1]] if n - costs[a - 1] >= 0 else neginf for a in A] +        if max(arr) < 0: +            continue +        D[n] = max(arr) + 1 + + +def main(): +    dp(N) +    n = N +    ans = "" +    while n > 0: +        arr = [ +            a for a in A if n - costs[a - 1] >= 0 and D[n - costs[a - 1]] == D[n] - 1 +        ] +        if len(arr) > 0: +            a = max(arr) -            i -= c -            break  # difficult !! -print(ans) +            n -= costs[a - 1] +        # else: +        #     break +    print(ans) + + +main()