Matches = {num: c for num, c in zip([1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 5, 5, 4, 5, 6, 3, 7, 6])}





def solve(N, M, As):

    dp = [-1 for _ in range(N + 1)]

    dp[0] = 0

    for i in range(N):

        for a in sorted(As, reverse=True):

            c = Matches[a]

            if dp[i] == -1:

                continue

            if i + c <= N:

                dp[i + c] = max(dp[i+c],

                                dp[i] * 10 + a)

    return dp[N]



if __name__ == "__main__":

    N, M = tuple(map(int, input().split(" ")))

    As = list(map(int, input().split(" ")))

    print((solve(N, M, As)))

Matches = {num: c for num, c in zip([1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 5, 5, 4, 5, 6, 3, 7, 6])}





def solve(N, M, As):

    dp = [-1 for _ in range(N + 1)]

    dp[0] = 0

    for i in range(N + 1):

        for a in As:

            c = Matches[a]

            if dp[i] == -1:

                continue

            if i + c <= N:

                dp[i + c] = max(dp[i + c],

                                dp[i] * 10 + a)

    return dp[N]



if __name__ == "__main__":

    N, M = tuple(map(int, input().split(" ")))

    As = list(map(int, input().split(" ")))

    print((solve(N, M, As)))

Matches = {
    num: c for num, c in zip([1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 5, 5, 4, 5, 6, 3, 7, 6])
}


def solve(N, M, As):
    dp = [-1 for _ in range(N + 1)]
    dp[0] = 0
    for i in range(N):
        for a in sorted(As, reverse=True):
            c = Matches[a]
            if dp[i] == -1:
                continue
            if i + c <= N:
                dp[i + c] = max(dp[i + c], dp[i] * 10 + a)
    return dp[N]


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    As = list(map(int, input().split(" ")))
    print((solve(N, M, As)))

Matches = {
    num: c for num, c in zip([1, 2, 3, 4, 5, 6, 7, 8, 9], [2, 5, 5, 4, 5, 6, 3, 7, 6])
}


def solve(N, M, As):
    dp = [-1 for _ in range(N + 1)]
    dp[0] = 0
    for i in range(N + 1):
        for a in As:
            c = Matches[a]
            if dp[i] == -1:
                continue
            if i + c <= N:
                dp[i + c] = max(dp[i + c], dp[i] * 10 + a)
    return dp[N]


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    As = list(map(int, input().split(" ")))
    print((solve(N, M, As)))

-    for i in range(N): -        for a in sorted(As, reverse=True): +    for i in range(N + 1): +        for a in As: