import sys
from collections import defaultdict as dd
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.buffer.readline
N, M = map(int, input().split())
nums = list(map(int, input().split()))
ms = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] * (N + 1)
for i in range(N):
  for n in nums:
    if i + ms[n] > N: continue
    if i != 0 and dp[i] == 0: continue
    dp[i + ms[n]] = max(dp[i] * 10 + n, dp[i + ms[n]])
print(dp[N]) import sys sys from collections import defaultdict as dd sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 input = sys.stdin.buffer.readline input sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M nums = list(map(int, input().split())) nums list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ms = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] ms [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 dp = [0] * (N + 1) dp [0] * (N + 1) [0] 0 N + 1 N 1 for i in range(N):
  for n in nums:
    if i + ms[n] > N: continue
    if i != 0 and dp[i] == 0: continue
    dp[i + ms[n]] = max(dp[i] * 10 + n, dp[i + ms[n]]) i range(N) range N for n in nums:
    if i + ms[n] > N: continue
    if i != 0 and dp[i] == 0: continue
    dp[i + ms[n]] = max(dp[i] * 10 + n, dp[i + ms[n]]) n nums if i + ms[n] > N: continue
     i + ms[n] > N i + ms[n] i ms[n] ms n N if i != 0 and dp[i] == 0: continue
     i != 0 and dp[i] == 0 i != 0 i 0 dp[i] == 0 dp[i] dp i 0 dp[i + ms[n]] = max(dp[i] * 10 + n, dp[i + ms[n]]) dp[i + ms[n]] dp i + ms[n] i ms[n] ms n max(dp[i] * 10 + n, dp[i + ms[n]]) max dp[i] * 10 + n dp[i] * 10 dp[i] dp i 10 n dp[i + ms[n]] dp i + ms[n] i ms[n] ms n print(dp[N]) print dp[N] dp N M = map(int, input().split()) map(int, input().split()) M ms = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] ms dp = [0] * (N + 1) [0] * (N + 1) dp input = sys.stdin.buffer.readline sys.stdin.buffer.readline input nums = list(map(int, input().split())) list(map(int, input().split())) nums N, M = map(int, input().split()) map(int, input().split()) N
import sys
from collections import defaultdict as dd
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.buffer.readline
N, M = map(int, input().split())
nums = list(map(int, input().split()))
ms = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
dp = [0] * (N + 1)
for i in range(N):
  for n in nums:
    if i + ms[n] > N: continue
    if i != 0 and dp[i] == 0: continue
    dp[i + ms[n]] = max(dp[i] * 10 + n, dp[i + ms[n]])
print(dp[N])