def f(x):

    k = int(x)

    return c[k], k





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

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

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

A = sorted(A, key=lambda x: x[0] * 10 - x[1])



# 数字を作成するのに必要なマッチ数のリストを作成

b = []

dic = {}

n = 0

for d in A:

    if n != d[0]:

        b.append(d[0])

        n = d[0]

        dic[n] = d[1]



ans = [0] * b[0]

n = b[0]

while True:

    max_ = 0

    for d in b:

        if n - d == 0:

            if dic[d] > max_:

                max_ = dic[d]

        elif n - d > 0 and ans[n - d] > 0:

            t = ans[n - d] * 10 + dic[d]

            max_ = max(max_, t)



    if n == N:

        print(max_)

        break

    ans.append(max_)

    n += 1
def f(x):

    k = int(x)

    return c[k], k



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



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

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

A = sorted(A, key=lambda x: x[0] * 10 - x[1])



b = []

dic = {}

n = 0

for d in A:

    if n != d[0]:

        b.append(d[0])

        n = d[0]

        dic[n] = d[1]



ans = [0] * b[0]

n = b[0]

count = 0

while True:

    max_ = 0

    for d in b:

        if n - d == 0:

            if dic[d] > max_:

                max_ = dic[d]

        elif n - d > 0 and ans[n - d] > 0:

            t = ans[n - d] * 10 + dic[d]

            max_ = max(max_, t)



    if n == N:

        print(max_)

        break;

    if max_ > 0 and (N - n) % b[0] == 0:

        if count < 2:

            count += 1

        else:

            print((''.join(sorted(list(str(max_) + str(dic[b[0]])*((N - n)//b[0])), reverse=True))))

            break

    ans.append(max_)

    n += 1
def f(x):
    k = int(x)
    return c[k], k


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = list(map(int, input().split()))
A = list(map(f, input().split()))
A = sorted(A, key=lambda x: x[0] * 10 - x[1])
# 数字を作成するのに必要なマッチ数のリストを作成
b = []
dic = {}
n = 0
for d in A:
    if n != d[0]:
        b.append(d[0])
        n = d[0]
        dic[n] = d[1]
ans = [0] * b[0]
n = b[0]
while True:
    max_ = 0
    for d in b:
        if n - d == 0:
            if dic[d] > max_:
                max_ = dic[d]
        elif n - d > 0 and ans[n - d] > 0:
            t = ans[n - d] * 10 + dic[d]
            max_ = max(max_, t)
    if n == N:
        print(max_)
        break
    ans.append(max_)
    n += 1

def f(x):
    k = int(x)
    return c[k], k


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = list(map(int, input().split()))
A = list(map(f, input().split()))
A = sorted(A, key=lambda x: x[0] * 10 - x[1])
b = []
dic = {}
n = 0
for d in A:
    if n != d[0]:
        b.append(d[0])
        n = d[0]
        dic[n] = d[1]
ans = [0] * b[0]
n = b[0]
count = 0
while True:
    max_ = 0
    for d in b:
        if n - d == 0:
            if dic[d] > max_:
                max_ = dic[d]
        elif n - d > 0 and ans[n - d] > 0:
            t = ans[n - d] * 10 + dic[d]
            max_ = max(max_, t)
    if n == N:
        print(max_)
        break
    if max_ > 0 and (N - n) % b[0] == 0:
        if count < 2:
            count += 1
        else:
            print(
                (
                    "".join(
                        sorted(
                            list(str(max_) + str(dic[b[0]]) * ((N - n) // b[0])),
                            reverse=True,
                        )
                    )
                )
            )
            break
    ans.append(max_)
    n += 1

-# 数字を作成するのに必要なマッチ数のリストを作成 +count = 0 +    if max_ > 0 and (N - n) % b[0] == 0: +        if count < 2: +            count += 1 +        else: +            print( +                ( +                    "".join( +                        sorted( +                            list(str(max_) + str(dic[b[0]]) * ((N - n) // b[0])), +                            reverse=True, +                        ) +                    ) +                ) +            ) +            break