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 mas[16];
int perm[16];
int matr[4][4];
int i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15;
int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15;
bool check() {
  int mas1[16];
  mas1[0] = a0;
  mas1[1] = a1;
  mas1[2] = a2;
  mas1[3] = a3;
  mas1[4] = a4;
  mas1[5] = a5;
  mas1[6] = a6;
  mas1[7] = a7;
  mas1[8] = a8;
  mas1[9] = a9;
  mas1[10] = a10;
  mas1[11] = a11;
  mas1[12] = a12;
  mas1[13] = a13;
  mas1[14] = a14;
  mas1[15] = a15;
  int mas2[16];
  for (int i = 0; i < 16; i++) {
    mas2[i] = mas1[i];
  }
  sort(mas2, mas2 + 16);
  bool f = true;
  for (int i = 0; i < 16; i++) {
    if (mas2[i] != mas[i]) return false;
  }
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) cout << mas1[i * 4 + j] << " ";
    cout << endl;
  }
  return true;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> mas[i];
    perm[i] = i;
  }
  if (n <= 3) {
    if (n == 1) {
      cout << mas[0] << endl;
      cout << mas[0] << endl;
      return 0;
    }
    while (next_permutation(perm, perm + n * n)) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          matr[i][j] = mas[perm[i * n + j]];
        }
      }
      int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
      for (int i = 0; i < n; i++) {
        a += matr[0][i];
        b += matr[min(1, n - 1)][i];
        c += matr[min(2, n - 1)][i];
        d += matr[i][0];
        e += matr[i][min(1, n - 1)];
        f += matr[i][min(2, n - 1)];
        g += matr[i][i];
        h += matr[i][n - 1 - i];
      }
      if (a == b && a == c && a == d && a == e && a == f && a == g && a == h) {
        cout << a << endl;
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            cout << matr[i][j] << " ";
          }
          cout << endl;
        }
        return 0;
      }
    }
  } else {
    int sumall = 0;
    for (int i = 0; i < 16; i++) {
      sumall += mas[i];
    }
    sort(mas, mas + 16);
    int sum = sumall / 4;
    cout << sum << endl;
    for (i0 = 0; i0 < 16; i0++) {
      for (i1 = 0; i1 < 16; i1++) {
        if (i0 == i1) continue;
        for (i2 = 0; i2 < 16; i2++) {
          if (i0 == i2 || i1 == i2) continue;
          a0 = mas[i0];
          a1 = mas[i1];
          a2 = mas[i2];
          a3 = sum - a0 - a1 - a2;
          bool isa3 = false;
          for (int i = 0; i < 16; i++) {
            if (i == i0 || i == i1 || i == i2) continue;
            if (mas[i] == a3) {
              isa3 = true;
              i3 = i;
              break;
            }
          }
          if (!isa3) continue;
          for (i4 = 0; i4 < 16; i4++) {
            if (i0 == i4 || i1 == i4 || i2 == i4 || i3 == i4) continue;
            for (i5 = 0; i5 < 16; i5++) {
              if (i0 == i5 || i1 == i5 || i2 == i5 || i3 == i5 || i4 == i5)
                continue;
              for (i6 = 0; i6 < 16; i6++) {
                if (i0 == i6 || i1 == i6 || i2 == i6 || i3 == i6 || i4 == i6 ||
                    i5 == i6)
                  continue;
                a4 = mas[i4];
                a5 = mas[i5];
                a6 = mas[i6];
                a7 = sum - a4 - a5 - a6;
                if (a0 == 16 && a1 == 3 && a2 == 2 && a3 == 13 && a4 == 5 &&
                    a5 == 10 && a6 == 11 && a7 == 8) {
                  cout << "";
                }
                bool isa7 = false;
                for (int i = 0; i < 16; i++) {
                  if (i0 == i || i1 == i || i2 == i || i3 == i || i4 == i ||
                      i5 == i || i6 == i)
                    continue;
                  if (mas[i] == a7) {
                    isa7 = true;
                    i7 = i;
                    break;
                  }
                }
                if (!isa7) continue;
                for (i9 = 0; i9 < 16; i9++) {
                  if (i0 == i9 || i1 == i9 || i2 == i9 || i3 == i9 ||
                      i4 == i9 || i5 == i9 || i6 == i9 || i7 == i9)
                    continue;
                  for (i10 = 0; i10 < 16; i10++) {
                    if (i0 == i10 || i1 == i10 || i2 == i10 || i3 == i10 ||
                        i4 == i10 || i5 == i10 || i6 == i10 || i7 == i10 ||
                        i9 == i10)
                      continue;
                    a9 = mas[i9];
                    a10 = mas[i10];
                    a13 = sum - a1 - a5 - a9;
                    a14 = sum - a2 - a6 - a10;
                    a12 = sum - a3 - a6 - a9;
                    a15 = sum - a0 - a5 - a10;
                    a8 = sum - a0 - a4 - a12;
                    a11 = sum - a3 - a7 - a15;
                    if (a11 != sum - a8 - a9 - a10) continue;
                    if (check()) {
                      return 0;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return 0;
}