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>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
int a, b, c, d, i, j, n, m, k;
void brute() {
  int mas[4][4];
  vector<int> num;
  memset((mas), 0, sizeof(mas));
  for (int _n((n * n) - 1), i(0); i <= _n; i++) {
    scanf("%d", &a);
    num.push_back(a);
  }
  sort((num).begin(), (num).end());
  do {
    a = 0;
    for (int _n((n)-1), i(0); i <= _n; i++) {
      for (int _n((n)-1), j(0); j <= _n; j++) {
        mas[i][j] = num[a++];
      }
    }
    int s = -1000000000;
    bool ok = 1;
    for (int _n((n)-1), i(0); i <= _n; i++) {
      int sum = 0;
      for (int _n((n)-1), j(0); j <= _n; j++) sum += mas[i][j];
      if (s == -1000000000)
        s = sum;
      else if (sum != s) {
        ok = 0;
        break;
      }
      sum = 0;
      for (int _n((n)-1), j(0); j <= _n; j++) sum += mas[j][i];
      if (sum != s) {
        ok = 0;
        break;
      }
    }
    int sum = 0;
    for (int _n((n)-1), i(0); i <= _n; i++) sum += mas[i][i];
    if (sum != s) ok = 0;
    sum = 0;
    for (int _n((n)-1), i(0); i <= _n; i++) sum += mas[n - i - 1][i];
    if (sum != s) ok = 0;
    if (ok == 1) {
      printf("%d\n", s);
      for (int _n((n)-1), i(0); i <= _n; i++) {
        for (int _n((n)-1), j(0); j <= _n; j++) {
          if (j) printf(" ");
          printf("%d", mas[i][j]);
        }
        printf("\n");
      }
      exit(0);
    }
  } while (next_permutation((num).begin(), (num).end()));
}
int mas[16];
int sum[2001], num[1 << 16], inv[2001];
vector<int> perm[2001][24], nperm[2001][24];
const int N = 1000003;
const int mod = 1000003;
unsigned long long key[N + 1];
int val[N + 1];
int nx[N + 1], last[mod], cc;
int rl[1024], r;
bool bad[17][17];
inline void add(unsigned long long k, int v) {
  int h = k % mod;
  key[cc] = k;
  val[cc] = v;
  nx[cc] = last[h];
  last[h] = cc++;
}
inline int fnd(unsigned long long k) {
  for (int w = last[k % mod]; w >= 0; w = nx[w]) {
    if (key[w] == k) return val[w];
  }
  return 0;
}
void die(unsigned long long rh, int n1, int n2, int cp1, int cp2, int sum) {
  int p2 = rh % 24;
  rh /= 24;
  int p1 = rh % 24;
  rh /= 24;
  int v2 = rh % k;
  rh /= k;
  int v1 = rh;
  printf("%d\n", sum);
  for (int _n((4) - 1), i(0); i <= _n; i++) {
    vector<int> cur;
    if (!i)
      cur = perm[n1][cp1];
    else if (i == 1)
      cur = perm[n2][cp2];
    else if (i == 2)
      cur = perm[v2][p2];
    else
      cur = perm[v1][p1];
    for (int _n((4) - 1), j(0); j <= _n; j++) {
      if (j) printf(" ");
      printf("%d", cur[j]);
    }
    printf("\n");
  }
  exit(0);
}
int main() {
  srand(25091992);
  memset(last, -1, sizeof(last));
  scanf("%d", &n);
  if (n <= 3) brute();
  for (int _n((n * n) - 1), i(0); i <= _n; i++) {
    scanf("%d", &mas[i]);
  }
  random_shuffle(mas, mas + n * n);
  for (int _n((1 << (n * n)) - 1), mask(0); mask <= _n; mask++) {
    c = 0;
    d = 0;
    for (int _n((n * n) - 1), i(0); i <= _n; i++)
      if (mask & 1 << i) {
        c += mas[i];
        ++d;
      }
    if (d != 4) continue;
    sum[k] = c;
    inv[k] = mask;
    num[mask] = ++k;
    vector<int> cur;
    for (int _n((n * n) - 1), i(0); i <= _n; i++)
      if (mask & 1 << i) cur.push_back(mas[i]);
    sort((cur).begin(), (cur).end());
    a = 0;
    do {
      perm[k - 1][a++] = cur;
      vector<int> np;
      for (int _n((n * n) - 1), i(0); i <= _n; i++)
        if (mask & 1 << i) np.push_back(i);
      do {
        bool ok = 1;
        for (int _n((4) - 1), j(0); j <= _n; j++)
          if (mas[np[j]] != cur[j]) {
            ok = 0;
            break;
          }
        if (ok) break;
      } while (next_permutation((np).begin(), (np).end()));
      nperm[k - 1][a - 1] = np;
    } while (next_permutation((cur).begin(), (cur).end()));
  }
  int ts = 0;
  for (int _n((n * n) - 1), i(0); i <= _n; i++) ts += mas[i];
  for (int _n((n * n) - 1), i(0); i <= _n; i++) {
    for (int _n((i)-1), j(0); j <= _n; j++) {
      rl[r++] = mas[i] + mas[j];
    }
  }
  sort(rl, rl + r);
  r = unique(rl, rl + r) - rl;
  for (int _n((n)-1), i(0); i <= _n; i++) {
    for (int _n((i)-1), j(0); j <= _n; j++) {
      if (!binary_search(rl, rl + r, ts / 4 - mas[i] - mas[j])) {
        bad[i][j] = bad[j][i] = 1;
      }
    }
  }
  for (int _n((k)-1), i(0); i <= _n; i++) {
    if (ts != sum[i] * 4) continue;
    for (int _n((k)-1), j(0); j <= _n; j++) {
      if (inv[i] & inv[j]) continue;
      if (sum[i] != sum[j]) continue;
      int s = sum[i];
      for (int _n((24) - 1), p1(0); p1 <= _n; p1++) {
        if ((int)((perm[i][p1]).size()) == 0) break;
        const vector<int> &cp1 = perm[i][p1];
        const vector<int> &np1 = nperm[i][p1];
        for (int _n((24) - 1), p2(0); p2 <= _n; p2++) {
          if (perm[j][p2].empty()) break;
          const vector<int> &cp2 = perm[j][p2];
          const vector<int> &np2 = nperm[j][p2];
          if (bad[np1[0]][np2[1]]) continue;
          if (bad[np1[3]][np2[2]]) continue;
          bool ok = 1;
          unsigned long long h = 1, rh = 1;
          for (int _n((4) - 1), z(0); z <= _n; z++) {
            if (bad[np1[z]][np2[z]]) {
              ok = 0;
              break;
            }
            h = h * 1234567891 + cp1[z] + cp2[z];
            rh = rh * 1234567891 + s - cp1[z] - cp2[z];
          }
          if (!ok) continue;
          h = h * 1234567891 + cp1[0] + cp2[1];
          rh = rh * 1234567891 + s - (cp1[3] + cp2[2]);
          h = h * 1234567891 + cp1[3] + cp2[2];
          rh = rh * 1234567891 + s - (cp1[0] + cp2[1]);
          h = h * 1234567891 * (inv[i] | inv[j]);
          rh = rh * 1234567891 * ((~(inv[i] | inv[j])) & ((1 << (n * n)) - 1));
          int t = fnd(rh);
          if (t) {
            die(t, i, j, p1, p2, s);
          }
          if (i >= j) continue;
          int val = k * 24 * 24 * i + 24 * 24 * j + 24 * p1 + p2;
          add(h, val);
        }
      }
    }
  }
}