#!usr/bin/env python3

from collections import defaultdict

import math

import bisect

import random

def LI(): return list(map(int, input().split()))

def II(): return int(eval(input()))

def LS(): return input().split()

def S(): return list(eval(input()))

def IIR(n): return [II() for i in range(n)]

def LIR(n): return [LI() for i in range(n)]

def SR(n): return [S() for i in range(n)]

mod = 1000000007



#A

"""

a,b = LI()

if b % a == 0:

    print(a+b)

else:

    print(b-a)

"""



#B

"""

n,m = LI()

lis = [0 for i in range(m)]

for i in range(n):

    k = LI()

    for j in k[1:]:

        lis[j-1] += 1

ans = 0

for i in lis:

    if i == n:ans += 1

print(ans)

"""



#C

"""

def gcd(a,b):

    if a == 0:

        return b

    return gcd(b%a,a)

n = II()

a = LI()

ans = a[0]

for i in range(1,n):

    ans = gcd(ans,a[i])

print(ans)

"""



#D

n,m = LI()

a = LI()

f = [0,2,5,5,4,5,6,3,7,6]

for i in range(m):

    a[i] = [a[i],f[a[i]]]

a.sort(key = lambda x:x[1])

dp = [[] for i in range(n+10)]

for i in a:

    dp[i[1]] = [i[0]]

for i in range(n+1):

    for j in a:

        if i-j[1] >= 0 and len(dp[i-j[1]]) > 0:

            if len(dp[i]) < len(dp[i-j[1]])+1:

                dp[i] = dp[i-j[1]]+[j[0]]

                dp[i].sort()

                dp[i] = dp[i][::-1]

            else:

                if len(dp[i]) == len(dp[i-j[1]])+1:

                    d = sorted(dp[i-j[1]]+[j[0]])

                    d = d[::-1]

                    k = [dp[i],d]

                    k.sort()

                    if k[1] == d:

                        dp[i] = d

ans = 0

k = 1

l = len(dp[n])

for i in range(l):

    ans += k*dp[n][l-i-1]

    k *= 10

print(ans)



#E



#F



#G



#H



#I



#J



#K



#L



#M



#N



#O



#P



#Q



#R



#S



#T

#!usr/bin/env python3

from collections import defaultdict

from collections import deque

from heapq import heappush, heappop

import sys

import math

import bisect

import random

def LI(): return list(map(int, sys.stdin.readline().split()))

def I(): return int(sys.stdin.readline())

def LS():return list(map(list, sys.stdin.readline().split()))

def S(): return list(sys.stdin.readline())[:-1]

def IR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = I()

    return l

def LIR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LI()

    return l

def SR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = S()

    return l

def LSR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = SR()

    return l

sys.setrecursionlimit(1000000)

mod = 1000000007



#A

def A():

    a,b = LI()

    if b%a == 0:

        print((a+b))

    else:

        print((b-a))

    return



#B

def B():

    n,m = LI()

    d = defaultdict(int)

    for i in range(n):

        l = LI()

        for j in range(1,len(l)):

            d[l[j]] += 1

    ans = 0

    for i in list(d.values()):

        if i == n:

            ans += 1

    print(ans)

    return



#C

def C():

    def gcd(a,b):

        if a == 0:

            return b

        return gcd(b%a,a)

    n = I()

    a = LI()

    g = a[0]

    for i in range(1,n):

        g = gcd(g,a[i])

    print(g)

    return



#D

def D():

    n,m = LI()

    a = LI()

    f = [0,2,5,5,4,5,6,3,7,6]

    dp = [None for i in range(n+1)]

    dp[0] = []

    for j in a:

        for i in range(n+1):

            if i-f[j] >= 0 and dp[i-f[j]] != None:

                if dp[i] == None:

                    dp[i] = dp[i-f[j]]+[j]

                    dp[i].sort(reverse = True)

                elif len(dp[i]) < len(dp[i-f[j]])+1:

                    dp[i] = dp[i-f[j]]+[j]

                    dp[i].sort(reverse = True)

                elif len(dp[i]) == len(dp[i-f[j]])+1:

                    d = dp[i-f[j]]+[j]

                    d.sort(reverse = True)

                    if dp[i] < d:

                        dp[i] = d

    ans = 0

    k = pow(10,len(dp[n])-1)

    for i in range(len(dp[n])):

        ans += k*dp[n][i]

        k //= 10

    print(ans)

    return



#E

def E():

    return



#F

def F():

    return



#G

def G():

    return



#H

def H():

    return



#Solve

if __name__ == "__main__":

    D()

#!usr/bin/env python3
from collections import defaultdict
import math
import bisect
import random


def LI():
    return list(map(int, input().split()))


def II():
    return int(eval(input()))


def LS():
    return input().split()


def S():
    return list(eval(input()))


def IIR(n):
    return [II() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


mod = 1000000007
# A
"""
a,b = LI()
if b % a == 0:
    print(a+b)
else:
    print(b-a)
"""
# B
"""
n,m = LI()
lis = [0 for i in range(m)]
for i in range(n):
    k = LI()
    for j in k[1:]:
        lis[j-1] += 1
ans = 0
for i in lis:
    if i == n:ans += 1
print(ans)
"""
# C
"""
def gcd(a,b):
    if a == 0:
        return b
    return gcd(b%a,a)
n = II()
a = LI()
ans = a[0]
for i in range(1,n):
    ans = gcd(ans,a[i])
print(ans)
"""
# D
n, m = LI()
a = LI()
f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
for i in range(m):
    a[i] = [a[i], f[a[i]]]
a.sort(key=lambda x: x[1])
dp = [[] for i in range(n + 10)]
for i in a:
    dp[i[1]] = [i[0]]
for i in range(n + 1):
    for j in a:
        if i - j[1] >= 0 and len(dp[i - j[1]]) > 0:
            if len(dp[i]) < len(dp[i - j[1]]) + 1:
                dp[i] = dp[i - j[1]] + [j[0]]
                dp[i].sort()
                dp[i] = dp[i][::-1]
            else:
                if len(dp[i]) == len(dp[i - j[1]]) + 1:
                    d = sorted(dp[i - j[1]] + [j[0]])
                    d = d[::-1]
                    k = [dp[i], d]
                    k.sort()
                    if k[1] == d:
                        dp[i] = d
ans = 0
k = 1
l = len(dp[n])
for i in range(l):
    ans += k * dp[n][l - i - 1]
    k *= 10
print(ans)
# E
# F
# G
# H
# I
# J
# K
# L
# M
# N
# O
# P
# Q
# R
# S
# T

#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random


def LI():
    return list(map(int, sys.stdin.readline().split()))


def I():
    return int(sys.stdin.readline())


def LS():
    return list(map(list, sys.stdin.readline().split()))


def S():
    return list(sys.stdin.readline())[:-1]


def IR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = I()
    return l


def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = LI()
    return l


def SR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = S()
    return l


def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = SR()
    return l


sys.setrecursionlimit(1000000)
mod = 1000000007
# A
def A():
    a, b = LI()
    if b % a == 0:
        print((a + b))
    else:
        print((b - a))
    return


# B
def B():
    n, m = LI()
    d = defaultdict(int)
    for i in range(n):
        l = LI()
        for j in range(1, len(l)):
            d[l[j]] += 1
    ans = 0
    for i in list(d.values()):
        if i == n:
            ans += 1
    print(ans)
    return


# C
def C():
    def gcd(a, b):
        if a == 0:
            return b
        return gcd(b % a, a)

    n = I()
    a = LI()
    g = a[0]
    for i in range(1, n):
        g = gcd(g, a[i])
    print(g)
    return


# D
def D():
    n, m = LI()
    a = LI()
    f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    dp = [None for i in range(n + 1)]
    dp[0] = []
    for j in a:
        for i in range(n + 1):
            if i - f[j] >= 0 and dp[i - f[j]] != None:
                if dp[i] == None:
                    dp[i] = dp[i - f[j]] + [j]
                    dp[i].sort(reverse=True)
                elif len(dp[i]) < len(dp[i - f[j]]) + 1:
                    dp[i] = dp[i - f[j]] + [j]
                    dp[i].sort(reverse=True)
                elif len(dp[i]) == len(dp[i - f[j]]) + 1:
                    d = dp[i - f[j]] + [j]
                    d.sort(reverse=True)
                    if dp[i] < d:
                        dp[i] = d
    ans = 0
    k = pow(10, len(dp[n]) - 1)
    for i in range(len(dp[n])):
        ans += k * dp[n][i]
        k //= 10
    print(ans)
    return


# E
def E():
    return


# F
def F():
    return


# G
def G():
    return


# H
def H():
    return


# Solve
if __name__ == "__main__":
    D()

+from collections import deque +from heapq import heappush, heappop +import sys -    return list(map(int, input().split())) +    return list(map(int, sys.stdin.readline().split())) -def II(): -    return int(eval(input())) +def I(): +    return int(sys.stdin.readline()) -    return input().split() +    return list(map(list, sys.stdin.readline().split())) -    return list(eval(input())) +    return list(sys.stdin.readline())[:-1] -def IIR(n): -    return [II() for i in range(n)] +def IR(n): +    l = [None for i in range(n)] +    for i in range(n): +        l[i] = I() +    return l -    return [LI() for i in range(n)] +    l = [None for i in range(n)] +    for i in range(n): +        l[i] = LI() +    return l -    return [S() for i in range(n)] +    l = [None for i in range(n)] +    for i in range(n): +        l[i] = S() +    return l +def LSR(n): +    l = [None for i in range(n)] +    for i in range(n): +        l[i] = SR() +    return l + + +sys.setrecursionlimit(1000000) -""" -a,b = LI() -if b % a == 0: -    print(a+b) -else: -    print(b-a) -""" +def A(): +    a, b = LI() +    if b % a == 0: +        print((a + b)) +    else: +        print((b - a)) +    return + + -""" -n,m = LI() -lis = [0 for i in range(m)] -for i in range(n): -    k = LI() -    for j in k[1:]: -        lis[j-1] += 1 -ans = 0 -for i in lis: -    if i == n:ans += 1 -print(ans) -""" +def B(): +    n, m = LI() +    d = defaultdict(int) +    for i in range(n): +        l = LI() +        for j in range(1, len(l)): +            d[l[j]] += 1 +    ans = 0 +    for i in list(d.values()): +        if i == n: +            ans += 1 +    print(ans) +    return + + -""" -def gcd(a,b): -    if a == 0: -        return b -    return gcd(b%a,a) -n = II() -a = LI() -ans = a[0] -for i in range(1,n): -    ans = gcd(ans,a[i]) -print(ans) -""" +def C(): +    def gcd(a, b): +        if a == 0: +            return b +        return gcd(b % a, a) + +    n = I() +    a = LI() +    g = a[0] +    for i in range(1, n): +        g = gcd(g, a[i]) +    print(g) +    return + + -n, m = LI() -a = LI() -f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] -for i in range(m): -    a[i] = [a[i], f[a[i]]] -a.sort(key=lambda x: x[1]) -dp = [[] for i in range(n + 10)] -for i in a: -    dp[i[1]] = [i[0]] -for i in range(n + 1): +def D(): +    n, m = LI() +    a = LI() +    f = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] +    dp = [None for i in range(n + 1)] +    dp[0] = [] -        if i - j[1] >= 0 and len(dp[i - j[1]]) > 0: -            if len(dp[i]) < len(dp[i - j[1]]) + 1: -                dp[i] = dp[i - j[1]] + [j[0]] -                dp[i].sort() -                dp[i] = dp[i][::-1] -            else: -                if len(dp[i]) == len(dp[i - j[1]]) + 1: -                    d = sorted(dp[i - j[1]] + [j[0]]) -                    d = d[::-1] -                    k = [dp[i], d] -                    k.sort() -                    if k[1] == d: +        for i in range(n + 1): +            if i - f[j] >= 0 and dp[i - f[j]] != None: +                if dp[i] == None: +                    dp[i] = dp[i - f[j]] + [j] +                    dp[i].sort(reverse=True) +                elif len(dp[i]) < len(dp[i - f[j]]) + 1: +                    dp[i] = dp[i - f[j]] + [j] +                    dp[i].sort(reverse=True) +                elif len(dp[i]) == len(dp[i - f[j]]) + 1: +                    d = dp[i - f[j]] + [j] +                    d.sort(reverse=True) +                    if dp[i] < d: -ans = 0 -k = 1 -l = len(dp[n]) -for i in range(l): -    ans += k * dp[n][l - i - 1] -    k *= 10 -print(ans) +    ans = 0 +    k = pow(10, len(dp[n]) - 1) +    for i in range(len(dp[n])): +        ans += k * dp[n][i] +        k //= 10 +    print(ans) +    return + + +def E(): +    return + + +def F(): +    return + + +def G(): +    return + + -# I -# J -# K -# L -# M -# N -# O -# P -# Q -# R -# S -# T +def H(): +    return + + +# Solve +if __name__ == "__main__": +    D()