def is_big(x = [0]*10, y = [0]*10):
    if sum(x) > sum(y):
        return True
    if sum(x) < sum(y):
        return False
    for i in range(0,10)[::-1]:
        if x[i] > y[i]:
            return True
        if y[i] > x[i]:
            return False
    return True
  
n,m = map(int,input().split())
a = sorted(list(map(int,input().split())))
#print(a)
d = [0]*8
b = []
L1 = [1,7,4,2,3,5,6,9,8]
L2 = [2,3,4,5,5,5,6,6,7]
for i in range(9):
  if L1[i] in a:
    d[L2[i]] = L1[i]
    if L2[i] not in b:
      b.append(L2[i])
#print(d)
#print(b)
p = []
for i in range(n+1):
  p.append([-1,0,0,0,0,0,0,0,0,0])
  
p[0][0] = 0
for i in range(1,n+1):
  for t in b:
    if i-t <0 or p[i-t][0] == -1: 
      continue
    q = p[i-t][:]
    q[d[t]] += 1
    if is_big(q,p[i]):
      p[i] = q

a = ""
for i in range(10)[::-1]:
  a += str(i)*p[n][i]
print(a) def is_big(x = [0]*10, y = [0]*10):
    if sum(x) > sum(y):
        return True
    if sum(x) < sum(y):
        return False
    for i in range(0,10)[::-1]:
        if x[i] > y[i]:
            return True
        if y[i] > x[i]:
            return False
    return True
   is_big if sum(x) > sum(y):
        return True
     sum(x) > sum(y) sum(x) sum x sum(y) sum y return True True if sum(x) < sum(y):
        return False
     sum(x) < sum(y) sum(x) sum x sum(y) sum y return False False for i in range(0,10)[::-1]:
        if x[i] > y[i]:
            return True
        if y[i] > x[i]:
            return False
     i range(0,10)[::-1] (0,10) range 0 10 ::-1 -1 if x[i] > y[i]:
            return True
         x[i] > y[i] x[i] x i y[i] y i return True True if y[i] > x[i]:
            return False
     y[i] > x[i] y[i] y i x[i] x i return False False return True True x = [0]*10 x [0]*10 [0] 0 10 y = [0]*10 y [0]*10 [0] 0 10 n,m = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split m a = sorted(list(map(int,input().split()))) a sorted(list(map(int,input().split()))) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split d = [0]*8 d [0]*8 [0] 0 8 b = [] b [] L1 = [1,7,4,2,3,5,6,9,8] L1 [1,7,4,2,3,5,6,9,8] 1 7 4 2 3 5 6 9 8 L2 = [2,3,4,5,5,5,6,6,7] L2 [2,3,4,5,5,5,6,6,7] 2 3 4 5 5 5 6 6 7 for i in range(9):
  if L1[i] in a:
    d[L2[i]] = L1[i]
    if L2[i] not in b:
      b.append(L2[i])
#print(d)
#print(b) i range(9) range 9 if L1[i] in a:
    d[L2[i]] = L1[i]
    if L2[i] not in b:
      b.append(L2[i])
#print(d)
#print(b) L1[i] in a L1[i] L1 i a d[L2[i]] = L1[i] d[L2[i]] d L2[i] L2 i L1[i] L1 i if L2[i] not in b:
      b.append(L2[i])
#print(d)
#print(b) L2[i] not in b L2[i] L2 i b b.append(L2[i]) b.append b append L2[i] L2 i p = [] p [] for i in range(n+1):
  p.append([-1,0,0,0,0,0,0,0,0,0])
   i range(n+1) range n+1 n 1 p.append([-1,0,0,0,0,0,0,0,0,0]) p.append p append [-1,0,0,0,0,0,0,0,0,0] -1 0 0 0 0 0 0 0 0 0 p[0][0] = 0 p[0][0] [0] p 0 0 0 for i in range(1,n+1):
  for t in b:
    if i-t <0 or p[i-t][0] == -1: 
      continue
    q = p[i-t][:]
    q[d[t]] += 1
    if is_big(q,p[i]):
      p[i] = q i range(1,n+1) range 1 n+1 n 1 for t in b:
    if i-t <0 or p[i-t][0] == -1: 
      continue
    q = p[i-t][:]
    q[d[t]] += 1
    if is_big(q,p[i]):
      p[i] = q t b if i-t <0 or p[i-t][0] == -1: 
      continue
     i-t <0 or p[i-t][0] == -1 i-t <0 i-t i t 0 p[i-t][0] == -1 p[i-t][0] [i-t] p i-t i t 0 -1 continue q = p[i-t][:] q p[i-t][:] [i-t] p i-t i t : q[d[t]] += 1 q[d[t]] q d[t] d t 1 if is_big(q,p[i]):
      p[i] = q is_big(q,p[i]) is_big q p[i] p i p[i] = q p[i] p i q a = "" a "" for i in range(10)[::-1]:
  a += str(i)*p[n][i] i range(10)[::-1] (10) range 10 ::-1 -1 a += str(i)*p[n][i] a str(i)*p[n][i] str(i) str i p[n][i] [n] p n i print(a) print a a = sorted(list(map(int,input().split()))) sorted(list(map(int,input().split()))) a b = [] [] b q = p[i-t][:] p[i-t][:] q a = "" "" a d = [0]*8 [0]*8 d a += str(i)*p[n][i] str(i)*p[n][i] a p = [] [] p def is_big(x = [0]*10, y = [0]*10):
    if sum(x) > sum(y):
        return True
    if sum(x) < sum(y):
        return False
    for i in range(0,10)[::-1]:
        if x[i] > y[i]:
            return True
        if y[i] > x[i]:
            return False
    return True
   def is_big(x = [0]*10, y = [0]*10):
    if sum(x) > sum(y):
        return True
    if sum(x) < sum(y):
        return False
    for i in range(0,10)[::-1]:
        if x[i] > y[i]:
            return True
        if y[i] > x[i]:
            return False
    return True
   is_big L1 = [1,7,4,2,3,5,6,9,8] [1,7,4,2,3,5,6,9,8] L1 n,m = map(int,input().split()) map(int,input().split()) n m = map(int,input().split()) map(int,input().split()) m L2 = [2,3,4,5,5,5,6,6,7] [2,3,4,5,5,5,6,6,7] L2
def is_big(x = [0]*10, y = [0]*10):
    if sum(x) > sum(y):
        return True
    if sum(x) < sum(y):
        return False
    for i in range(0,10)[::-1]:
        if x[i] > y[i]:
            return True
        if y[i] > x[i]:
            return False
    return True
  
n,m = map(int,input().split())
a = sorted(list(map(int,input().split())))
#print(a)
d = [0]*8
b = []
L1 = [1,7,4,2,3,5,6,9,8]
L2 = [2,3,4,5,5,5,6,6,7]
for i in range(9):
  if L1[i] in a:
    d[L2[i]] = L1[i]
    if L2[i] not in b:
      b.append(L2[i])
#print(d)
#print(b)
p = []
for i in range(n+1):
  p.append([-1,0,0,0,0,0,0,0,0,0])
  
p[0][0] = 0
for i in range(1,n+1):
  for t in b:
    if i-t <0 or p[i-t][0] == -1: 
      continue
    q = p[i-t][:]
    q[d[t]] += 1
    if is_big(q,p[i]):
      p[i] = q

a = ""
for i in range(10)[::-1]:
  a += str(i)*p[n][i]
print(a)