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;
const int inf = 1000000000;
int f[20];
int G[20], H[20];
bool z[20];
int A, B;
int main() {
  int n;
  cin >> n;
  int N = n * n;
  for (int i = 1; i <= N; ++i) cin >> f[i];
  sort(f + 1, f + N + 1);
  int s = 0;
  for (int i = 1; i <= N; ++i) s += f[i];
  s /= n;
  cout << s << endl;
  if (n == 4) {
    memset(z, true, sizeof(z));
    for (int i1 = 1; i1 <= N; ++i1)
      for (int i2 = 1; i2 <= N; ++i2)
        if (i2 != i1)
          for (int i3 = 1; i3 <= N; ++i3)
            if (i1 != i3 && i2 != i3) {
              int j;
              for (j = 1; j <= N; ++j)
                if (j != i1 && j != i2 && j != i3 && z[j] &&
                    f[j] == s - f[i1] - f[i2] - f[i3])
                  break;
              if (j == N + 1) continue;
              z[i1] = false;
              z[i2] = false;
              z[i3] = false;
              z[j] = false;
              A = f[j];
              for (int j1 = 1; j1 <= N; ++j1)
                if (z[j1])
                  for (int j2 = 1; j2 <= N; ++j2)
                    if (z[j2] && j2 != j1)
                      for (int j3 = 1; j3 <= N; ++j3)
                        if (z[j3] && j3 != j2 && j3 != j1) {
                          int j;
                          for (j = 1; j <= N; ++j)
                            if (j != j1 && j != j2 && j != j3 && z[j] &&
                                f[j] == s - f[j1] - f[j2] - f[j3])
                              break;
                          if (j == N + 1) continue;
                          z[j1] = false;
                          z[j2] = false;
                          z[j3] = false;
                          z[j] = false;
                          B = f[j];
                          for (int c = 1; c <= N; ++c)
                            if (z[c]) {
                              int v1 = -1, v2 = -1;
                              for (int v = 1; v <= N; ++v)
                                if (z[v])
                                  if (v != c && v1 == -1 &&
                                      f[v] == s - f[i2] - f[j2] - f[c])
                                    v1 = v;
                                  else if (v != c &&
                                           f[v] == s - A - f[j3] - f[c])
                                    v2 = v;
                              if (v1 == -1 || v2 == -1) continue;
                              int vv = -1;
                              for (int v = 1; v <= N; ++v)
                                if (v != v1 && v != v2 && v != c && z[v] &&
                                    f[v] == s - f[i1] - f[j1] - f[v2])
                                  vv = v;
                              if (vv == -1) continue;
                              z[c] = false;
                              z[v1] = false;
                              z[v2] = false;
                              z[vv] = false;
                              int gg = 0;
                              for (int v = 1; v <= N; ++v)
                                if (z[v]) G[++gg] = f[v];
                              int x = s - f[vv] - f[c], y = s - f[v1] - f[v2],
                                  zz = s - f[i3] - f[j3], t = s - A - B,
                                  p = s - f[i1] - f[j2];
                              int d = (p + y - zz);
                              if (d % 2 == 0) {
                                d >>= 1;
                                int a = p - d, b = x - a, c = zz - a;
                                int hh = 0;
                                H[++hh] = a;
                                H[++hh] = b;
                                H[++hh] = c;
                                H[++hh] = d;
                                sort(H + 1, H + hh + 1);
                                if (H[1] == G[1] && H[2] == G[2] &&
                                    H[3] == G[3] && H[4] == G[4]) {
                                  cout << f[i1] << " " << f[i2] << " " << f[i3]
                                       << " " << A << "\n"
                                       << f[j1] << " " << f[j2] << " " << f[j3]
                                       << " " << B << "\n"
                                       << f[vv] << " " << f[c] << " " << a
                                       << " " << b << "\n"
                                       << f[v2] << " " << f[v1] << " " << c
                                       << " " << d << "\n";
                                  return 0;
                                }
                              }
                              z[vv] = true;
                              z[v1] = z[v2] = true;
                              z[c] = true;
                            }
                          z[j] = true;
                          z[j1] = true;
                          z[j2] = true;
                          z[j3] = true;
                        }
              z[j] = true;
              z[i1] = true;
              z[i2] = true;
              z[i3] = true;
            }
    return 0;
  }
  if (n == 1) {
    cout << f[1] << endl;
    return 0;
  }
  if (n == 3) {
    memset(z, true, sizeof(z));
    for (int i1 = 1; i1 <= N; ++i1)
      for (int i2 = 1; i2 <= N; ++i2)
        if (i2 != i1)
          for (int i3 = 1; i3 <= N; ++i3)
            if (i3 != i2 && i3 != i1)
              for (int i4 = 1; i4 <= N; ++i4)
                if (i4 != i3 && i4 != i2 && i4 != i1) {
                  z[i1] = false;
                  z[i2] = false;
                  z[i3] = false;
                  z[i4] = false;
                  int gg = 0;
                  for (int i = 1; i <= N; ++i)
                    if (z[i]) {
                      G[++gg] = f[i];
                    }
                  int a, b, c, d;
                  int hh = 0;
                  H[++hh] = s - f[i1] - f[i2];
                  a = H[hh];
                  H[++hh] = s - f[i3] - f[i4];
                  b = H[hh];
                  H[++hh] = s - f[i1] - f[i4];
                  H[++hh] = s - f[i2] - f[i4];
                  c = H[hh];
                  H[++hh] = s - f[i1] - f[i3];
                  d = H[hh];
                  sort(H + 1, H + hh + 1);
                  sort(G + 1, G + gg + 1);
                  bool ok = true;
                  for (int i = 1; i <= gg; ++i)
                    if (G[i] != H[i]) ok = false;
                  if (ok && d + f[i4] + a == s && d + c == a + b &&
                      d + c == f[i1] + f[i4]) {
                    cout << f[i1] << " " << f[i2] << " " << s - f[i1] - f[i2]
                         << endl;
                    cout << f[i3] << " " << f[i4] << " " << s - f[i3] - f[i4]
                         << endl;
                    cout << s - f[i1] - f[i3] << " " << s - f[i2] - f[i4] << " "
                         << s - (s - f[i1] - f[i2]) - (s - f[i3] - f[i4])
                         << endl;
                    return 0;
                  }
                  z[i1] = true;
                  z[i2] = true;
                  z[i3] = true;
                  z[i4] = true;
                }
  }
  if (n == 2) {
    for (int i1 = 1; i1 <= N; ++i1)
      for (int i2 = 1; i2 <= N; ++i2)
        if (i2 != i1)
          for (int i3 = 1; i3 <= N; ++i3)
            if (i3 != i2 && i3 != i1)
              for (int i4 = 1; i4 <= N; ++i4)
                if (i4 != i3 && i4 != i2 && i4 != i1) {
                  if (f[i1] + f[i2] == s && f[i1] + f[i3] == s &&
                      f[i1] + f[i4] == s && f[i3] + f[i4] == s &&
                      f[i2] + f[i4] == s) {
                    cout << f[i1] << " " << f[i2] << endl;
                    cout << f[i3] << " " << f[i4] << endl;
                    return 0;
                  }
                }
  }
  return 0;
}