# 解説放送の１つめ

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

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

f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed



# dp[i]: マッチi本数で作れる最大の数(str)

dp = [None]*(N+1)

dp[0] = ""



# strの数の大きいほう

def max_str(s1:str, s2:str)->str:

    """return bigger one"""

    l1, l2 = len(s1), len(s2)

    if l1 != l2:

        return s1 if l1>l2 else s2

    if l1 == l2: 

        return s1 if s1>s2 else s2

    

for i in range(2, N+1):

    nxt = ""

    for aj in A:

        # 配列外参照と，i本ちょうどで作れないものはだめ

        if i-f[aj]<0 or dp[i-f[aj]]==None: continue

        nxt = max_str(nxt, 

                      dp[i-f[aj]]+str(aj))

    if nxt!="":

        dp[i] = nxt

    #print(dp)

print((dp[N]))

# dpに最大桁数を持たせる

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

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

A.sort(reverse=True)

f = [-1, 2,5,5,4,5,6,3,7,6] # 1-indexed



# dp[i]: マッチi本数で作れる最大の桁数(int)

dp = [-float('inf')]*(N+1)

dp[0] = 0

for i in range(N+1):

    nxt = dp[i]

    for aj in A:

        if i-f[aj]<0: continue

        nxt = max(nxt, 

                  dp[i-f[aj]] + 1)

    dp[i] = nxt

    #print(dp)

#print(dp[N])

keta = dp[N]

ans = ""

matchs = N

# 上位の桁から貪欲に

while keta>0:

    for aj in A:

        if matchs-f[aj] < 0: continue

        if dp[matchs-f[aj]] == keta-1:

            ans += str(aj)

            matchs -= f[aj]

            keta -= 1

            break

print(ans)

# 解説放送の１つめ
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
f = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]  # 1-indexed
# dp[i]: マッチi本数で作れる最大の数(str)
dp = [None] * (N + 1)
dp[0] = ""
# strの数の大きいほう
def max_str(s1: str, s2: str) -> str:
    """return bigger one"""
    l1, l2 = len(s1), len(s2)
    if l1 != l2:
        return s1 if l1 > l2 else s2
    if l1 == l2:
        return s1 if s1 > s2 else s2


for i in range(2, N + 1):
    nxt = ""
    for aj in A:
        # 配列外参照と，i本ちょうどで作れないものはだめ
        if i - f[aj] < 0 or dp[i - f[aj]] == None:
            continue
        nxt = max_str(nxt, dp[i - f[aj]] + str(aj))
    if nxt != "":
        dp[i] = nxt
    # print(dp)
print((dp[N]))

# dpに最大桁数を持たせる
N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)
f = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]  # 1-indexed
# dp[i]: マッチi本数で作れる最大の桁数(int)
dp = [-float("inf")] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    nxt = dp[i]
    for aj in A:
        if i - f[aj] < 0:
            continue
        nxt = max(nxt, dp[i - f[aj]] + 1)
    dp[i] = nxt
    # print(dp)
# print(dp[N])
keta = dp[N]
ans = ""
matchs = N
# 上位の桁から貪欲に
while keta > 0:
    for aj in A:
        if matchs - f[aj] < 0:
            continue
        if dp[matchs - f[aj]] == keta - 1:
            ans += str(aj)
            matchs -= f[aj]
            keta -= 1
            break
print(ans)

-# 解説放送の１つめ +# dpに最大桁数を持たせる +A.sort(reverse=True) -# dp[i]: マッチi本数で作れる最大の数(str) -dp = [None] * (N + 1) -dp[0] = "" -# strの数の大きいほう -def max_str(s1: str, s2: str) -> str: -    """return bigger one""" -    l1, l2 = len(s1), len(s2) -    if l1 != l2: -        return s1 if l1 > l2 else s2 -    if l1 == l2: -        return s1 if s1 > s2 else s2 - - -for i in range(2, N + 1): -    nxt = "" +# dp[i]: マッチi本数で作れる最大の桁数(int) +dp = [-float("inf")] * (N + 1) +dp[0] = 0 +for i in range(N + 1): +    nxt = dp[i] -        # 配列外参照と，i本ちょうどで作れないものはだめ -        if i - f[aj] < 0 or dp[i - f[aj]] == None: +        if i - f[aj] < 0: -        nxt = max_str(nxt, dp[i - f[aj]] + str(aj)) -    if nxt != "": -        dp[i] = nxt +        nxt = max(nxt, dp[i - f[aj]] + 1) +    dp[i] = nxt -print((dp[N])) +# print(dp[N]) +keta = dp[N] +ans = "" +matchs = N +# 上位の桁から貪欲に +while keta > 0: +    for aj in A: +        if matchs - f[aj] < 0: +            continue +        if dp[matchs - f[aj]] == keta - 1: +            ans += str(aj) +            matchs -= f[aj] +            keta -= 1 +            break +print(ans)