import sys

input = sys.stdin.readline


def main():
    N, M = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

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

    dp = [0] * (N + 1)

    for i in range(1, N + 1):
        for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

    print(dp[-1])



if __name__ == '__main__':
    main() import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def main():
    N, M = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

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

    dp = [0] * (N + 1)

    for i in range(1, N + 1):
        for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

    print(dp[-1]) main int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x N, M = [int(x) for x in input().split()] N [int(x) for x in input().split()] M int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x A = [int(x) for x in input().split()] A [int(x) for x in input().split()] match = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] match [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 dp = [0] * (N + 1) dp [0] * (N + 1) [0] 0 N + 1 N 1 for i in range(1, N + 1):
        for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

     i range(1, N + 1) range 1 N + 1 N 1 for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

     a A if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

     match[a] <= i match[a] match a i if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

     match[a] == i or dp[i - match[a]] != 0 match[a] == i match[a] match a i dp[i - match[a]] != 0 dp[i - match[a]] dp i - match[a] i match[a] match a 0 dp[i] = max(dp[i], dp[i - match[a]] * 10 + a) dp[i] dp i max(dp[i], dp[i - match[a]] * 10 + a) max dp[i] dp i dp[i - match[a]] * 10 + a dp[i - match[a]] * 10 dp[i - match[a]] dp i - match[a] i match[a] match a 10 a print(dp[-1]) print dp[-1] dp -1 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    N, M = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

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

    dp = [0] * (N + 1)

    for i in range(1, N + 1):
        for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

    print(dp[-1]) def main():
    N, M = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

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

    dp = [0] * (N + 1)

    for i in range(1, N + 1):
        for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

    print(dp[-1]) main input = sys.stdin.readline sys.stdin.readline input
import sys

input = sys.stdin.readline


def main():
    N, M = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

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

    dp = [0] * (N + 1)

    for i in range(1, N + 1):
        for a in A:
            if match[a] <= i:
                if match[a] == i or dp[i - match[a]] != 0:
                    dp[i] = max(dp[i], dp[i - match[a]] * 10 + a)

    print(dp[-1])



if __name__ == '__main__':
    main()
