import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N, M = NMI()
    A = NLI()

    match_dict = {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])}
    usable_list = [[a, match_dict[a]] for a in A]
    usable_list.sort(key=lambda x: (x[1], -x[0]))

    # dp[i] はi本使った時の最大数
    dp = [-1]*(N+10)
    dp[0] = 0

    for i in range(N+1):
        for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
    print(dp[N])



if __name__ == "__main__":
    main() import sys sys import math math from collections import deque sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 input = lambda: sys.stdin.readline().strip() input lambda: sys.stdin.readline().strip() sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip NI = lambda: int(input()) NI lambda: int(input()) int(input()) int input() input NMI = lambda: map(int, input().split()) NMI lambda: map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split NLI = lambda: list(NMI()) NLI lambda: list(NMI()) list(NMI()) list NMI() NMI SI = lambda: input() SI lambda: input() input() input [int(num)] * w for _ in range(h) for _ in range(h) _ range(h) range h for _ in range(h) [int(num)] * w [int(num)] int(num) int num w def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)] make_grid h h w w num num def main():
    N, M = NMI()
    A = NLI()

    match_dict = {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])}
    usable_list = [[a, match_dict[a]] for a in A]
    usable_list.sort(key=lambda x: (x[1], -x[0]))

    # dp[i] はi本使った時の最大数
    dp = [-1]*(N+10)
    dp[0] = 0

    for i in range(N+1):
        for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
    print(dp[N]) main N, M = NMI() N NMI() NMI M A = NLI() A NLI() NLI i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6]) for i, m in enumerate([2,5,5,4,5,6,3,7,6]) i m enumerate([2,5,5,4,5,6,3,7,6]) enumerate [2,5,5,4,5,6,3,7,6] 2 5 5 4 5 6 3 7 6 for i, m in enumerate([2,5,5,4,5,6,3,7,6]) i+1 i 1 m match_dict = {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])} match_dict {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])} [a, match_dict[a]] for a in A for a in A a A for a in A [a, match_dict[a]] a match_dict[a] match_dict a usable_list = [[a, match_dict[a]] for a in A] usable_list [[a, match_dict[a]] for a in A] usable_list.sort(key=lambda x: (x[1], -x[0])) usable_list.sort usable_list sort key=lambda x: (x[1], -x[0]) key lambda x: (x[1], -x[0]) (x[1], -x[0]) x[1] x 1 -x[0] x[0] x 0 x dp = [-1]*(N+10) dp [-1]*(N+10) [-1] -1 N+10 N 10 dp[0] = 0 dp[0] dp 0 0 for i in range(N+1):
        for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
     i range(N+1) range N+1 N 1 for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
     num m usable_list dp[i+m] = max(dp[i+m], dp[i]*10+num) dp[i+m] dp i+m i m max(dp[i+m], dp[i]*10+num) max dp[i+m] dp i+m i m dp[i]*10+num dp[i]*10 dp[i] dp i 10 num print(dp[N]) print dp[N] dp N if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main NI = lambda: int(input()) lambda: int(input()) NI MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD def main():
    N, M = NMI()
    A = NLI()

    match_dict = {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])}
    usable_list = [[a, match_dict[a]] for a in A]
    usable_list.sort(key=lambda x: (x[1], -x[0]))

    # dp[i] はi本使った時の最大数
    dp = [-1]*(N+10)
    dp[0] = 0

    for i in range(N+1):
        for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
    print(dp[N]) def main():
    N, M = NMI()
    A = NLI()

    match_dict = {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])}
    usable_list = [[a, match_dict[a]] for a in A]
    usable_list.sort(key=lambda x: (x[1], -x[0]))

    # dp[i] はi本使った時の最大数
    dp = [-1]*(N+10)
    dp[0] = 0

    for i in range(N+1):
        for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
    print(dp[N]) main NMI = lambda: map(int, input().split()) lambda: map(int, input().split()) NMI SI = lambda: input() lambda: input() SI input = lambda: sys.stdin.readline().strip() lambda: sys.stdin.readline().strip() input NLI = lambda: list(NMI()) lambda: list(NMI()) NLI def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)] def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)] make_grid
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N, M = NMI()
    A = NLI()

    match_dict = {i+1: m for i, m in enumerate([2,5,5,4,5,6,3,7,6])}
    usable_list = [[a, match_dict[a]] for a in A]
    usable_list.sort(key=lambda x: (x[1], -x[0]))

    # dp[i] はi本使った時の最大数
    dp = [-1]*(N+10)
    dp[0] = 0

    for i in range(N+1):
        for num, m in usable_list:
            dp[i+m] = max(dp[i+m], dp[i]*10+num)
    print(dp[N])



if __name__ == "__main__":
    main()