import sys
N,M = map(int,sys.stdin.readline().rstrip().split())
A = list(map(int,sys.stdin.readline().rstrip().split()))

from collections import defaultdict

d = defaultdict(int)  # d[i] = (Aの中でi本用いて表せる最大の数)
for i in range(M):
    if A[i] == 1:
        d[2] = 1
    elif A[i] == 7:
        d[3] = 7
    elif A[i] == 4:
        d[4] = 4
    elif A[i] in [2,3,5]:
        d[5] = max(d[5],A[i])
    elif A[i] in [6,9]:
        d[6] = max(d[6],A[i])
    else:
        d[7] = 8

B = list(d.keys())
B.sort()

a = B[0]
B[0] = 0

if N % a == 0:
    print(''.join([str(d[a])]*(N//a)))
    exit()

from itertools import combinations_with_replacement

C = []

for i in range(N//a,max(-1,N//a-7),-1):  # aをi回用いる
    x = N-a*i
    for u in list(combinations_with_replacement(B,5)):
        if sum(u) == x:
            s = []
            for j in range(5):
                if u[j] != 0:
                    s.append(u[j])
            C.append((i,s))

D = []
for i,j in C:
    if i + len(j) == C[0][0] + len(C[0][1]):
        D.append((i,j))

m = min(D[i][0] for i in range(len(D)))

q,r,s = 0,0,0
for i,j in D:
    X = [d[a]]*(i-m) + [d[k] for k in j]
    X.sort(reverse=True)
    M = ''
    for k in X:
        M += str(k)
    M = int(M)
    if q < M:
        q,r,s = M,i,j

E = [d[a]]*r + [d[i] for i in s]
E.sort(reverse=True)

ans = ''
for i in E:
    ans += str(i)
print(ans) import sys sys N,M = map(int,sys.stdin.readline().rstrip().split()) N map(int,sys.stdin.readline().rstrip().split()) map int sys.stdin.readline().rstrip().split() sys.stdin.readline().rstrip().split sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip split M A = list(map(int,sys.stdin.readline().rstrip().split())) A list(map(int,sys.stdin.readline().rstrip().split())) list map(int,sys.stdin.readline().rstrip().split()) map int sys.stdin.readline().rstrip().split() sys.stdin.readline().rstrip().split sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip split from collections import defaultdict d = defaultdict(int) d defaultdict(int) defaultdict int for i in range(M):
    if A[i] == 1:
        d[2] = 1
    elif A[i] == 7:
        d[3] = 7
    elif A[i] == 4:
        d[4] = 4
    elif A[i] in [2,3,5]:
        d[5] = max(d[5],A[i])
    elif A[i] in [6,9]:
        d[6] = max(d[6],A[i])
    else:
        d[7] = 8 i range(M) range M if A[i] == 1:
        d[2] = 1
    elif A[i] == 7:
        d[3] = 7
    elif A[i] == 4:
        d[4] = 4
    elif A[i] in [2,3,5]:
        d[5] = max(d[5],A[i])
    elif A[i] in [6,9]:
        d[6] = max(d[6],A[i])
    else:
        d[7] = 8 A[i] == 1 A[i] A i 1 d[2] = 1 d[2] d 2 1 elif A[i] == 7:
        d[3] = 7
     A[i] == 7 A[i] A i 7 d[3] = 7 d[3] d 3 7 elif A[i] == 4:
        d[4] = 4
     A[i] == 4 A[i] A i 4 d[4] = 4 d[4] d 4 4 elif A[i] in [2,3,5]:
        d[5] = max(d[5],A[i])
     A[i] in [2,3,5] A[i] A i [2,3,5] 2 3 5 d[5] = max(d[5],A[i]) d[5] d 5 max(d[5],A[i]) max d[5] d 5 A[i] A i elif A[i] in [6,9]:
        d[6] = max(d[6],A[i])
     A[i] in [6,9] A[i] A i [6,9] 6 9 d[6] = max(d[6],A[i]) d[6] d 6 max(d[6],A[i]) max d[6] d 6 A[i] A i d[7] = 8 d[7] d 7 8 B = list(d.keys()) B list(d.keys()) list d.keys() d.keys d keys B.sort() B.sort B sort a = B[0] a B[0] B 0 B[0] = 0 B[0] B 0 0 if N % a == 0:
    print(''.join([str(d[a])]*(N//a)))
    exit() N % a == 0 N % a N a 0 print(''.join([str(d[a])]*(N//a))) print ''.join([str(d[a])]*(N//a)) ''.join '' join [str(d[a])]*(N//a) [str(d[a])] str(d[a]) str d[a] d a N//a N a exit() exit from itertools import combinations_with_replacement C = [] C [] for i in range(N//a,max(-1,N//a-7),-1):  # aをi回用いる
    x = N-a*i
    for u in list(combinations_with_replacement(B,5)):
        if sum(u) == x:
            s = []
            for j in range(5):
                if u[j] != 0:
                    s.append(u[j])
            C.append((i,s)) i range(N//a,max(-1,N//a-7),-1) range N//a N a max(-1,N//a-7) max -1 N//a-7 N//a N a 7 -1 x = N-a*i x N-a*i N a*i a i for u in list(combinations_with_replacement(B,5)):
        if sum(u) == x:
            s = []
            for j in range(5):
                if u[j] != 0:
                    s.append(u[j])
            C.append((i,s)) u list(combinations_with_replacement(B,5)) list combinations_with_replacement(B,5) combinations_with_replacement B 5 if sum(u) == x:
            s = []
            for j in range(5):
                if u[j] != 0:
                    s.append(u[j])
            C.append((i,s)) sum(u) == x sum(u) sum u x s = [] s [] for j in range(5):
                if u[j] != 0:
                    s.append(u[j])
             j range(5) range 5 if u[j] != 0:
                    s.append(u[j])
             u[j] != 0 u[j] u j 0 s.append(u[j]) s.append s append u[j] u j C.append((i,s)) C.append C append (i,s) i s D = [] D [] for i,j in C:
    if i + len(j) == C[0][0] + len(C[0][1]):
        D.append((i,j)) i j C if i + len(j) == C[0][0] + len(C[0][1]):
        D.append((i,j)) i + len(j) == C[0][0] + len(C[0][1]) i + len(j) i len(j) len j C[0][0] + len(C[0][1]) C[0][0] [0] C 0 0 len(C[0][1]) len C[0][1] [0] C 0 1 D.append((i,j)) D.append D append (i,j) i j m = min(D[i][0] for i in range(len(D))) m min(D[i][0] for i in range(len(D))) min D[i][0] [i] D i 0 q,r,s = 0,0,0 q 0 r 0 s 0 for i,j in D:
    X = [d[a]]*(i-m) + [d[k] for k in j]
    X.sort(reverse=True)
    M = ''
    for k in X:
        M += str(k)
    M = int(M)
    if q < M:
        q,r,s = M,i,j i j D d[k] for k in j for k in j k j for k in j d[k] d k X = [d[a]]*(i-m) + [d[k] for k in j] X [d[a]]*(i-m) + [d[k] for k in j] [d[a]]*(i-m) [d[a]] d[a] d a i-m i m [d[k] for k in j] X.sort(reverse=True) X.sort X sort reverse=True reverse True M = '' M '' for k in X:
        M += str(k)
     k X M += str(k) M str(k) str k M = int(M) M int(M) int M if q < M:
        q,r,s = M,i,j q < M q M q,r,s = M,i,j q M r i s j d[i] for i in s for i in s i s for i in s d[i] d i E = [d[a]]*r + [d[i] for i in s] E [d[a]]*r + [d[i] for i in s] [d[a]]*r [d[a]] d[a] d a r [d[i] for i in s] E.sort(reverse=True) E.sort E sort reverse=True reverse True ans = '' ans '' for i in E:
    ans += str(i) i E ans += str(i) ans str(i) str i print(ans) print ans d = defaultdict(int) defaultdict(int) d X = [d[a]]*(i-m) + [d[k] for k in j] [d[a]]*(i-m) + [d[k] for k in j] X s = [] [] s q,r,s = M,i,j M q s = M,i,j j s N,M = map(int,sys.stdin.readline().rstrip().split()) map(int,sys.stdin.readline().rstrip().split()) N ans += str(i) str(i) ans A = list(map(int,sys.stdin.readline().rstrip().split())) list(map(int,sys.stdin.readline().rstrip().split())) A m = min(D[i][0] for i in range(len(D))) min(D[i][0] for i in range(len(D))) m q,r,s = 0,0,0 0 q B = list(d.keys()) list(d.keys()) B r,s = 0,0,0 0 r M = int(M) int(M) M a = B[0] B[0] a M += str(k) str(k) M ans = '' '' ans x = N-a*i N-a*i x C = [] [] C M = '' '' M r,s = M,i,j i r s = 0,0,0 0 s M = map(int,sys.stdin.readline().rstrip().split()) map(int,sys.stdin.readline().rstrip().split()) M D = [] [] D E = [d[a]]*r + [d[i] for i in s] [d[a]]*r + [d[i] for i in s] E
import sys
N,M = map(int,sys.stdin.readline().rstrip().split())
A = list(map(int,sys.stdin.readline().rstrip().split()))

from collections import defaultdict

d = defaultdict(int)  # d[i] = (Aの中でi本用いて表せる最大の数)
for i in range(M):
    if A[i] == 1:
        d[2] = 1
    elif A[i] == 7:
        d[3] = 7
    elif A[i] == 4:
        d[4] = 4
    elif A[i] in [2,3,5]:
        d[5] = max(d[5],A[i])
    elif A[i] in [6,9]:
        d[6] = max(d[6],A[i])
    else:
        d[7] = 8

B = list(d.keys())
B.sort()

a = B[0]
B[0] = 0

if N % a == 0:
    print(''.join([str(d[a])]*(N//a)))
    exit()

from itertools import combinations_with_replacement

C = []

for i in range(N//a,max(-1,N//a-7),-1):  # aをi回用いる
    x = N-a*i
    for u in list(combinations_with_replacement(B,5)):
        if sum(u) == x:
            s = []
            for j in range(5):
                if u[j] != 0:
                    s.append(u[j])
            C.append((i,s))

D = []
for i,j in C:
    if i + len(j) == C[0][0] + len(C[0][1]):
        D.append((i,j))

m = min(D[i][0] for i in range(len(D)))

q,r,s = 0,0,0
for i,j in D:
    X = [d[a]]*(i-m) + [d[k] for k in j]
    X.sort(reverse=True)
    M = ''
    for k in X:
        M += str(k)
    M = int(M)
    if q < M:
        q,r,s = M,i,j

E = [d[a]]*r + [d[i] for i in s]
E.sort(reverse=True)

ans = ''
for i in E:
    ans += str(i)
print(ans)