from collections import defaultdict
N,M,*A = map(int, open(0).read().split())
ls = [0]*10
ls[1] = 2
ls[2] = 5
ls[3] = 5
ls[4] = 4
ls[5] = 5
ls[6] = 6
ls[7] = 3
ls[8] = 7
ls[9] = 6
dic = defaultdict(int)
for a in A:
  k = ls[a]
  dic[k] = max(dic[k],a)
dp = [-1]*(N+1)
for k in dic.keys():
  if k>N:
    continue
  dp[k] = dic[k]
for i in range(N+1):
  for k in dic.keys():
    if i-k>0 and dp[i-k]!=-1:
      m = dp[i-k]*10+dic[k]
      dp[i] = max(dp[i], m)
print(dp[N]) from collections import defaultdict N,M,*A = map(int, open(0).read().split()) N map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split M *A A ls = [0]*10 ls [0]*10 [0] 0 10 ls[1] = 2 ls[1] ls 1 2 ls[2] = 5 ls[2] ls 2 5 ls[3] = 5 ls[3] ls 3 5 ls[4] = 4 ls[4] ls 4 4 ls[5] = 5 ls[5] ls 5 5 ls[6] = 6 ls[6] ls 6 6 ls[7] = 3 ls[7] ls 7 3 ls[8] = 7 ls[8] ls 8 7 ls[9] = 6 ls[9] ls 9 6 dic = defaultdict(int) dic defaultdict(int) defaultdict int for a in A:
  k = ls[a]
  dic[k] = max(dic[k],a) a A k = ls[a] k ls[a] ls a dic[k] = max(dic[k],a) dic[k] dic k max(dic[k],a) max dic[k] dic k a dp = [-1]*(N+1) dp [-1]*(N+1) [-1] -1 N+1 N 1 for k in dic.keys():
  if k>N:
    continue
  dp[k] = dic[k] k dic.keys() dic.keys dic keys if k>N:
    continue
   k>N k N continue dp[k] = dic[k] dp[k] dp k dic[k] dic k for i in range(N+1):
  for k in dic.keys():
    if i-k>0 and dp[i-k]!=-1:
      m = dp[i-k]*10+dic[k]
      dp[i] = max(dp[i], m) i range(N+1) range N+1 N 1 for k in dic.keys():
    if i-k>0 and dp[i-k]!=-1:
      m = dp[i-k]*10+dic[k]
      dp[i] = max(dp[i], m) k dic.keys() dic.keys dic keys if i-k>0 and dp[i-k]!=-1:
      m = dp[i-k]*10+dic[k]
      dp[i] = max(dp[i], m) i-k>0 and dp[i-k]!=-1 i-k>0 i-k i k 0 dp[i-k]!=-1 dp[i-k] dp i-k i k -1 m = dp[i-k]*10+dic[k] m dp[i-k]*10+dic[k] dp[i-k]*10 dp[i-k] dp i-k i k 10 dic[k] dic k dp[i] = max(dp[i], m) dp[i] dp i max(dp[i], m) max dp[i] dp i m print(dp[N]) print dp[N] dp N dic = defaultdict(int) defaultdict(int) dic k = ls[a] ls[a] k dp = [-1]*(N+1) [-1]*(N+1) dp N,M,*A = map(int, open(0).read().split()) map(int, open(0).read().split()) N M,*A = map(int, open(0).read().split()) map(int, open(0).read().split()) M ls = [0]*10 [0]*10 ls m = dp[i-k]*10+dic[k] dp[i-k]*10+dic[k] m
from collections import defaultdict
N,M,*A = map(int, open(0).read().split())
ls = [0]*10
ls[1] = 2
ls[2] = 5
ls[3] = 5
ls[4] = 4
ls[5] = 5
ls[6] = 6
ls[7] = 3
ls[8] = 7
ls[9] = 6
dic = defaultdict(int)
for a in A:
  k = ls[a]
  dic[k] = max(dic[k],a)
dp = [-1]*(N+1)
for k in dic.keys():
  if k>N:
    continue
  dp[k] = dic[k]
for i in range(N+1):
  for k in dic.keys():
    if i-k>0 and dp[i-k]!=-1:
      m = dp[i-k]*10+dic[k]
      dp[i] = max(dp[i], m)
print(dp[N])