nm = input().split()
n = int(nm[0])
m = int(nm[1])
alist = set([int(i) for i in input().split()])

piece_num = {2: {1},
             3: {7},
             4: {4},
             5: {2, 3, 5},
             6: {6, 9},
             7: {8},
            }


def print_count(num_count_list):
    to_print = ''
    num_count_list.sort(reverse=True)
    for num, count in num_count_list:
        to_print += str(num) * count
    print(to_print)


def safe_max(nset):
    try:
        return max(nset)
    except:
        return -1


if piece_num[2] & alist:
    max_2 = max(piece_num[2] & alist)
    if n & 1 == 0:
        print(str(max_2) * (n // 2))
    else:
        if piece_num[3] & alist:
            print_count([(max(piece_num[3] & alist), 1), (max_2, max(0, n // 2 - 1))])
        elif piece_num[5] & alist:
            print_count([(max(piece_num[5] & alist), 1), (max_2, max(0, n // 2 - 2))])
        elif piece_num[7] & alist:
            print_count([(max(piece_num[7] & alist), 1), (max_2, max(0, n // 2 - 3))])

elif piece_num[3] & alist:
    max_3 = max(piece_num[3] & alist)
    if n % 3 == 0:
        print(str(max_3) * (n // 3))
    elif n % 3 == 1:
        if piece_num[4] & alist:
            print_count([(max(piece_num[4] & alist), 1), (max_3, max(0, n // 3 - 1))])
        elif piece_num[7] & alist:
            print_count([(max(piece_num[7] & alist), 1), (max_3, max(0, n // 3 - 2))])
        elif piece_num[5] & alist:
            print_count([(max(piece_num[5] & alist), 2), (max_3, max(0, n // 3 - 3))])
    elif n % 3 == 2:
        if piece_num[5] & alist:
            max_5 = max(piece_num[5] & alist)
            print_count([(max_5, 1), (max_3, max(0, n // 3 - 1))])
        elif piece_num[4] & alist:
            max_4 = max(piece_num[4] & alist)
            print_count([(max_4, 2), (max_3, max(0, n // 3 - 2))])
        else:
            print_count([(max(piece_num[7] & alist), 2), (max_3, max(0, n // 3 - 4))])

elif piece_num[4] & alist:
    max_4 = max(piece_num[4] & alist)
    if n % 4 == 0:
        print(str(max_4) * (n // 4))
    elif n % 4 == 1:
        if piece_num[5] & alist:
            print_count([(max(piece_num[5] & alist), 1), (max_4, max(0, n // 4 - 1))])
        elif (piece_num[6] & alist) and (piece_num[7] & alist):
            max_6 = max(piece_num[6] & alist)
            max_7 = max(piece_num[7] & alist)
            print_count([(max_6, 1), (max_7, 1), (max_4, max(0, n // 4 - 3))])
        elif piece_num[7] & alist:
            max_7 = max(piece_num[7] & alist)
            print_count([(max_7, 3), (max_4, max(0, n // 4 - 5))])
    elif n % 4 == 2:
        if (piece_num[5] & alist) and (piece_num[6] & alist):
            max_5 = max(piece_num[5] & alist)
            max_6 = max(piece_num[6] & alist)
            if max_5 < max_6:
                print_count([(max_6, 1), (max_4, max(0, n // 4 - 1))])
            else:
                print_count([(max_5, 2), (max_4, max(0, n // 4 - 2))])
        elif piece_num[5] & alist:
            max_5 = max(piece_num[5] & alist)
            print_count([(max_5, 2), (max_4, max(0, n // 4 - 2))])
        elif piece_num[6] & alist:
            max_6 = max(piece_num[6] & alist)
            print_count([(max_6, 1), (max_4, max(0, n // 4 - 1))])
        else:
            max_7 = max(piece_num[7] & alist)
            print_count([(max_7, 2), (max_4, max(0, n // 4 - 3))])
    elif n % 4 == 3:
        max_5 = safe_max(piece_num[5] & alist)
        max_6 = safe_max(piece_num[6] & alist)
        max_7 = safe_max(piece_num[7] & alist)
        if max_7 == max(max_5, max_6, max_7):
            print_count([(max_7, 1), (max_4, max(0, n // 4 - 1))])
        elif max_5 == max(max_5, max_6, max_7):
            print_count([(max_5, 3), (max_4, max(0, n // 4 - 3))])
        else:
            if piece_num[5] & alist:
                print_count([(max_6, 1), (max_5, 1), (max_4, max(0, n // 4 - 2))])
            else:
                print_count([(max_7, 1), (max_4, max(0, n // 4 - 1))])

elif piece_num[5] & alist:
    max_5 = max(piece_num[5] & alist)
    if n % 5 == 0:
        print(str(max_5) * (n // 5))
    elif n % 5 == 1:
        if piece_num[6] & alist:
            max_6 = safe_max(piece_num[6] & alist)
            print_count([(max_6, 1), (max_5, max(0, n // 5 - 1))])
        else:
            max_7 = safe_max(piece_num[7] & alist)
            print_count([(max_7, 3), (max_5, max(0, n // 5 - 4))])
    elif n % 5 == 2:
        max_6 = safe_max(piece_num[6] & alist)
        max_7 = safe_max(piece_num[7] & alist)
        if max_7 == max(max_6, max_7):
            print_count([(max_7, 1), (max_5, max(0, n // 5 - 1))])
        else:
            print_count([(max_6, 2), (max_5, max(0, n // 5 - 2))])
    elif n % 5 == 3:
        max_6 = safe_max(piece_num[6] & alist)
        max_7 = safe_max(piece_num[7] & alist)
        if max_7 == max(max_6, max_7):
            if piece_num[6] & alist:
                print_count([(max_7, 1), (max_6, 1), (max_5, max(0, n // 5 - 2))])
            else:
                print_count([(max_7, 4), (max_5, max(0, n // 5 - 5))])
        else:
            print_count([(max_6, 3), (max_5, max(0, n // 5 - 3))])
    elif n % 5 == 4:
        max_6 = safe_max(piece_num[6] & alist)
        max_7 = safe_max(piece_num[7] & alist)
        if max_7 == max(max_6, max_7):
            print_count([(max_7, 2), (max_5, max(0, n // 5 - 2))])
        else:
            print_count([(max_6, 4), (max_5, max(0, n // 5 - 4))])

elif piece_num[6] & alist:
    max_6 = max(piece_num[6] & alist)
    if n % 6 == 0:
        print(str(max_6) * (n // 6))
    else:
        count_7 = n % 6
        max_7 = max(piece_num[7] & alist)
        print_count([(max_7, count_7), (max_6, max(0, n // 6 - count_7))])

else:
    max_7 = max(piece_num[7] & alist)
    print(str(max_7) * (n // 7))




Measured memory_bytes: 38512