import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

#桁数のMAX
dp = [-1] * (N+1)
dp[0] = 0

for a in A:
    for i in range(match_dict[a], N+1):
        if dp[i - match_dict[a]] >= 0:
            dp[i] = max(dp[i], dp[i - match_dict[a]] + 1)

ans = ''
A.sort(reverse=True)

i = N
while i:
    for a in A:
        if i - match_dict[a] >= 0 and dp[i - match_dict[a]] == dp[i] - 1:
            ans += str(a)
            i -= match_dict[a]
            break

print(ans) import sys sys input = lambda: sys.stdin.readline().rstrip() input lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip match_dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} match_dict {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 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 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:
    for i in range(match_dict[a], N+1):
        if dp[i - match_dict[a]] >= 0:
            dp[i] = max(dp[i], dp[i - match_dict[a]] + 1) a A for i in range(match_dict[a], N+1):
        if dp[i - match_dict[a]] >= 0:
            dp[i] = max(dp[i], dp[i - match_dict[a]] + 1) i range(match_dict[a], N+1) range match_dict[a] match_dict a N+1 N 1 if dp[i - match_dict[a]] >= 0:
            dp[i] = max(dp[i], dp[i - match_dict[a]] + 1) dp[i - match_dict[a]] >= 0 dp[i - match_dict[a]] dp i - match_dict[a] i match_dict[a] match_dict a 0 dp[i] = max(dp[i], dp[i - match_dict[a]] + 1) dp[i] dp i max(dp[i], dp[i - match_dict[a]] + 1) max dp[i] dp i dp[i - match_dict[a]] + 1 dp[i - match_dict[a]] dp i - match_dict[a] i match_dict[a] match_dict a 1 ans = '' ans '' A.sort(reverse=True) A.sort A sort reverse=True reverse True i = N i N while i:
    for a in A:
        if i - match_dict[a] >= 0 and dp[i - match_dict[a]] == dp[i] - 1:
            ans += str(a)
            i -= match_dict[a]
            break i for a in A:
        if i - match_dict[a] >= 0 and dp[i - match_dict[a]] == dp[i] - 1:
            ans += str(a)
            i -= match_dict[a]
            break a A if i - match_dict[a] >= 0 and dp[i - match_dict[a]] == dp[i] - 1:
            ans += str(a)
            i -= match_dict[a]
            break i - match_dict[a] >= 0 and dp[i - match_dict[a]] == dp[i] - 1 i - match_dict[a] >= 0 i - match_dict[a] i match_dict[a] match_dict a 0 dp[i - match_dict[a]] == dp[i] - 1 dp[i - match_dict[a]] dp i - match_dict[a] i match_dict[a] match_dict a dp[i] - 1 dp[i] dp i 1 ans += str(a) ans str(a) str a i -= match_dict[a] i match_dict[a] match_dict a break print(ans) print ans i -= match_dict[a] match_dict[a] i ans = '' '' ans i = N N i input = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() input M = map(int, input().split()) map(int, input().split()) M A = list(map(int, input().split())) list(map(int, input().split())) A ans += str(a) str(a) ans dp = [-1] * (N+1) [-1] * (N+1) dp match_dict = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} match_dict N,M = map(int, input().split()) map(int, input().split()) N
import sys
input = lambda: sys.stdin.readline().rstrip()

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

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

#桁数のMAX
dp = [-1] * (N+1)
dp[0] = 0

for a in A:
    for i in range(match_dict[a], N+1):
        if dp[i - match_dict[a]] >= 0:
            dp[i] = max(dp[i], dp[i - match_dict[a]] + 1)

ans = ''
A.sort(reverse=True)

i = N
while i:
    for a in A:
        if i - match_dict[a] >= 0 and dp[i - match_dict[a]] == dp[i] - 1:
            ans += str(a)
            i -= match_dict[a]
            break

print(ans)