import sys

sys.setrecursionlimit(100000)

n, m = map(int, input().split())
a = list(map(int, input().split()))
memo = {}
matches = [100, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def doit(n):
    if n == 0:
        return ""
    if not n in memo:
        ret = None
        for d in a:
            m = matches[d]
            if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
        memo[n] = ret
    return memo[n]

print(doit(n)) import sys sys sys.setrecursionlimit(100000) sys.setrecursionlimit sys setrecursionlimit 100000 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 memo = {} memo {} matches = [100, 2, 5, 5, 4, 5, 6, 3, 7, 6] matches [100, 2, 5, 5, 4, 5, 6, 3, 7, 6] 100 2 5 5 4 5 6 3 7 6 def doit(n):
    if n == 0:
        return ""
    if not n in memo:
        ret = None
        for d in a:
            m = matches[d]
            if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
        memo[n] = ret
    return memo[n] doit if n == 0:
        return ""
     n == 0 n 0 return "" "" if not n in memo:
        ret = None
        for d in a:
            m = matches[d]
            if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
        memo[n] = ret
     not n in memo n in memo n memo ret = None ret None for d in a:
            m = matches[d]
            if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
         d a m = matches[d] m matches[d] matches d if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
         n - m >= 0 and doit(n - m) is not None n - m >= 0 n - m n m 0 doit(n - m) is not None doit(n - m) doit n - m n m None r = max(doit(n - m) + str(d), str(d) + doit(n - m)) r max(doit(n - m) + str(d), str(d) + doit(n - m)) max doit(n - m) + str(d) doit(n - m) doit n - m n m str(d) str d str(d) + doit(n - m) str(d) str d doit(n - m) doit n - m n m if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
         ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret ret is None or len(r) > len(ret) ret is None ret None len(r) > len(ret) len(r) len r len(ret) len ret len(r) == len(ret) and r > ret len(r) == len(ret) len(r) len r len(ret) len ret r > ret r ret ret = r ret r memo[n] = ret memo[n] memo n ret return memo[n] memo[n] memo n n n print(doit(n)) print doit(n) doit n matches = [100, 2, 5, 5, 4, 5, 6, 3, 7, 6] [100, 2, 5, 5, 4, 5, 6, 3, 7, 6] matches m = map(int, input().split()) map(int, input().split()) m memo = {} {} memo a = list(map(int, input().split())) list(map(int, input().split())) a n, m = map(int, input().split()) map(int, input().split()) n def doit(n):
    if n == 0:
        return ""
    if not n in memo:
        ret = None
        for d in a:
            m = matches[d]
            if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
        memo[n] = ret
    return memo[n] def doit(n):
    if n == 0:
        return ""
    if not n in memo:
        ret = None
        for d in a:
            m = matches[d]
            if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
        memo[n] = ret
    return memo[n] doit
import sys

sys.setrecursionlimit(100000)

n, m = map(int, input().split())
a = list(map(int, input().split()))
memo = {}
matches = [100, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def doit(n):
    if n == 0:
        return ""
    if not n in memo:
        ret = None
        for d in a:
            m = matches[d]
            if n - m >= 0 and doit(n - m) is not None:
                r = max(doit(n - m) + str(d), str(d) + doit(n - m))
                if ret is None or len(r) > len(ret) or len(r) == len(ret) and r > ret:
                    ret = r
        memo[n] = ret
    return memo[n]

print(doit(n))