#!/usr/bin/python

#
# Stacking Numbers
# Problem 3, AIC 2003
# Python Sample Solution (slow but simple, scores 50%)
#

#
# This solution simply runs through all possible choices for the six
# numbers at the bottom of the pyramid.  From these it selects the
# combination that gives the highest possible score (number at the
# top of the pyramid).
#
# If there are N numbers to choose from in the input set, we expect
# a bit under N^6 combinations overall for large N.  If N = 32 (the
# maximum case allowed in the problem statement) then this is about
# 1,000,000,000 combinations which is too many for a 2s time limit.
#

#
# Calculates the score at the top of the pyramid from the six numbers
# currently at the bottom of the pyramid.  The numbers at the bottom of
# the pyramid are start(1..6).
#
def score(start, modBase):
    left = (start[0] + (4 * start[1]) + (6 * start[2]) +
        (4 * start[3]) + start[4]) % modBase
    right = (start[1] + (4 * start[2]) + (6 * start[3]) +
        (4 * start[4]) + start[5]) % modBase
    return left * right

def main():
    #
    # The main program.
    #

    # Open the I/O files.
    input_file = file("stackin.txt", "r")
    output_file = file("stackout.txt", "w")

    # Read the input data.
    modBase = int(input_file.readline())
    setSize = int(input_file.readline())
    inputSet = map(int, input_file.readline().split())

    # Search through all possible combinations.
    # This is done with six nested FOR loops, one for each number at
    # the bottom of the pyramid.
    # Note that we ensure at each stage that these six numbers are
    # distinct.
    bestScore = -1

    start = [0] * 6
    for start[0] in inputSet:
        for start[1] in inputSet:
            if start[1] in start[:1]:
                continue
            for start[2] in inputSet:
                if start[2] in start[:2]:
                    continue
                for start[3] in inputSet:
                    if start[3] in start[:3]:
                        continue
                    for start[4] in inputSet:
                        if start[4] in start[:4]:
                            continue
                        for start[5] in inputSet:
                            if start[5] in start[:5]:
                                continue
                            # We have a selection of six starting numbers.
                            # See if the resulting score is better than
                            # we've seen so far.
                            currScore = score(start, modBase)
                            if currScore > bestScore:
                                bestScore = currScore
                                # Copy the start array into ans.
                                ans = start[:]

    # Output the best solution that was found.
    output_file.write('%d\n' % (bestScore))
    output_file.write('%d %d %d %d %d %d\n' % tuple(ans))

    # Tidy up.
    input_file.close()
    output_file.close()

# Call the main program.
main()
