import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

    needs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)

    dp = [-1]*(N+1)
    # 初期条件
    dp[0] = 0

    # ループ
    for a in A:
        na = needs[a-1]
        for i in range(N+1):
            if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

    print(dp[N])


resolve() import sys sys sys.setrecursionlimit(10 ** 5 + 10) sys.setrecursionlimit sys setrecursionlimit 10 ** 5 + 10 10 ** 5 10 5 10 def input(): return sys.stdin.readline().strip() input def resolve():

    needs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)

    dp = [-1]*(N+1)
    # 初期条件
    dp[0] = 0

    # ループ
    for a in A:
        na = needs[a-1]
        for i in range(N+1):
            if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

    print(dp[N]) resolve needs = [2, 5, 5, 4, 5, 6, 3, 7, 6] needs [2, 5, 5, 4, 5, 6, 3, 7, 6] 2 5 5 4 5 6 3 7 6 N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = sorted(list(map(int, input().split())), reverse=True) A sorted(list(map(int, input().split())), reverse=True) sorted list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split reverse=True reverse True dp = [-1]*(N+1) dp [-1]*(N+1) [-1] -1 N+1 N 1 dp[0] = 0 dp[0] dp 0 0 for a in A:
        na = needs[a-1]
        for i in range(N+1):
            if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

     a A na = needs[a-1] na needs[a-1] needs a-1 a 1 for i in range(N+1):
            if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

     i range(N+1) range N+1 N 1 if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

     i-na >= 0 i-na i na 0 if dp[i-na] == -1:
                    continue
                 dp[i-na] == -1 dp[i-na] dp i-na i na -1 continue dp[i] = max(dp[i], dp[i-na]*10+a) dp[i] dp i max(dp[i], dp[i-na]*10+a) max dp[i] dp i dp[i-na]*10+a dp[i-na]*10 dp[i-na] dp i-na i na 10 a print(dp[N]) print dp[N] dp N resolve() resolve def input(): return sys.stdin.readline().strip() def input(): return sys.stdin.readline().strip() input def resolve():

    needs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)

    dp = [-1]*(N+1)
    # 初期条件
    dp[0] = 0

    # ループ
    for a in A:
        na = needs[a-1]
        for i in range(N+1):
            if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

    print(dp[N]) def resolve():

    needs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)

    dp = [-1]*(N+1)
    # 初期条件
    dp[0] = 0

    # ループ
    for a in A:
        na = needs[a-1]
        for i in range(N+1):
            if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

    print(dp[N]) resolve
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

    needs = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    N, M = map(int, input().split())
    A = sorted(list(map(int, input().split())), reverse=True)

    dp = [-1]*(N+1)
    # 初期条件
    dp[0] = 0

    # ループ
    for a in A:
        na = needs[a-1]
        for i in range(N+1):
            if i-na >= 0:
                if dp[i-na] == -1:
                    continue
                dp[i] = max(dp[i], dp[i-na]*10+a)

    print(dp[N])


resolve()