#123456789
#255456376
n, m = map(int, input().split())
a = list(map(int , input().split()))
a.sort()
t = [2,5,5,4,5,6,3,7,6]
cost = []
for i in range(m):
    cost.append(t[a[i]-1])
#print(a)
#print(cost)
min_c = min(cost)
digit = n // min_c # 最大桁数
s = n % min_c # 最大桁まで作った余りの本数

max_ai = 0
import itertools
for i in itertools.product(list(range(m+1)), repeat=min(digit, 5)):
    #print(i)
    t = list(i)
    ci = sum(list(map(lambda j: cost[j-1] if j != 0 else 0, t)))
    ai = ''.join(map(lambda j: str(a[j-1]) if j != 0 else '', t))
    if digit <= 5:
        if ci == n:
            max_ai = max(max_ai, int(ai))
    else:
        if ci == (n - min_c * (digit - 5)):
            max_ai = max(max_ai, int(ai))
    #print(ci, max_ai)
min_chr = ''
for i in reversed(range(m)):
    if min_c == cost[i]:
        min_chr = a[i]
        break

if digit <= 5:
    print(max_ai)
else:
    ans = list(str(max_ai)) + ([str(min_chr)] * (digit - 5))
    ans.sort(reverse=True)
    print(''.join(ans))


"""
chg = []
while s > 0:
    #print(s, chg)
    for i in reversed(range(m)):
        if min_c + s == cost[i]:
            chg.append(a[i])
            s = 0
            break
    for i in reversed(range(m)):
        if min_c < cost[i] and cost[i] < min_c + s:
            chg.append(a[i])
            s = min_c + s - cost[i]
            break

#print(chg)
min_chr = ''
for i in reversed(range(m)):
    if min_c == cost[i]:
        min_chr = a[i]
        break

ans = chg + ([min_chr] * (d - len(chg)))
ans.sort(reverse=True)
print(''.join(map(str, ans)))
""" 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 a.sort() a.sort a sort t = [2,5,5,4,5,6,3,7,6] t [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 cost = [] cost [] for i in range(m):
    cost.append(t[a[i]-1])
#print(a)
#print(cost) i range(m) range m cost.append(t[a[i]-1]) cost.append cost append t[a[i]-1] t a[i]-1 a[i] a i 1 min_c = min(cost) min_c min(cost) min cost digit = n // min_c digit n // min_c n min_c s = n % min_c s n % min_c n min_c max_ai = 0 max_ai 0 import itertools itertools for i in itertools.product(list(range(m+1)), repeat=min(digit, 5)):
    #print(i)
    t = list(i)
    ci = sum(list(map(lambda j: cost[j-1] if j != 0 else 0, t)))
    ai = ''.join(map(lambda j: str(a[j-1]) if j != 0 else '', t))
    if digit <= 5:
        if ci == n:
            max_ai = max(max_ai, int(ai))
    else:
        if ci == (n - min_c * (digit - 5)):
            max_ai = max(max_ai, int(ai))
    #print(ci, max_ai) i itertools.product(list(range(m+1)), repeat=min(digit, 5)) itertools.product itertools product list(range(m+1)) list range(m+1) range m+1 m 1 repeat=min(digit, 5) repeat min(digit, 5) min digit 5 t = list(i) t list(i) list i ci = sum(list(map(lambda j: cost[j-1] if j != 0 else 0, t))) ci sum(list(map(lambda j: cost[j-1] if j != 0 else 0, t))) sum list(map(lambda j: cost[j-1] if j != 0 else 0, t)) list map(lambda j: cost[j-1] if j != 0 else 0, t) map lambda j: cost[j-1] if j != 0 else 0 cost[j-1] if j != 0 else 0 j != 0 j 0 cost[j-1] cost j-1 j 1 0 j t ai = ''.join(map(lambda j: str(a[j-1]) if j != 0 else '', t)) ai ''.join(map(lambda j: str(a[j-1]) if j != 0 else '', t)) ''.join '' join map(lambda j: str(a[j-1]) if j != 0 else '', t) map lambda j: str(a[j-1]) if j != 0 else '' str(a[j-1]) if j != 0 else '' j != 0 j 0 str(a[j-1]) str a[j-1] a j-1 j 1 '' j t if digit <= 5:
        if ci == n:
            max_ai = max(max_ai, int(ai))
    else:
        if ci == (n - min_c * (digit - 5)):
            max_ai = max(max_ai, int(ai))
    #print(ci, max_ai) digit <= 5 digit 5 if ci == n:
            max_ai = max(max_ai, int(ai))
     ci == n ci n max_ai = max(max_ai, int(ai)) max_ai max(max_ai, int(ai)) max max_ai int(ai) int ai if ci == (n - min_c * (digit - 5)):
            max_ai = max(max_ai, int(ai))
    #print(ci, max_ai) ci == (n - min_c * (digit - 5)) ci n - min_c * (digit - 5) n min_c * (digit - 5) min_c digit - 5 digit 5 max_ai = max(max_ai, int(ai)) max_ai max(max_ai, int(ai)) max max_ai int(ai) int ai min_chr = '' min_chr '' for i in reversed(range(m)):
    if min_c == cost[i]:
        min_chr = a[i]
        break i reversed(range(m)) reversed range(m) range m if min_c == cost[i]:
        min_chr = a[i]
        break min_c == cost[i] min_c cost[i] cost i min_chr = a[i] min_chr a[i] a i break if digit <= 5:
    print(max_ai)
else:
    ans = list(str(max_ai)) + ([str(min_chr)] * (digit - 5))
    ans.sort(reverse=True)
    print(''.join(ans)) digit <= 5 digit 5 print(max_ai) print max_ai ans = list(str(max_ai)) + ([str(min_chr)] * (digit - 5)) ans list(str(max_ai)) + ([str(min_chr)] * (digit - 5)) list(str(max_ai)) list str(max_ai) str max_ai [str(min_chr)] * (digit - 5) [str(min_chr)] str(min_chr) str min_chr digit - 5 digit 5 ans.sort(reverse=True) ans.sort ans sort reverse=True reverse True print(''.join(ans)) print ''.join(ans) ''.join '' join ans """
chg = []
while s > 0:
    #print(s, chg)
    for i in reversed(range(m)):
        if min_c + s == cost[i]:
            chg.append(a[i])
            s = 0
            break
    for i in reversed(range(m)):
        if min_c < cost[i] and cost[i] < min_c + s:
            chg.append(a[i])
            s = min_c + s - cost[i]
            break

#print(chg)
min_chr = ''
for i in reversed(range(m)):
    if min_c == cost[i]:
        min_chr = a[i]
        break

ans = chg + ([min_chr] * (d - len(chg)))
ans.sort(reverse=True)
print(''.join(map(str, ans)))
""" t = [2,5,5,4,5,6,3,7,6] [2,5,5,4,5,6,3,7,6] t min_c = min(cost) min(cost) min_c min_chr = a[i] a[i] min_chr digit = n // min_c n // min_c digit max_ai = max(max_ai, int(ai)) max(max_ai, int(ai)) max_ai ans = list(str(max_ai)) + ([str(min_chr)] * (digit - 5)) list(str(max_ai)) + ([str(min_chr)] * (digit - 5)) ans a = list(map(int , input().split())) list(map(int , input().split())) a min_chr = '' '' min_chr m = map(int, input().split()) map(int, input().split()) m t = list(i) list(i) t s = n % min_c n % min_c s max_ai = 0 0 max_ai ci = sum(list(map(lambda j: cost[j-1] if j != 0 else 0, t))) sum(list(map(lambda j: cost[j-1] if j != 0 else 0, t))) ci ai = ''.join(map(lambda j: str(a[j-1]) if j != 0 else '', t)) ''.join(map(lambda j: str(a[j-1]) if j != 0 else '', t)) ai max_ai = max(max_ai, int(ai)) max(max_ai, int(ai)) max_ai n, m = map(int, input().split()) map(int, input().split()) n cost = [] [] cost
#123456789
#255456376
n, m = map(int, input().split())
a = list(map(int , input().split()))
a.sort()
t = [2,5,5,4,5,6,3,7,6]
cost = []
for i in range(m):
    cost.append(t[a[i]-1])
#print(a)
#print(cost)
min_c = min(cost)
digit = n // min_c # 最大桁数
s = n % min_c # 最大桁まで作った余りの本数

max_ai = 0
import itertools
for i in itertools.product(list(range(m+1)), repeat=min(digit, 5)):
    #print(i)
    t = list(i)
    ci = sum(list(map(lambda j: cost[j-1] if j != 0 else 0, t)))
    ai = ''.join(map(lambda j: str(a[j-1]) if j != 0 else '', t))
    if digit <= 5:
        if ci == n:
            max_ai = max(max_ai, int(ai))
    else:
        if ci == (n - min_c * (digit - 5)):
            max_ai = max(max_ai, int(ai))
    #print(ci, max_ai)
min_chr = ''
for i in reversed(range(m)):
    if min_c == cost[i]:
        min_chr = a[i]
        break

if digit <= 5:
    print(max_ai)
else:
    ans = list(str(max_ai)) + ([str(min_chr)] * (digit - 5))
    ans.sort(reverse=True)
    print(''.join(ans))


"""
chg = []
while s > 0:
    #print(s, chg)
    for i in reversed(range(m)):
        if min_c + s == cost[i]:
            chg.append(a[i])
            s = 0
            break
    for i in reversed(range(m)):
        if min_c < cost[i] and cost[i] < min_c + s:
            chg.append(a[i])
            s = min_c + s - cost[i]
            break

#print(chg)
min_chr = ''
for i in reversed(range(m)):
    if min_c == cost[i]:
        min_chr = a[i]
        break

ans = chg + ([min_chr] * (d - len(chg)))
ans.sort(reverse=True)
print(''.join(map(str, ans)))
"""