def main():
    n, m = map(int, input().split())
    a = set(map(int, input().split()))

    # type(x) == type(y) == string
    def bigger(x, y):
        if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

    l = []
    if 1 in a:
        l.append((2, '1'))

    if 7 in a:
        l.append((3, '7'))

    if 4 in a:
        l.append((4, '4'))

    if 5 in a:
        l.append((5, '5'))
    elif 3 in a:
        l.append((5, '3'))
    elif 2 in a:
        l.append((5, '2'))

    if 9 in a:
        l.append((6, '9'))
    elif 6 in a:
        l.append((6, '6'))

    if 8 in a:
        l.append((7, '8'))

    dp = [None] * (n+1)
    dp[0] = ''
    for i in range(1, n+1):
        for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

    print(dp[n])

main() def main():
    n, m = map(int, input().split())
    a = set(map(int, input().split()))

    # type(x) == type(y) == string
    def bigger(x, y):
        if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

    l = []
    if 1 in a:
        l.append((2, '1'))

    if 7 in a:
        l.append((3, '7'))

    if 4 in a:
        l.append((4, '4'))

    if 5 in a:
        l.append((5, '5'))
    elif 3 in a:
        l.append((5, '3'))
    elif 2 in a:
        l.append((5, '2'))

    if 9 in a:
        l.append((6, '9'))
    elif 6 in a:
        l.append((6, '6'))

    if 8 in a:
        l.append((7, '8'))

    dp = [None] * (n+1)
    dp[0] = ''
    for i in range(1, n+1):
        for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

    print(dp[n]) main n, m = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split m a = set(map(int, input().split())) a set(map(int, input().split())) set map(int, input().split()) map int input().split() ().split () input split def bigger(x, y):
        if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

     bigger if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

     len(x) > len(y) len(x) len x len(y) len y return x x elif len(x) < len(y):
            return y
         len(x) < len(y) len(x) len x len(y) len y return y y elif x < y:
            return y
         x < y x y return y y return x x x x y y l = [] l [] if 1 in a:
        l.append((2, '1'))

     1 in a 1 a l.append((2, '1')) l.append l append (2, '1') 2 '1' if 7 in a:
        l.append((3, '7'))

     7 in a 7 a l.append((3, '7')) l.append l append (3, '7') 3 '7' if 4 in a:
        l.append((4, '4'))

     4 in a 4 a l.append((4, '4')) l.append l append (4, '4') 4 '4' if 5 in a:
        l.append((5, '5'))
    elif 3 in a:
        l.append((5, '3'))
    elif 2 in a:
        l.append((5, '2'))

     5 in a 5 a l.append((5, '5')) l.append l append (5, '5') 5 '5' elif 3 in a:
        l.append((5, '3'))
     3 in a 3 a l.append((5, '3')) l.append l append (5, '3') 5 '3' elif 2 in a:
        l.append((5, '2'))

     2 in a 2 a l.append((5, '2')) l.append l append (5, '2') 5 '2' if 9 in a:
        l.append((6, '9'))
    elif 6 in a:
        l.append((6, '6'))

     9 in a 9 a l.append((6, '9')) l.append l append (6, '9') 6 '9' elif 6 in a:
        l.append((6, '6'))

     6 in a 6 a l.append((6, '6')) l.append l append (6, '6') 6 '6' if 8 in a:
        l.append((7, '8'))

     8 in a 8 a l.append((7, '8')) l.append l append (7, '8') 7 '8' dp = [None] * (n+1) dp [None] * (n+1) [None] None n+1 n 1 dp[0] = '' dp[0] dp 0 '' for i in range(1, n+1):
        for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

     i range(1, n+1) range 1 n+1 n 1 for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

     cost char l if i - cost < 0 or dp[i-cost] is None:
                continue
             i - cost < 0 or dp[i-cost] is None i - cost < 0 i - cost i cost 0 dp[i-cost] is None dp[i-cost] dp i-cost i cost None continue candidate = max(dp[i-cost] + char, char + dp[i-cost]) candidate max(dp[i-cost] + char, char + dp[i-cost]) max dp[i-cost] + char dp[i-cost] dp i-cost i cost char char + dp[i-cost] char dp[i-cost] dp i-cost i cost if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

     dp[i] is None dp[i] dp i None dp[i] = candidate dp[i] dp i candidate dp[i] = bigger(dp[i], candidate) dp[i] dp i bigger(dp[i], candidate) bigger dp[i] dp i candidate print(dp[n]) print dp[n] dp n main() main def main():
    n, m = map(int, input().split())
    a = set(map(int, input().split()))

    # type(x) == type(y) == string
    def bigger(x, y):
        if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

    l = []
    if 1 in a:
        l.append((2, '1'))

    if 7 in a:
        l.append((3, '7'))

    if 4 in a:
        l.append((4, '4'))

    if 5 in a:
        l.append((5, '5'))
    elif 3 in a:
        l.append((5, '3'))
    elif 2 in a:
        l.append((5, '2'))

    if 9 in a:
        l.append((6, '9'))
    elif 6 in a:
        l.append((6, '6'))

    if 8 in a:
        l.append((7, '8'))

    dp = [None] * (n+1)
    dp[0] = ''
    for i in range(1, n+1):
        for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

    print(dp[n]) def main():
    n, m = map(int, input().split())
    a = set(map(int, input().split()))

    # type(x) == type(y) == string
    def bigger(x, y):
        if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

    l = []
    if 1 in a:
        l.append((2, '1'))

    if 7 in a:
        l.append((3, '7'))

    if 4 in a:
        l.append((4, '4'))

    if 5 in a:
        l.append((5, '5'))
    elif 3 in a:
        l.append((5, '3'))
    elif 2 in a:
        l.append((5, '2'))

    if 9 in a:
        l.append((6, '9'))
    elif 6 in a:
        l.append((6, '6'))

    if 8 in a:
        l.append((7, '8'))

    dp = [None] * (n+1)
    dp[0] = ''
    for i in range(1, n+1):
        for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

    print(dp[n]) main
def main():
    n, m = map(int, input().split())
    a = set(map(int, input().split()))

    # type(x) == type(y) == string
    def bigger(x, y):
        if len(x) > len(y):
            return x
        elif len(x) < len(y):
            return y
        elif x < y:
            return y
        else:
            return x

    l = []
    if 1 in a:
        l.append((2, '1'))

    if 7 in a:
        l.append((3, '7'))

    if 4 in a:
        l.append((4, '4'))

    if 5 in a:
        l.append((5, '5'))
    elif 3 in a:
        l.append((5, '3'))
    elif 2 in a:
        l.append((5, '2'))

    if 9 in a:
        l.append((6, '9'))
    elif 6 in a:
        l.append((6, '6'))

    if 8 in a:
        l.append((7, '8'))

    dp = [None] * (n+1)
    dp[0] = ''
    for i in range(1, n+1):
        for cost, char in l:
            if i - cost < 0 or dp[i-cost] is None:
                continue
            candidate = max(dp[i-cost] + char, char + dp[i-cost])
            if dp[i] is None:
                dp[i] = candidate
            else:
                dp[i] = bigger(dp[i], candidate)

    print(dp[n])

main()