<?

/**
 * Stacking Numbers
 * Problem 3, AIC 2003
 * PHP 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.
 */

// Variables describing the current and best arrangements:
$start = array(); // The six numbers at the bottom of the pyramid
$ans = array();   // The six numbers that give the best possible final score
$bestScore = -1;  // The best possible final score

// Input and output files:
$in fopen("stackin.txt""r");
$out fopen("stackout.txt""w");

/**
 * 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].
 */
function score() {
    global 
$start$base;

    
$left = ($start[0] + ($start[1]) + ($start[2]) +
        (
$start[3]) + $start[4]) % $base;
    
$right = ($start[1] + ($start[2]) + ($start[3]) +
        (
$start[4]) + $start[5]) % $base;
    return 
$left $right;
}

/**
 * The main program.
 */

// Read the input data.
$base fgets($in);
$setSize fgets($in);
$setLine fgets($in);
$set explode(' '$setLine);

// 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.

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 (
$i 0$i 6$i++)
                                
$ans[$i] = $start[$i];
                        }
                    }
                }
            }
        }
    }
}

// Output the best solution that was found.
fprintf($out"%d\n"$bestScore);
fprintf($out"%d %d %d %d %d %d\n",
    
$ans[0], $ans[1], $ans[2],  $ans[3], $ans[4], $ans[5]);