import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def create_integer_string(counts):
    res = ""

    for i in range(9, 0, -1):
        count = counts[i]
        res += str(i) * count

    return res


def main():
    from copy import copy

    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    pat = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    match_nums = dict()

    for x in a:
        match_num = pat[x]
        match_nums[match_num] = x

    dp = [0] * (n + 1)
    dp_c = [[0] * 10 for _ in range(n + 1)]

    for i in range(n + 1):
        for match_num, val in match_nums.items():
            if i + match_num > n:
                continue
            if i > 0 and dp[i] == 0:
                continue
            if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

    counts = dp_c[n]

    print(create_integer_string(counts))


if __name__ == '__main__':
    main() import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = float('INF') INF float('INF') float 'INF' sys.setrecursionlimit(10 ** 5) sys.setrecursionlimit sys setrecursionlimit 10 ** 5 10 5 def create_integer_string(counts):
    res = ""

    for i in range(9, 0, -1):
        count = counts[i]
        res += str(i) * count

    return res create_integer_string res = "" res "" for i in range(9, 0, -1):
        count = counts[i]
        res += str(i) * count

     i range(9, 0, -1) range 9 0 -1 count = counts[i] count counts[i] counts i res += str(i) * count res str(i) * count str(i) str i count return res res counts counts def main():
    from copy import copy

    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    pat = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    match_nums = dict()

    for x in a:
        match_num = pat[x]
        match_nums[match_num] = x

    dp = [0] * (n + 1)
    dp_c = [[0] * 10 for _ in range(n + 1)]

    for i in range(n + 1):
        for match_num, val in match_nums.items():
            if i + match_num > n:
                continue
            if i > 0 and dp[i] == 0:
                continue
            if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

    counts = dp_c[n]

    print(create_integer_string(counts)) main from copy import copy n, m = list(map(int, readline().split())) n list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split m a = list(map(int, readline().split())) a list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split a.sort() a.sort a sort pat = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] pat [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6] -1 2 5 5 4 5 6 3 7 6 match_nums = dict() match_nums dict() dict for x in a:
        match_num = pat[x]
        match_nums[match_num] = x

     x a match_num = pat[x] match_num pat[x] pat x match_nums[match_num] = x match_nums[match_num] match_nums match_num x dp = [0] * (n + 1) dp [0] * (n + 1) [0] 0 n + 1 n 1 [0] * 10 for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) [0] * 10 [0] 0 10 dp_c = [[0] * 10 for _ in range(n + 1)] dp_c [[0] * 10 for _ in range(n + 1)] for i in range(n + 1):
        for match_num, val in match_nums.items():
            if i + match_num > n:
                continue
            if i > 0 and dp[i] == 0:
                continue
            if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

     i range(n + 1) range n + 1 n 1 for match_num, val in match_nums.items():
            if i + match_num > n:
                continue
            if i > 0 and dp[i] == 0:
                continue
            if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

     match_num val match_nums.items() match_nums.items match_nums items if i + match_num > n:
                continue
             i + match_num > n i + match_num i match_num n continue if i > 0 and dp[i] == 0:
                continue
             i > 0 and dp[i] == 0 i > 0 i 0 dp[i] == 0 dp[i] dp i 0 continue if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

     dp[i + match_num] < dp[i] + 1 dp[i + match_num] dp i + match_num i match_num dp[i] + 1 dp[i] dp i 1 dp[i + match_num] = dp[i] + 1 dp[i + match_num] dp i + match_num i match_num dp[i] + 1 dp[i] dp i 1 dp_c[i + match_num] = copy(dp_c[i]) dp_c[i + match_num] dp_c i + match_num i match_num copy(dp_c[i]) copy dp_c[i] dp_c i dp_c[i + match_num][val] += 1 dp_c[i + match_num][val] [i + match_num] dp_c i + match_num i match_num val 1 elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

     dp[i + match_num] == dp[i] + 1 dp[i + match_num] dp i + match_num i match_num dp[i] + 1 dp[i] dp i 1 counts_cur = copy(dp_c[i + match_num]) counts_cur copy(dp_c[i + match_num]) copy dp_c[i + match_num] dp_c i + match_num i match_num cur = create_integer_string(counts_cur) cur create_integer_string(counts_cur) create_integer_string counts_cur counts_new = copy(dp_c[i]) counts_new copy(dp_c[i]) copy dp_c[i] dp_c i counts_new[val] += 1 counts_new[val] counts_new val 1 new = create_integer_string(counts_new) new create_integer_string(counts_new) create_integer_string counts_new if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

     new > cur new cur dp[i + match_num] = dp[i] + 1 dp[i + match_num] dp i + match_num i match_num dp[i] + 1 dp[i] dp i 1 dp_c[i + match_num] = copy(dp_c[i]) dp_c[i + match_num] dp_c i + match_num i match_num copy(dp_c[i]) copy dp_c[i] dp_c i dp_c[i + match_num][val] += 1 dp_c[i + match_num][val] [i + match_num] dp_c i + match_num i match_num val 1 counts = dp_c[n] counts dp_c[n] dp_c n print(create_integer_string(counts)) print create_integer_string(counts) create_integer_string counts if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD INF = float('INF') float('INF') INF def create_integer_string(counts):
    res = ""

    for i in range(9, 0, -1):
        count = counts[i]
        res += str(i) * count

    return res def create_integer_string(counts):
    res = ""

    for i in range(9, 0, -1):
        count = counts[i]
        res += str(i) * count

    return res create_integer_string def main():
    from copy import copy

    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    pat = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    match_nums = dict()

    for x in a:
        match_num = pat[x]
        match_nums[match_num] = x

    dp = [0] * (n + 1)
    dp_c = [[0] * 10 for _ in range(n + 1)]

    for i in range(n + 1):
        for match_num, val in match_nums.items():
            if i + match_num > n:
                continue
            if i > 0 and dp[i] == 0:
                continue
            if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

    counts = dp_c[n]

    print(create_integer_string(counts)) def main():
    from copy import copy

    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    pat = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    match_nums = dict()

    for x in a:
        match_num = pat[x]
        match_nums[match_num] = x

    dp = [0] * (n + 1)
    dp_c = [[0] * 10 for _ in range(n + 1)]

    for i in range(n + 1):
        for match_num, val in match_nums.items():
            if i + match_num > n:
                continue
            if i > 0 and dp[i] == 0:
                continue
            if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

    counts = dp_c[n]

    print(create_integer_string(counts)) main readline = sys.stdin.readline sys.stdin.readline readline
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def create_integer_string(counts):
    res = ""

    for i in range(9, 0, -1):
        count = counts[i]
        res += str(i) * count

    return res


def main():
    from copy import copy

    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    pat = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    match_nums = dict()

    for x in a:
        match_num = pat[x]
        match_nums[match_num] = x

    dp = [0] * (n + 1)
    dp_c = [[0] * 10 for _ in range(n + 1)]

    for i in range(n + 1):
        for match_num, val in match_nums.items():
            if i + match_num > n:
                continue
            if i > 0 and dp[i] == 0:
                continue
            if dp[i + match_num] < dp[i] + 1:
                dp[i + match_num] = dp[i] + 1
                dp_c[i + match_num] = copy(dp_c[i])
                dp_c[i + match_num][val] += 1
            elif dp[i + match_num] == dp[i] + 1:
                counts_cur = copy(dp_c[i + match_num])
                cur = create_integer_string(counts_cur)
                counts_new = copy(dp_c[i])
                counts_new[val] += 1
                new = create_integer_string(counts_new)
                if new > cur:
                    dp[i + match_num] = dp[i] + 1
                    dp_c[i + match_num] = copy(dp_c[i])
                    dp_c[i + match_num][val] += 1

    counts = dp_c[n]

    print(create_integer_string(counts))


if __name__ == '__main__':
    main()
