
/**
 * Stacking Numbers
 * Problem 3, AIC 2003
 * C++ 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.
 */

#include <fstream>

// Constants from the problem statement:
#define MAXSET 32   // Maximum size of the input set

// Variables holding the input data:
long base;          // Modular base to use with addition
long set[MAXSET];   // Set of available starting numbers to choose from
long setSize;       // Number of available starting numbers to choose from

// Variables describing the current and best arrangements:
long start[6];      // The six numbers at the bottom of the pyramid
long ans[6];        // The six numbers that give the best possible final score
long bestScore;     // The best possible final score

// Input and output files:
std::ifstream in("stackin.txt");
std::ofstream out("stackout.txt");

/**
 * 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[0..5].
 */
long score() {
    long left = (start[0] + (4 * start[1]) + (6 * start[2]) +
        (4 * start[3]) + start[4]) % base;
    long right = (start[1] + (4 * start[2]) + (6 * start[3]) +
        (4 * start[4]) + start[5]) % base;
    return left * right;
}

/**
 * The main program.
 */
int main() {
    // Read the input data.
    in >> base >> setSize;
    for (int i = 0; i < setSize; i++)
        in >> set[i];

    // 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;
    int i0, i1, i2, i3, i4, i5;

    long currScore;
    for (i0 = 0; i0 < setSize; i0++) {
        start[0] = set[i0];
        for (i1 = 0; i1 < setSize; i1++) {
            if (i1 == i0)
                continue;
            start[1] = set[i1];
            for (i2 = 0; i2 < setSize; i2++) {
                if (i2 == i1 || i2 == i0)
                    continue;
                start[2] = set[i2];
                for (i3 = 0; i3 < setSize; i3++) {
                    if (i3 == i2 || i3 == i1 || i3 == i0)
                        continue;
                    start[3] = set[i3];
                    for (i4 = 0; i4 < setSize; i4++) {
                        if (i4 == i3 || i4 == i2 || i4 == i1 || i4 == i0)
                            continue;
                        start[4] = set[i4];
                        for (i5 = 0; i5 < setSize; i5++) {
                            if (i5 == i4 || i5 == i3 || i5 == i2 ||
                                    i5 == i1 || i5 == i0)
                                continue;
                            start[5] = set[i5];

                            // We have a selection of six starting numbers.
                            // See if the resulting score is better than
                            // we've seen so far.
                            currScore = score();
                            if (currScore > bestScore) {
                                bestScore = currScore;
                                for (int i = 0; i < 6; i++)
                                    ans[i] = start[i];
                            }
                        }
                    }
                }
            }
        }
    }

    // Output the best solution that was found.
    out << bestScore << '\n';
    out << ans[0] << ' ' << ans[1] << ' ' << ans[2] << ' '
        << ans[3] << ' ' << ans[4] << ' ' << ans[5] << '\n';
    return 0;
}
