import sys
sys.setrecursionlimit(10 ** 6)

N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10 ** 8

for i in range(9):
    if i + 1 not in A:
        num[i] = INF

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

for i in range(1, N + 1):
    for j in range(9):
        k = i - num[j]
        if k >= 0:
            dp[i] = max(dp[i], dp[k] + 1)

ans = ''
rest = N
for i in range(dp[N]):
    t = 0
    for j in range(8, -1, -1):
        k = rest - num[j]
        if k >= 0:
            if dp[rest - num[j]] == dp[rest] - 1:
                t = max(t, j + 1)
    ans += str(t)
    rest -= num[t - 1]

print(int(ans)) import sys sys sys.setrecursionlimit(10 ** 6) sys.setrecursionlimit sys setrecursionlimit 10 ** 6 10 6 N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split num = [2, 5, 5, 4, 5, 6, 3, 7, 6] num [2, 5, 5, 4, 5, 6, 3, 7, 6] 2 5 5 4 5 6 3 7 6 INF = 10 ** 8 INF 10 ** 8 10 8 for i in range(9):
    if i + 1 not in A:
        num[i] = INF i range(9) range 9 if i + 1 not in A:
        num[i] = INF i + 1 not in A i + 1 i 1 A num[i] = INF num[i] num i INF dp = [-INF] * (N + 1) dp [-INF] * (N + 1) [-INF] -INF INF N + 1 N 1 dp[0] = 0 dp[0] dp 0 0 for i in range(1, N + 1):
    for j in range(9):
        k = i - num[j]
        if k >= 0:
            dp[i] = max(dp[i], dp[k] + 1) i range(1, N + 1) range 1 N + 1 N 1 for j in range(9):
        k = i - num[j]
        if k >= 0:
            dp[i] = max(dp[i], dp[k] + 1) j range(9) range 9 k = i - num[j] k i - num[j] i num[j] num j if k >= 0:
            dp[i] = max(dp[i], dp[k] + 1) k >= 0 k 0 dp[i] = max(dp[i], dp[k] + 1) dp[i] dp i max(dp[i], dp[k] + 1) max dp[i] dp i dp[k] + 1 dp[k] dp k 1 ans = '' ans '' rest = N rest N for i in range(dp[N]):
    t = 0
    for j in range(8, -1, -1):
        k = rest - num[j]
        if k >= 0:
            if dp[rest - num[j]] == dp[rest] - 1:
                t = max(t, j + 1)
    ans += str(t)
    rest -= num[t - 1] i range(dp[N]) range dp[N] dp N t = 0 t 0 for j in range(8, -1, -1):
        k = rest - num[j]
        if k >= 0:
            if dp[rest - num[j]] == dp[rest] - 1:
                t = max(t, j + 1)
     j range(8, -1, -1) range 8 -1 -1 k = rest - num[j] k rest - num[j] rest num[j] num j if k >= 0:
            if dp[rest - num[j]] == dp[rest] - 1:
                t = max(t, j + 1)
     k >= 0 k 0 if dp[rest - num[j]] == dp[rest] - 1:
                t = max(t, j + 1)
     dp[rest - num[j]] == dp[rest] - 1 dp[rest - num[j]] dp rest - num[j] rest num[j] num j dp[rest] - 1 dp[rest] dp rest 1 t = max(t, j + 1) t max(t, j + 1) max t j + 1 j 1 ans += str(t) ans str(t) str t rest -= num[t - 1] rest num[t - 1] num t - 1 t 1 print(int(ans)) print int(ans) int ans A = list(map(int, input().split())) list(map(int, input().split())) A rest = N N rest INF = 10 ** 8 10 ** 8 INF t = max(t, j + 1) max(t, j + 1) t k = rest - num[j] rest - num[j] k k = i - num[j] i - num[j] k N, M = map(int, input().split()) map(int, input().split()) N t = 0 0 t ans += str(t) str(t) ans num = [2, 5, 5, 4, 5, 6, 3, 7, 6] [2, 5, 5, 4, 5, 6, 3, 7, 6] num dp = [-INF] * (N + 1) [-INF] * (N + 1) dp M = map(int, input().split()) map(int, input().split()) M rest -= num[t - 1] num[t - 1] rest ans = '' '' ans
import sys
sys.setrecursionlimit(10 ** 6)

N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [2, 5, 5, 4, 5, 6, 3, 7, 6]
INF = 10 ** 8

for i in range(9):
    if i + 1 not in A:
        num[i] = INF

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

for i in range(1, N + 1):
    for j in range(9):
        k = i - num[j]
        if k >= 0:
            dp[i] = max(dp[i], dp[k] + 1)

ans = ''
rest = N
for i in range(dp[N]):
    t = 0
    for j in range(8, -1, -1):
        k = rest - num[j]
        if k >= 0:
            if dp[rest - num[j]] == dp[rest] - 1:
                t = max(t, j + 1)
    ans += str(t)
    rest -= num[t - 1]

print(int(ans))
