import math
from collections import Counter
n, m = map(int, input().split())
a = list(map(int, input().split()))

if 9 in a and 6 in a:
    a.remove(6)
if 5 in a:
    if 2 in a:
        a.remove(2)
    if 3 in a:
        a.remove(3)
if 3 in a and 2 in a:
    a.remove(2)

req_m = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [None] * (n+1)
dp[0] = Counter([])

int_dp = [0] * (n+1)

for i in range(1, n+1):
    for j in a:
        if i >= req_m[j] and dp[i-req_m[j]] is not None:
            v = dp[i-req_m[j]] + Counter([j])
            if dp[i] is None:
                dp[i] = v
            else:
                if sum(dp[i].values()) < sum(v.values()):
                    dp[i] = v
                elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v

print(''.join(map(str, sorted(dp[-1].elements(), reverse=True)))) import math math from collections import Counter 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 if 9 in a and 6 in a:
    a.remove(6) 9 in a and 6 in a 9 in a 9 a 6 in a 6 a a.remove(6) a.remove a remove 6 if 5 in a:
    if 2 in a:
        a.remove(2)
    if 3 in a:
        a.remove(3) 5 in a 5 a if 2 in a:
        a.remove(2)
     2 in a 2 a a.remove(2) a.remove a remove 2 if 3 in a:
        a.remove(3) 3 in a 3 a a.remove(3) a.remove a remove 3 if 3 in a and 2 in a:
    a.remove(2) 3 in a and 2 in a 3 in a 3 a 2 in a 2 a a.remove(2) a.remove a remove 2 req_m = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] req_m [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] -1 2 5 5 4 5 6 3 7 6 dp = [None] * (n+1) dp [None] * (n+1) [None] None n+1 n 1 dp[0] = Counter([]) dp[0] dp 0 Counter([]) Counter [] int_dp = [0] * (n+1) int_dp [0] * (n+1) [0] 0 n+1 n 1 for i in range(1, n+1):
    for j in a:
        if i >= req_m[j] and dp[i-req_m[j]] is not None:
            v = dp[i-req_m[j]] + Counter([j])
            if dp[i] is None:
                dp[i] = v
            else:
                if sum(dp[i].values()) < sum(v.values()):
                    dp[i] = v
                elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v i range(1, n+1) range 1 n+1 n 1 for j in a:
        if i >= req_m[j] and dp[i-req_m[j]] is not None:
            v = dp[i-req_m[j]] + Counter([j])
            if dp[i] is None:
                dp[i] = v
            else:
                if sum(dp[i].values()) < sum(v.values()):
                    dp[i] = v
                elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v j a if i >= req_m[j] and dp[i-req_m[j]] is not None:
            v = dp[i-req_m[j]] + Counter([j])
            if dp[i] is None:
                dp[i] = v
            else:
                if sum(dp[i].values()) < sum(v.values()):
                    dp[i] = v
                elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v i >= req_m[j] and dp[i-req_m[j]] is not None i >= req_m[j] i req_m[j] req_m j dp[i-req_m[j]] is not None dp[i-req_m[j]] dp i-req_m[j] i req_m[j] req_m j None v = dp[i-req_m[j]] + Counter([j]) v dp[i-req_m[j]] + Counter([j]) dp[i-req_m[j]] dp i-req_m[j] i req_m[j] req_m j Counter([j]) Counter [j] j if dp[i] is None:
                dp[i] = v
            else:
                if sum(dp[i].values()) < sum(v.values()):
                    dp[i] = v
                elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v dp[i] is None dp[i] dp i None dp[i] = v dp[i] dp i v if sum(dp[i].values()) < sum(v.values()):
                    dp[i] = v
                elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v sum(dp[i].values()) < sum(v.values()) sum(dp[i].values()) sum dp[i].values() [i].values [i] dp i values sum(v.values()) sum v.values() v.values v values dp[i] = v dp[i] dp i v elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v sum(dp[i].values()) == sum(v.values()) sum(dp[i].values()) sum dp[i].values() [i].values [i] dp i values sum(v.values()) sum v.values() v.values v values if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True) sorted(dp[i].elements(), reverse=True) sorted dp[i].elements() [i].elements [i] dp i elements reverse=True reverse True sorted(v.elements(), reverse=True) sorted v.elements() v.elements v elements reverse=True reverse True dp[i] = v dp[i] dp i v print(''.join(map(str, sorted(dp[-1].elements(), reverse=True)))) print ''.join(map(str, sorted(dp[-1].elements(), reverse=True))) ''.join '' join map(str, sorted(dp[-1].elements(), reverse=True)) map str sorted(dp[-1].elements(), reverse=True) sorted dp[-1].elements() [-1].elements [-1] dp -1 elements reverse=True reverse True n, m = map(int, input().split()) map(int, input().split()) n int_dp = [0] * (n+1) [0] * (n+1) int_dp m = map(int, input().split()) map(int, input().split()) m a = list(map(int, input().split())) list(map(int, input().split())) a req_m = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] req_m v = dp[i-req_m[j]] + Counter([j]) dp[i-req_m[j]] + Counter([j]) v dp = [None] * (n+1) [None] * (n+1) dp
import math
from collections import Counter
n, m = map(int, input().split())
a = list(map(int, input().split()))

if 9 in a and 6 in a:
    a.remove(6)
if 5 in a:
    if 2 in a:
        a.remove(2)
    if 3 in a:
        a.remove(3)
if 3 in a and 2 in a:
    a.remove(2)

req_m = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]

dp = [None] * (n+1)
dp[0] = Counter([])

int_dp = [0] * (n+1)

for i in range(1, n+1):
    for j in a:
        if i >= req_m[j] and dp[i-req_m[j]] is not None:
            v = dp[i-req_m[j]] + Counter([j])
            if dp[i] is None:
                dp[i] = v
            else:
                if sum(dp[i].values()) < sum(v.values()):
                    dp[i] = v
                elif sum(dp[i].values()) == sum(v.values()):
                    if sorted(dp[i].elements(), reverse=True) < sorted(v.elements(), reverse=True):
                        dp[i] = v

print(''.join(map(str, sorted(dp[-1].elements(), reverse=True))))
