from collections import defaultdict

memo = []


def find(nowstr, rest):
    if rest < 0:
        return ""

    for i in costlist:
        if i < rest:
            find(nowstr+str(i), rest - i)
        elif i == rest:

            memo.append(nowstr+str(i))


def cost_to_num(c):
    if c == 2:
        return "1"
    if c == 3:
        return "7"
    if c == 4:
        return "4"

    if c == 5:
        if 2 in num_to_use:
            return "2"
        elif 3 in num_to_use:
            return "3"
        else:
            return "5"
    if c == 6:
        if 6 in num_to_use:
            return "6"
        else:
            return "9"
    if c == 7:
        return "8"


N, M = map(int, input().split())
Alist = list(map(int, input().split()))

num_to_use = set(Alist)
"""
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 を 1 つ作るには、
2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 本のマッチ棒を使う。
"""
ref = [2, 5, 5, 4, 5, 6, 3, 7, 6]
if 9 in num_to_use:
    num_to_use.discard(6)
if 5 in num_to_use:
    num_to_use.discard(2)
    num_to_use.discard(3)
if 3 in num_to_use:
    num_to_use.discard(2)


costlist = [ref[i-1] for i in num_to_use]
costlist.sort()
# print(num_to_use)
# print(costlist)
# print(costlist[0])
minlength = N // costlist[0] - 5
if minlength < 0:
    minlength = 0
ans_tail = [cost_to_num(costlist[0])]*minlength

delta = N - minlength * costlist[0]
# print(delta)
find("", delta)

# print(memo)

ans_length = len(memo[0])
temp = 0
for i in memo:
    if len(i) < ans_length:
        continue

    numlist = [cost_to_num(int(j)) for j in i]+ans_tail
    # print(numlist)
    numlist.sort(reverse=True)
    joined = "".join(numlist)
    # print(joined)
    if temp < int(joined):
        temp = int(joined)
ans_head = str(temp)

# print("---")
print(ans_head) from collections import defaultdict memo = [] memo [] def find(nowstr, rest):
    if rest < 0:
        return ""

    for i in costlist:
        if i < rest:
            find(nowstr+str(i), rest - i)
        elif i == rest:

            memo.append(nowstr+str(i)) find if rest < 0:
        return ""

     rest < 0 rest 0 return "" "" for i in costlist:
        if i < rest:
            find(nowstr+str(i), rest - i)
        elif i == rest:

            memo.append(nowstr+str(i)) i costlist if i < rest:
            find(nowstr+str(i), rest - i)
        elif i == rest:

            memo.append(nowstr+str(i)) i < rest i rest find(nowstr+str(i), rest - i) find nowstr+str(i) nowstr str(i) str i rest - i rest i elif i == rest:

            memo.append(nowstr+str(i)) i == rest i rest memo.append(nowstr+str(i)) memo.append memo append nowstr+str(i) nowstr str(i) str i nowstr nowstr rest rest def cost_to_num(c):
    if c == 2:
        return "1"
    if c == 3:
        return "7"
    if c == 4:
        return "4"

    if c == 5:
        if 2 in num_to_use:
            return "2"
        elif 3 in num_to_use:
            return "3"
        else:
            return "5"
    if c == 6:
        if 6 in num_to_use:
            return "6"
        else:
            return "9"
    if c == 7:
        return "8" cost_to_num if c == 2:
        return "1"
     c == 2 c 2 return "1" "1" if c == 3:
        return "7"
     c == 3 c 3 return "7" "7" if c == 4:
        return "4"

     c == 4 c 4 return "4" "4" if c == 5:
        if 2 in num_to_use:
            return "2"
        elif 3 in num_to_use:
            return "3"
        else:
            return "5"
     c == 5 c 5 if 2 in num_to_use:
            return "2"
        elif 3 in num_to_use:
            return "3"
        else:
            return "5"
     2 in num_to_use 2 num_to_use return "2" "2" elif 3 in num_to_use:
            return "3"
         3 in num_to_use 3 num_to_use return "3" "3" return "5" "5" if c == 6:
        if 6 in num_to_use:
            return "6"
        else:
            return "9"
     c == 6 c 6 if 6 in num_to_use:
            return "6"
        else:
            return "9"
     6 in num_to_use 6 num_to_use return "6" "6" return "9" "9" if c == 7:
        return "8" c == 7 c 7 return "8" "8" c c N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M Alist = list(map(int, input().split())) Alist list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split num_to_use = set(Alist) num_to_use set(Alist) set Alist """
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 を 1 つ作るには、
2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 本のマッチ棒を使う。
""" ref = [2, 5, 5, 4, 5, 6, 3, 7, 6] ref [2, 5, 5, 4, 5, 6, 3, 7, 6] 2 5 5 4 5 6 3 7 6 if 9 in num_to_use:
    num_to_use.discard(6) 9 in num_to_use 9 num_to_use num_to_use.discard(6) num_to_use.discard num_to_use discard 6 if 5 in num_to_use:
    num_to_use.discard(2)
    num_to_use.discard(3) 5 in num_to_use 5 num_to_use num_to_use.discard(2) num_to_use.discard num_to_use discard 2 num_to_use.discard(3) num_to_use.discard num_to_use discard 3 if 3 in num_to_use:
    num_to_use.discard(2) 3 in num_to_use 3 num_to_use num_to_use.discard(2) num_to_use.discard num_to_use discard 2 ref[i-1] for i in num_to_use for i in num_to_use i num_to_use for i in num_to_use ref[i-1] ref i-1 i 1 costlist = [ref[i-1] for i in num_to_use] costlist [ref[i-1] for i in num_to_use] costlist.sort() costlist.sort costlist sort minlength = N // costlist[0] - 5 minlength N // costlist[0] - 5 N // costlist[0] N costlist[0] costlist 0 5 if minlength < 0:
    minlength = 0 minlength < 0 minlength 0 minlength = 0 minlength 0 ans_tail = [cost_to_num(costlist[0])]*minlength ans_tail [cost_to_num(costlist[0])]*minlength [cost_to_num(costlist[0])] cost_to_num(costlist[0]) cost_to_num costlist[0] costlist 0 minlength delta = N - minlength * costlist[0] delta N - minlength * costlist[0] N minlength * costlist[0] minlength costlist[0] costlist 0 find("", delta) find "" delta ans_length = len(memo[0]) ans_length len(memo[0]) len memo[0] memo 0 temp = 0 temp 0 for i in memo:
    if len(i) < ans_length:
        continue

    numlist = [cost_to_num(int(j)) for j in i]+ans_tail
    # print(numlist)
    numlist.sort(reverse=True)
    joined = "".join(numlist)
    # print(joined)
    if temp < int(joined):
        temp = int(joined) i memo if len(i) < ans_length:
        continue

     len(i) < ans_length len(i) len i ans_length continue cost_to_num(int(j)) for j in i for j in i j i for j in i cost_to_num(int(j)) cost_to_num int(j) int j numlist = [cost_to_num(int(j)) for j in i]+ans_tail numlist [cost_to_num(int(j)) for j in i]+ans_tail [cost_to_num(int(j)) for j in i] ans_tail numlist.sort(reverse=True) numlist.sort numlist sort reverse=True reverse True joined = "".join(numlist) joined "".join(numlist) "".join "" join numlist if temp < int(joined):
        temp = int(joined) temp < int(joined) temp int(joined) int joined temp = int(joined) temp int(joined) int joined ans_head = str(temp) ans_head str(temp) str temp print(ans_head) print ans_head ans_head = str(temp) str(temp) ans_head minlength = 0 0 minlength minlength = N // costlist[0] - 5 N // costlist[0] - 5 minlength ans_tail = [cost_to_num(costlist[0])]*minlength [cost_to_num(costlist[0])]*minlength ans_tail def find(nowstr, rest):
    if rest < 0:
        return ""

    for i in costlist:
        if i < rest:
            find(nowstr+str(i), rest - i)
        elif i == rest:

            memo.append(nowstr+str(i)) def find(nowstr, rest):
    if rest < 0:
        return ""

    for i in costlist:
        if i < rest:
            find(nowstr+str(i), rest - i)
        elif i == rest:

            memo.append(nowstr+str(i)) find temp = 0 0 temp ans_length = len(memo[0]) len(memo[0]) ans_length def cost_to_num(c):
    if c == 2:
        return "1"
    if c == 3:
        return "7"
    if c == 4:
        return "4"

    if c == 5:
        if 2 in num_to_use:
            return "2"
        elif 3 in num_to_use:
            return "3"
        else:
            return "5"
    if c == 6:
        if 6 in num_to_use:
            return "6"
        else:
            return "9"
    if c == 7:
        return "8" def cost_to_num(c):
    if c == 2:
        return "1"
    if c == 3:
        return "7"
    if c == 4:
        return "4"

    if c == 5:
        if 2 in num_to_use:
            return "2"
        elif 3 in num_to_use:
            return "3"
        else:
            return "5"
    if c == 6:
        if 6 in num_to_use:
            return "6"
        else:
            return "9"
    if c == 7:
        return "8" cost_to_num N, M = map(int, input().split()) map(int, input().split()) N Alist = list(map(int, input().split())) list(map(int, input().split())) Alist costlist = [ref[i-1] for i in num_to_use] [ref[i-1] for i in num_to_use] costlist num_to_use = set(Alist) set(Alist) num_to_use temp = int(joined) int(joined) temp memo = [] [] memo joined = "".join(numlist) "".join(numlist) joined M = map(int, input().split()) map(int, input().split()) M ref = [2, 5, 5, 4, 5, 6, 3, 7, 6] [2, 5, 5, 4, 5, 6, 3, 7, 6] ref numlist = [cost_to_num(int(j)) for j in i]+ans_tail [cost_to_num(int(j)) for j in i]+ans_tail numlist delta = N - minlength * costlist[0] N - minlength * costlist[0] delta
from collections import defaultdict

memo = []


def find(nowstr, rest):
    if rest < 0:
        return ""

    for i in costlist:
        if i < rest:
            find(nowstr+str(i), rest - i)
        elif i == rest:

            memo.append(nowstr+str(i))


def cost_to_num(c):
    if c == 2:
        return "1"
    if c == 3:
        return "7"
    if c == 4:
        return "4"

    if c == 5:
        if 2 in num_to_use:
            return "2"
        elif 3 in num_to_use:
            return "3"
        else:
            return "5"
    if c == 6:
        if 6 in num_to_use:
            return "6"
        else:
            return "9"
    if c == 7:
        return "8"


N, M = map(int, input().split())
Alist = list(map(int, input().split()))

num_to_use = set(Alist)
"""
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 を 1 つ作るには、
2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 本のマッチ棒を使う。
"""
ref = [2, 5, 5, 4, 5, 6, 3, 7, 6]
if 9 in num_to_use:
    num_to_use.discard(6)
if 5 in num_to_use:
    num_to_use.discard(2)
    num_to_use.discard(3)
if 3 in num_to_use:
    num_to_use.discard(2)


costlist = [ref[i-1] for i in num_to_use]
costlist.sort()
# print(num_to_use)
# print(costlist)
# print(costlist[0])
minlength = N // costlist[0] - 5
if minlength < 0:
    minlength = 0
ans_tail = [cost_to_num(costlist[0])]*minlength

delta = N - minlength * costlist[0]
# print(delta)
find("", delta)

# print(memo)

ans_length = len(memo[0])
temp = 0
for i in memo:
    if len(i) < ans_length:
        continue

    numlist = [cost_to_num(int(j)) for j in i]+ans_tail
    # print(numlist)
    numlist.sort(reverse=True)
    joined = "".join(numlist)
    # print(joined)
    if temp < int(joined):
        temp = int(joined)
ans_head = str(temp)

# print("---")
print(ans_head)
