#!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, deque

from heapq import heappush, heappop

from itertools import permutations, accumulate

import sys

import math

import bisect

def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]

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

def LS():return [list(x) for x in sys.stdin.readline().split()]

def S():

    res = list(sys.stdin.readline())

    if res[-1] == "\n":

        return res[:-1]

    return res

def IR(n):

    return [I() 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)]

def LSR(n):

    return [LS() for i in range(n)]



sys.setrecursionlimit(1000000)

mod = 1000000007



def solve():

    W,n = LI()

    a = LI()

    for i in range(n):

        a[i] -= 1

    a.sort(reverse = True)

    b = [2,5,5,4,5,6,3,7,6]

    dp = [-float("inf")]*(W+1)

    dp[0] = 0

    for i in range(n):

        v = a[i]+1

        w = b[a[i]]

        for j in range(W):

            nj = j+w

            if nj > W:

                continue

            nd = dp[j]*10+v

            if dp[nj] < nd:

                dp[nj] = nd

    print((dp[W]))

    return



#Solve

if __name__ == "__main__":

    solve()

#!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, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect


def LI():
    return [int(x) for x in sys.stdin.buffer.readline().split()]


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


def LS():
    return [list(x) for x in sys.stdin.readline().split()]


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() 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)]


def LSR(n):
    return [LS() for i in range(n)]


sys.setrecursionlimit(1000000)
mod = 1000000007


def solve():
    W, n = LI()
    a = LI()
    for i in range(n):
        a[i] -= 1
    a.sort(reverse=True)
    b = [2, 5, 5, 4, 5, 6, 3, 7, 6]
    dp = [-float("inf")] * (W + 1)
    dp[0] = 0
    for i in range(n):
        v = a[i] + 1
        w = b[a[i]]
        for j in range(W):
            nj = j + w
            if nj > W:
                continue
            nd = dp[j] * 10 + v
            if dp[nj] < nd:
                dp[nj] = nd
    print((dp[W]))
    return


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

-from collections import defaultdict -from collections import deque +from collections import defaultdict, deque +from itertools import permutations, accumulate -import random -    return list(map(int, sys.stdin.readline().split())) +    return [int(x) for x in sys.stdin.buffer.readline().split()] -    return int(sys.stdin.readline()) +    return int(sys.stdin.buffer.readline()) -    return list(map(list, sys.stdin.readline().split())) +    return [list(x) for x in sys.stdin.readline().split()] -    return list(sys.stdin.readline())[:-1] +    res = list(sys.stdin.readline()) +    if res[-1] == "\n": +        return res[:-1] +    return res -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = I() -    return l +    return [I() for i in range(n)] -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = LI() -    return l +    return [LI() for i in range(n)] -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = S() -    return l +    return [S() for i in range(n)] -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = SR() -    return l +    return [LS() for i in range(n)] -# 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) +def solve(): +    W, n = LI() +    a = LI() -        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(): +        a[i] -= 1 +    a.sort(reverse=True) +    b = [2, 5, 5, 4, 5, 6, 3, 7, 6] +    dp = [-float("inf")] * (W + 1) +    dp[0] = 0 +    for i in range(n): +        v = a[i] + 1 +        w = b[a[i]] +        for j in range(W): +            nj = j + w +            if nj > W: +                continue +            nd = dp[j] * 10 + v +            if dp[nj] < nd: +                dp[nj] = nd +    print((dp[W])) -    D() +    solve()