def main():
    n, m = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    b = [0,2,5,5,4,5,6,3,7,6]
    c = [[b[i], -i] for i in a]
    c.sort()
    result = [[] for _ in range(n+1)]
    dp = [-1]*(n+1)
    dp[0] = 0
    for i in range(1, n+1):
        for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
    c = [[-x[1], x[0]] for x in c]
    c.sort(reverse = True)
    result = []
    i = n
    while i > 0:
        j = 0
        while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
    print(''.join(map(str, result)))
if __name__ =='__main__':
    main() def main():
    n, m = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    b = [0,2,5,5,4,5,6,3,7,6]
    c = [[b[i], -i] for i in a]
    c.sort()
    result = [[] for _ in range(n+1)]
    dp = [-1]*(n+1)
    dp[0] = 0
    for i in range(1, n+1):
        for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
    c = [[-x[1], x[0]] for x in c]
    c.sort(reverse = True)
    result = []
    i = n
    while i > 0:
        j = 0
        while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
    print(''.join(map(str, result))) main 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 ' ' b = [0,2,5,5,4,5,6,3,7,6] b [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 [b[i], -i] for i in a for i in a i a for i in a [b[i], -i] b[i] b i -i i c = [[b[i], -i] for i in a] c [[b[i], -i] for i in a] c.sort() c.sort c sort [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] result = [[] for _ in range(n+1)] result [[] for _ in range(n+1)] dp = [-1]*(n+1) dp [-1]*(n+1) [-1] -1 n+1 n 1 dp[0] = 0 dp[0] dp 0 0 for i in range(1, n+1):
        for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
     i range(1, n+1) range 1 n+1 n 1 for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
     j c if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
     i-j[0] >= 0 i-j[0] i j[0] j 0 0 if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
     dp[i-j[0]] >= 0 dp[i-j[0]] dp i-j[0] i j[0] j 0 0 dp[i] = dp[i-j[0]] +1 dp[i] dp i dp[i-j[0]] +1 dp[i-j[0]] dp i-j[0] i j[0] j 0 1 break [-x[1], x[0]] for x in c for x in c x c for x in c [-x[1], x[0]] -x[1] x[1] x 1 x[0] x 0 c = [[-x[1], x[0]] for x in c] c [[-x[1], x[0]] for x in c] c.sort(reverse = True) c.sort c sort reverse = True reverse True result = [] result [] i = n i n while i > 0:
        j = 0
        while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
     i > 0 i 0 j = 0 j 0 while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
     j < len(c) j len(c) len c if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
     i-c[j][1] >= 0 i-c[j][1] i c[j][1] [j] c j 1 0 if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
             dp[i-c[j][1]] + 1 == dp[i] dp[i-c[j][1]] + 1 dp[i-c[j][1]] dp i-c[j][1] i c[j][1] [j] c j 1 1 dp[i] dp i result.append(c[j][0]) result.append result append c[j][0] [j] c j 0 i -= c[j][1] i c[j][1] [j] c j 1 j = len(c) j len(c) len c j += 1 j 1 j += 1 j 1 print(''.join(map(str, result))) print ''.join(map(str, result)) ''.join '' join map(str, result) map str result if __name__ =='__main__':
    main() __name__ =='__main__' __name__ '__main__' main() main def main():
    n, m = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    b = [0,2,5,5,4,5,6,3,7,6]
    c = [[b[i], -i] for i in a]
    c.sort()
    result = [[] for _ in range(n+1)]
    dp = [-1]*(n+1)
    dp[0] = 0
    for i in range(1, n+1):
        for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
    c = [[-x[1], x[0]] for x in c]
    c.sort(reverse = True)
    result = []
    i = n
    while i > 0:
        j = 0
        while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
    print(''.join(map(str, result))) def main():
    n, m = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    b = [0,2,5,5,4,5,6,3,7,6]
    c = [[b[i], -i] for i in a]
    c.sort()
    result = [[] for _ in range(n+1)]
    dp = [-1]*(n+1)
    dp[0] = 0
    for i in range(1, n+1):
        for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
    c = [[-x[1], x[0]] for x in c]
    c.sort(reverse = True)
    result = []
    i = n
    while i > 0:
        j = 0
        while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
    print(''.join(map(str, result))) main
def main():
    n, m = map(int, input().split(' '))
    a = list(map(int, input().split(' ')))
    b = [0,2,5,5,4,5,6,3,7,6]
    c = [[b[i], -i] for i in a]
    c.sort()
    result = [[] for _ in range(n+1)]
    dp = [-1]*(n+1)
    dp[0] = 0
    for i in range(1, n+1):
        for j in c:
            if i-j[0] >= 0:
                if dp[i-j[0]] >= 0:
                    dp[i] = dp[i-j[0]] +1
                    break
    c = [[-x[1], x[0]] for x in c]
    c.sort(reverse = True)
    result = []
    i = n
    while i > 0:
        j = 0
        while j < len(c):
            if i-c[j][1] >= 0:
                if dp[i-c[j][1]] + 1 == dp[i]:
                    result.append(c[j][0])
                    i -= c[j][1]
                    j = len(c)
                else:
                    j += 1
            else:
                j += 1
    print(''.join(map(str, result)))
if __name__ =='__main__':
    main()