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 n, a[20], s, per, ans[4][4], canPair[20][20], app[20];
vector<int> mask;
map<int, int> d, e;
pair<int, pair<int, int> > p;
void output() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cout << ans[i][j] << ' ';
    puts("");
  }
  exit(0);
}
int checkDiagonal() {
  int cur = 0;
  for (int i = 0; i < n; i++) cur += ans[i][i];
  if (cur != per) return 0;
  cur = 0;
  for (int i = 0; i < n; i++) cur += ans[i][n - 1 - i];
  return cur == per;
}
void go(int c1, int c2, int c3, int c4) {
  vector<int> used(n * n, 0);
  d = e;
  ans[0][0] = a[c1];
  ans[n - 1][n - 1] = a[c2];
  ans[0][n - 1] = a[c3];
  ans[n - 1][0] = a[c4];
  d[a[c1]]--;
  d[a[c2]]--;
  d[a[c3]]--;
  d[a[c4]]--;
  used[c1] = used[c2] = used[c3] = used[c4] = 1;
  if (n == 3) {
    int x = per - a[c1] - a[c3];
    if (!d.count(x) || !d[x]) return;
    d[x]--;
    ans[0][1] = x;
    x = per - a[c1] - a[c4];
    if (!d.count(x) || !d[x]) return;
    d[x]--;
    ans[1][0] = x;
    x = per - a[c2] - a[c3];
    if (!d.count(x) || !d[x]) return;
    d[x]--;
    ans[1][2] = x;
    x = per - a[c2] - a[c4];
    if (!d.count(x) || !d[x]) return;
    d[x]--;
    ans[2][1] = x;
    x = per - ans[0][1] - ans[2][1];
    if (!d.count(x) || !d[x]) return;
    d[x]--;
    ans[1][1] = x;
    if (checkDiagonal()) output();
  }
  if (n == 4) {
    vector<int> rest;
    for (int i = 0; i < 16; i++)
      if (!used[i] && app[i] >= 3) rest.push_back(i);
    int R = int(rest.size());
    for (int ib1 = 0; ib1 < R; ib1++)
      for (int ib2 = 0; ib2 < R; ib2++)
        if (ib1 != ib2) {
          int b1 = rest[ib1], b2 = rest[ib2];
          if (!canPair[b1][b2]) continue;
          ans[1][1] = a[b1];
          ans[1][2] = a[b2];
          d[a[b1]]--;
          d[a[b2]]--;
          int x = per - ans[0][0] - ans[1][1] - ans[3][3];
          ans[2][2] = x;
          int y = per - ans[3][0] - ans[1][2] - ans[0][3];
          ans[2][1] = y;
          if ((x != y && d[x] > 0 && d[y] > 0) || (x == y && d[x] > 1)) {
            d[x]--;
            d[y]--;
            vector<int> rest2;
            for (map<int, int>::iterator it = d.begin(); it != d.end(); it++)
              if (it->second > 0) rest2.push_back(it->first);
            for (int iq1 = 0; iq1 < int(rest2.size()); iq1++)
              for (int iq2 = 0; iq2 < int(rest2.size()); iq2++) {
                int q1 = rest2[iq1], q2 = rest2[iq2];
                if ((q1 == q2 && d[q1] > 1) ||
                    (q1 != q2 && d[q1] > 0 && d[q2] > 0)) {
                  d[q1]--;
                  d[q2]--;
                  ans[0][1] = q1;
                  ans[1][0] = q2;
                  int x = per - ans[0][0] - ans[0][1] - ans[0][3];
                  ans[0][2] = x;
                  if (!d[x]) {
                    d[q1]++;
                    d[q2]++;
                    continue;
                  }
                  d[x]--;
                  int y = per - ans[0][0] - ans[1][0] - ans[3][0];
                  ans[2][0] = y;
                  if (!d[y]) {
                    d[q1]++;
                    d[q2]++;
                    d[x]++;
                    continue;
                  }
                  d[y]--;
                  int z = per - ans[0][2] - ans[1][2] - ans[2][2];
                  ans[3][2] = z;
                  if (!d[z]) {
                    d[q1]++;
                    d[q2]++;
                    d[x]++;
                    d[y]++;
                    continue;
                  }
                  d[z]--;
                  int t = per - ans[2][0] - ans[2][1] - ans[2][2];
                  ans[2][3] = t;
                  if (!d[t]) {
                    d[q1]++;
                    d[q2]++;
                    d[x]++;
                    d[y]++;
                    d[z]++;
                    continue;
                  }
                  d[t]--;
                  int u = per - ans[3][0] - ans[3][2] - ans[3][3];
                  ans[3][1] = u;
                  if (!d[u]) {
                    d[q1]++;
                    d[q2]++;
                    d[x]++;
                    d[y]++;
                    d[z]++;
                    d[t]++;
                    continue;
                  }
                  d[u]--;
                  int v = per - ans[0][3] - ans[2][3] - ans[3][3];
                  ans[1][3] = v;
                  if (d[v] && checkDiagonal()) output();
                  d[q1]++;
                  d[q2]++;
                  d[x]++;
                  d[y]++;
                  d[z]++;
                  d[t]++;
                  d[u]++;
                }
              }
            d[x]++;
            d[y]++;
          }
          d[a[b1]]++;
          d[a[b2]]++;
        }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> a[i], s += a[i], e[a[i]]++;
  per = s / n;
  cout << per << endl;
  if (n == 1) {
    cout << a[0] << endl;
    return 0;
  }
  if (n == 2) {
    cout << a[0] << ' ' << a[1] << endl << a[2] << ' ' << a[3] << endl;
    return 0;
  }
  for (int i = 0; i < 1 << (n * n); i++) {
    int cur = 0;
    vector<int> bit;
    for (int j = 0; j < n * n; j++)
      if (i >> j & 1) cur += a[j], bit.push_back(j);
    if (cur == per && int(bit.size()) == n) {
      mask.push_back(i);
      for (int j = 0; j < int(bit.size()); j++) {
        app[bit[j]]++;
        for (int jj = j + 1; jj <= int(bit.size()) - 1; jj++)
          canPair[bit[j]][bit[jj]] = canPair[bit[jj]][bit[j]] = 1;
      }
    }
  }
  for (int c1 = 0; c1 <= n * n - 4; c1++)
    if (app[c1] >= 3)
      for (int c2 = c1 + 1; c2 <= n * n - 1; c2++)
        if (canPair[c1][c2] && app[c2] >= 3)
          for (int c3 = c1 + 1; c3 <= n * n - 1; c3++)
            if (c3 != c2 && canPair[c1][c3] && canPair[c2][c3] && app[c3] >= 3)
              for (int c4 = c3 + 1; c4 <= n * n - 1; c4++)
                if (c4 != c2 && canPair[c1][c4] && canPair[c2][c4] &&
                    canPair[c3][c4] && app[c4] >= 3)
                  go(c1, c2, c3, c4);
}