def main():
    n, m, *a = map(int, open(0).read().split())
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)
    b = {}
    for i in a:
        x = t[i]
        if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

    b = [t[i] for i in a]
    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans)


if __name__ == '__main__':
    main() def main():
    n, m, *a = map(int, open(0).read().split())
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)
    b = {}
    for i in a:
        x = t[i]
        if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

    b = [t[i] for i in a]
    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans) main n, m, *a = map(int, open(0).read().split()) n map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split m *a a t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} t {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 a.sort(reverse=True) a.sort a sort reverse=True reverse True b = {} b {} for i in a:
        x = t[i]
        if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

     i a x = t[i] x t[i] t i if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

     b.get(x) is not None b.get(x) b.get b get x None a.remove(i) a.remove a remove i b[x] = 0 b[x] b x 0 t[i] for i in a for i in a i a for i in a t[i] t i b = [t[i] for i in a] b [t[i] for i in a] dp = [-float('Inf')] * (n + 10) dp [-float('Inf')] * (n + 10) [-float('Inf')] -float('Inf') float('Inf') float 'Inf' n + 10 n 10 dp[0] = 0 dp[0] dp 0 0 for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

     i range(2, n + 1) range 2 n + 1 n 1 dp[i] = max(dp[i - j] + 1 for j in b) dp[i] dp i max(dp[i - j] + 1 for j in b) max dp[i - j] + 1 dp[i - j] dp i - j i j 1 nums = [] nums [] for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

     i range(dp[n], 0, -1) range dp[n] dp n 0 -1 for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

     x a if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

     dp[n - t[x]] == i - 1 dp[n - t[x]] dp n - t[x] n t[x] t x i - 1 i 1 nums.append(x) nums.append nums append x n -= t[x] n t[x] t x break ans = ''.join(map(str, nums)) ans ''.join(map(str, nums)) ''.join '' join map(str, nums) map str nums print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    n, m, *a = map(int, open(0).read().split())
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)
    b = {}
    for i in a:
        x = t[i]
        if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

    b = [t[i] for i in a]
    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans) def main():
    n, m, *a = map(int, open(0).read().split())
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)
    b = {}
    for i in a:
        x = t[i]
        if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

    b = [t[i] for i in a]
    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans) main
def main():
    n, m, *a = map(int, open(0).read().split())
    t = {1: 2, 2: 5, 3: 5, 4: 4, 5: 5, 6: 6, 7: 3, 8: 7, 9: 6}
    a.sort(reverse=True)
    b = {}
    for i in a:
        x = t[i]
        if b.get(x) is not None:
            a.remove(i)
        else:
            b[x] = 0

    b = [t[i] for i in a]
    dp = [-float('Inf')] * (n + 10)
    dp[0] = 0
    for i in range(2, n + 1):
        dp[i] = max(dp[i - j] + 1 for j in b)

    nums = []
    for i in range(dp[n], 0, -1):
        for x in a:
            if dp[n - t[x]] == i - 1:
                nums.append(x)
                n -= t[x]
                break

    ans = ''.join(map(str, nums))
    print(ans)


if __name__ == '__main__':
    main()
