Problem: The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.

The magic square is a matrix of size n × n. The elements of this matrix are integers. The sum of numbers in each row of the matrix is equal to some number s. The sum of numbers in each column of the matrix is also equal to s. In addition, the sum of the elements on the main diagonal is equal to s and the sum of elements on the secondary diagonal is equal to s. Examples of magic squares are given in the following figure:

<image> Magic squares 

You are given a set of n2 integers ai. It is required to place these numbers into a square matrix of size n × n so that they form a magic square. Note that each number must occur in the matrix exactly the same number of times as it occurs in the original set.

It is guaranteed that a solution exists!

Input

The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 ≤ ai ≤ 108), separated by single spaces.

The input limitations for getting 20 points are: 

  * 1 ≤ n ≤ 3



The input limitations for getting 50 points are: 

  * 1 ≤ n ≤ 4
  * It is guaranteed that there are no more than 9 distinct numbers among ai. 



The input limitations for getting 100 points are: 

  * 1 ≤ n ≤ 4

Output

The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed to print any of them.

Examples

Input

3
1 2 3 4 5 6 7 8 9


Output

15
2 7 6
9 5 1
4 3 8


Input

3
1 0 -1 0 2 -1 -2 0 1


Output

0
1 0 -1
-2 0 2
1 0 -1


Input

2
5 5 5 5


Output

10
5 5
5 5

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int a[22], s1[3], s2[3], s3, s4, b[22], ss = 0;
int n, nn, c[16], sc[16];
int main() {
  cin >> n;
  nn = n * n;
  for (int i = 0; i < nn; i++) {
    cin >> a[i];
    ss += a[i];
  }
  int s = ss / 4;
  if (n <= 3) {
    sort(a, a + nn);
    do {
      memset(s1, 0, sizeof(s1));
      memset(s2, 0, sizeof(s2));
      s3 = s4 = 0;
      for (int i = 0; i < nn; i++) {
        s1[i / n] += a[i];
        s2[i % n] += a[i];
      }
      for (int i = 0; i < nn; i += n + 1) s3 += a[i];
      for (int i = nn - n, j = 0; j < n; i -= n - 1, j++) s4 += a[i];
      bool good = s3 == s4;
      for (int i = 1; i < n; i++)
        if (s1[i] != s1[0]) good = false;
      for (int i = 1; i < n; i++)
        if (s2[i] != s2[0]) good = false;
      good &= s3 == s1[0] && s3 == s2[0];
      if (good) {
        cout << s3 << endl;
        for (int i = 0; i < nn; i++) {
          if (i % n == n - 1) {
            cout << a[i] << endl;
          } else
            cout << a[i] << " ";
        }
        return 0;
      }
    } while (next_permutation(a, a + nn));
  } else {
    int cn = 0;
    sort(a, a + nn);
    for (int i = 0; i < nn; i++)
      if (i == 0 || a[i] != a[i - 1]) {
        b[cn] = a[i];
        c[cn++] = 1;
      } else
        c[cn - 1]++;
    for (int a11 = 0; a11 < cn; a11++)
      if (c[a11]) {
        c[a11]--;
        for (int a12 = 0; a12 < cn; a12++)
          if (c[a12]) {
            c[a12]--;
            for (int a13 = 0; a13 < cn; a13++)
              if (c[a13]) {
                c[a13]--;
                int b14 = s - b[a11] - b[a12] - b[a13];
                int j0 = 0;
                for (; j0 < cn; j0++)
                  if (b[j0] == b14) break;
                if (j0 == cn || c[j0] == 0) {
                  c[a13]++;
                  continue;
                }
                c[j0]--;
                for (int a21 = 0; a21 < cn; a21++)
                  if (c[a21]) {
                    c[a21]--;
                    for (int a22 = 0; a22 < cn; a22++)
                      if (c[a22]) {
                        c[a22]--;
                        for (int a23 = 0; a23 < cn; a23++)
                          if (c[a23]) {
                            c[a23]--;
                            int b24 = s - b[a21] - b[a23] - b[a22];
                            int j1 = 0;
                            for (; j1 < cn; j1++)
                              if (b[j1] == b24) break;
                            if (j1 == cn || c[j1] == 0) {
                              c[a23]++;
                              continue;
                            }
                            c[j1]--;
                            for (int a32 = 0; a32 < cn; a32++)
                              if (c[a32]) {
                                c[a32]--;
                                int b42 = s - b[a12] - b[a22] - b[a32];
                                int j2 = 0;
                                for (; j2 < cn; j2++)
                                  if (b[j2] == b42) break;
                                if (j2 == cn || c[j2] == 0) {
                                  c[a32]++;
                                  continue;
                                }
                                c[j2]--;
                                for (int a33 = 0; a33 < cn; a33++)
                                  if (c[a33]) {
                                    c[a33]--;
                                    int b43 = s - b[a13] - b[a23] - b[a33];
                                    int b44 = s - b[a11] - b[a22] - b[a33];
                                    int b41 = s - b42 - b43 - b44;
                                    if (b41 != s - b14 - b[a23] - b[a32]) {
                                      c[a33]++;
                                      continue;
                                    }
                                    int b31 = s - b[a11] - b[a21] - b41;
                                    int b34 = s - b[a32] - b31 - b[a33];
                                    memcpy(sc, c, sizeof(c));
                                    int j3;
                                    for (j3 = 0; j3 < cn; j3++)
                                      if (b[j3] == b43 && c[j3]) {
                                        c[j3]--;
                                        break;
                                      }
                                    if (j3 < cn) {
                                      for (j3 = 0; j3 < cn; j3++)
                                        if (b[j3] == b44 && c[j3]) {
                                          c[j3]--;
                                          break;
                                        }
                                      if (j3 < cn) {
                                        for (j3 = 0; j3 < cn; j3++)
                                          if (b[j3] == b41 && c[j3]) {
                                            c[j3]--;
                                            break;
                                          }
                                        if (j3 < cn) {
                                          for (j3 = 0; j3 < cn; j3++)
                                            if (b[j3] == b31 && c[j3]) {
                                              c[j3]--;
                                              break;
                                            }
                                          if (j3 < cn) {
                                            for (j3 = 0; j3 < cn; j3++)
                                              if (b[j3] == b34 && c[j3]) {
                                                c[j3]--;
                                                break;
                                              }
                                            if (j3 < cn) {
                                              cout << s << endl;
                                              cout << b[a11] << " " << b[a12]
                                                   << " " << b[a13] << " "
                                                   << b14 << endl;
                                              cout << b[a21] << " " << b[a22]
                                                   << " " << b[a23] << " "
                                                   << b24 << endl;
                                              cout << b31 << " " << b[a32]
                                                   << " " << b[a33] << " "
                                                   << b34 << endl;
                                              cout << b41 << " " << b42 << " "
                                                   << b43 << " " << b44 << endl;
                                              return 0;
                                            }
                                          }
                                        }
                                      }
                                    }
                                    memcpy(c, sc, sizeof(c));
                                    c[a33]++;
                                  }
                                c[j2]++;
                                c[a32]++;
                              }
                            c[j1]++;
                            c[a23]++;
                          }
                        c[a22]++;
                      }
                    c[a21]++;
                  }
                c[j0]++;
                c[a13]++;
              }
            c[a12]++;
          }
        c[a11]++;
      }
  }
  return 0;
}