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

A = [i for i in input().split()]



# dp[i] = i本使ってできる最大の数



def mx(a, b):

    if a is not None and b is None:

        return a

    elif a is None and b is not None:

        return b

    if len(a) != len(b):

        if len(a) > len(b):

            return a

        else:

            return b

    return max(a, b)



# xを作るために使うマッチの本数

h = {'1':2, '2':5, '3':5, '4':4, '5':5, '6':6, '7':3, '8':7, '9':6}



dp = [None] * (N + 1)

dp[0] = ''



for i in range(N + 1):

    for a in A:

        if i - h[a] < 0: continue

        if dp[i-h[a]] is None and dp[i] is None:

            continue

        try:

            dp[i] = mx(dp[i-h[a]] + a, dp[i])

        except:

            continue



print((dp[N]))
N, M = list(map(int, input().split()))

A = sorted([int(i) for i in input().split()], reverse=True)



# xを作るために使うマッチの本数

h = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}



# dp[i] = i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数

dp = [-1] * (N + 1)

dp[0] = 0



for i in range(N + 1):

    for a in A:

        if i - h[a] < 0: continue

        dp[i] = max(dp[i-h[a]] + 1, dp[i])



digit = dp[N]

ans = ''



n = N

for d in range(digit):

    for a in A:

        if n - h[a] < 0:continue

        if dp[n - h[a]] == digit - 1:

            ans += str(a)

            n -= h[a]

            digit -= 1

            break

print(ans)
N, M = list(map(int, input().split()))
A = [i for i in input().split()]
# dp[i] = i本使ってできる最大の数
def mx(a, b):
    if a is not None and b is None:
        return a
    elif a is None and b is not None:
        return b
    if len(a) != len(b):
        if len(a) > len(b):
            return a
        else:
            return b
    return max(a, b)


# xを作るために使うマッチの本数
h = {"1": 2, "2": 5, "3": 5, "4": 4, "5": 5, "6": 6, "7": 3, "8": 7, "9": 6}
dp = [None] * (N + 1)
dp[0] = ""
for i in range(N + 1):
    for a in A:
        if i - h[a] < 0:
            continue
        if dp[i - h[a]] is None and dp[i] is None:
            continue
        try:
            dp[i] = mx(dp[i - h[a]] + a, dp[i])
        except:
            continue
print((dp[N]))

N, M = list(map(int, input().split()))
A = sorted([int(i) for i in input().split()], reverse=True)
# xを作るために使うマッチの本数
h = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
# dp[i] = i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数
dp = [-1] * (N + 1)
dp[0] = 0
for i in range(N + 1):
    for a in A:
        if i - h[a] < 0:
            continue
        dp[i] = max(dp[i - h[a]] + 1, dp[i])
digit = dp[N]
ans = ""
n = N
for d in range(digit):
    for a in A:
        if n - h[a] < 0:
            continue
        if dp[n - h[a]] == digit - 1:
            ans += str(a)
            n -= h[a]
            digit -= 1
            break
print(ans)

-A = [i for i in input().split()] -# dp[i] = i本使ってできる最大の数 -def mx(a, b): -    if a is not None and b is None: -        return a -    elif a is None and b is not None: -        return b -    if len(a) != len(b): -        if len(a) > len(b): -            return a -        else: -            return b -    return max(a, b) - - +A = sorted([int(i) for i in input().split()], reverse=True) -h = {"1": 2, "2": 5, "3": 5, "4": 4, "5": 5, "6": 6, "7": 3, "8": 7, "9": 6} -dp = [None] * (N + 1) -dp[0] = "" +h = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} +# dp[i] = i 本のマッチ棒を使って、条件を満たす整数を作るときの最大桁数 +dp = [-1] * (N + 1) +dp[0] = 0 -        if dp[i - h[a]] is None and dp[i] is None: +        dp[i] = max(dp[i - h[a]] + 1, dp[i]) +digit = dp[N] +ans = "" +n = N +for d in range(digit): +    for a in A: +        if n - h[a] < 0: -        try: -            dp[i] = mx(dp[i - h[a]] + a, dp[i]) -        except: -            continue -print((dp[N])) +        if dp[n - h[a]] == digit - 1: +            ans += str(a) +            n -= h[a] +            digit -= 1 +            break +print(ans)