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 MAX_N = 1e5 + 10;
const int MAX1 = 1e3 + 10;
const int MAX2 = 1e2 + 10;
const int INF = 1e9 + 10;
const long long mod = 1e18;
inline int bpow(int t, int n) {
  int ans = 1;
  while (n > 0) {
    if (n & 1) ans = (ans * t) % mod;
    t = (t * t) % mod, n >>= 1;
  }
  return ans;
}
int a[9];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> a[i];
  if (n == 1)
    cout << a[0] << "\n" << a[0];
  else if (n == 2) {
    cout << 2 * a[0] << "\n";
    cout << a[0] << " " << a[0] << endl;
    cout << a[0] << " " << a[0] << endl;
  } else if (n == 3) {
    vector<int> vec;
    for (int i = 0; i < n * n; i++) vec.push_back(i);
    do {
      int t[] = {
          a[vec[0]] + a[vec[1]] + a[vec[2]], a[vec[3]] + a[vec[4]] + a[vec[5]],
          a[vec[6]] + a[vec[7]] + a[vec[8]], a[vec[0]] + a[vec[3]] + a[vec[6]],
          a[vec[1]] + a[vec[4]] + a[vec[7]], a[vec[2]] + a[vec[5]] + a[vec[8]],
          a[vec[0]] + a[vec[4]] + a[vec[8]], a[vec[2]] + a[vec[4]] + a[vec[6]]};
      bool ok = true;
      for (int i = 1; i < 8 && ok; i++)
        if (t[0] != t[i]) ok = false;
      if (ok) {
        cout << t[0];
        for (int i = 0; i < n * n; i++) {
          if (i % n == 0) cout << endl;
          cout << a[vec[i]] << " ";
        }
        return 0;
      }
    } while (next_permutation(vec.begin(), vec.end()));
  } else {
    int sum = 0;
    for (int i = 0; i < n * n; i++) sum += a[i];
    sum /= n;
    int h[16];
    bool mark[100] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    for (h[0] = 0; h[0] < n * n; h[0]++) {
      mark[h[0]] = true;
      for (h[5] = 0; h[5] < n * n; h[5]++) {
        if (!mark[h[5]]) {
          mark[h[5]] = true;
          for (h[10] = 0; h[10] < n * n; h[10]++) {
            if (!mark[h[10]]) {
              mark[h[10]] = true;
              int wh15 = sum - a[h[10]] - a[h[5]] - a[h[0]];
              for (h[15] = 0; h[15] < n * n; h[15]++) {
                if (a[h[15]] == wh15 && !mark[h[15]]) {
                  mark[h[15]] = true;
                  for (h[12] = 0; h[12] < n * n; h[12]++) {
                    if (!mark[h[12]]) {
                      mark[h[12]] = true;
                      for (h[9] = 0; h[9] < n * n; h[9]++) {
                        if (!mark[h[9]]) {
                          mark[h[9]] = true;
                          for (h[6] = 0; h[6] < n * n; h[6]++) {
                            if (!mark[h[6]]) {
                              mark[h[6]] = true;
                              int wh3 = sum - a[h[6]] - a[h[9]] - a[h[12]];
                              for (h[3] = 0; h[3] < n * n; h[3]++) {
                                if (a[h[3]] == wh3 && !mark[h[3]]) {
                                  mark[h[3]] = true;
                                  for (h[1] = 0; h[1] < n * n; h[1]++) {
                                    if (!mark[h[1]]) {
                                      mark[h[1]] = true;
                                      int wh13 =
                                          sum - a[h[1]] - a[h[5]] - a[h[9]];
                                      for (h[13] = 0; h[13] < n * n; h[13]++) {
                                        if (a[h[13]] == wh13 && !mark[h[13]]) {
                                          mark[h[13]] = true;
                                          int wh2 =
                                              sum - a[h[0]] - a[h[1]] - a[h[3]];
                                          for (h[2] = 0; h[2] < n * n; h[2]++) {
                                            if (a[h[2]] == wh2 && !mark[h[2]]) {
                                              mark[h[2]] = true;
                                              int wh14 = sum - a[h[12]] -
                                                         a[h[13]] - a[h[15]];
                                              for (h[14] = 0; h[14] < n * n;
                                                   h[14]++) {
                                                if (a[h[14]] == wh14 &&
                                                    !mark[h[14]]) {
                                                  mark[h[14]] = true;
                                                  for (h[8] = 0; h[8] < n * n;
                                                       h[8]++) {
                                                    if (!mark[h[8]]) {
                                                      mark[h[8]] = true;
                                                      int wh11 = sum - a[h[8]] -
                                                                 a[h[9]] -
                                                                 a[h[10]];
                                                      for (h[11] = 0;
                                                           h[11] < n * n;
                                                           h[11]++) {
                                                        if (a[h[11]] == wh11 &&
                                                            !mark[h[11]]) {
                                                          mark[h[11]] = true;
                                                          int wh7 = sum -
                                                                    a[h[11]] -
                                                                    a[h[15]] -
                                                                    a[h[3]];
                                                          for (h[7] = 0;
                                                               h[7] < n * n;
                                                               h[7]++) {
                                                            if (a[h[7]] ==
                                                                    wh7 &&
                                                                !mark[h[7]]) {
                                                              mark[h[7]] = true;
                                                              int wh4 =
                                                                  sum -
                                                                  a[h[0]] -
                                                                  a[h[8]] -
                                                                  a[h[12]];
                                                              for (h[4] = 0;
                                                                   h[4] < n * n;
                                                                   h[4]++) {
                                                                if (a[h[4]] ==
                                                                        wh4 &&
                                                                    !mark
                                                                        [h[4]]) {
                                                                  mark[h[4]] =
                                                                      true;
                                                                  cout << sum;
                                                                  for (int i =
                                                                           0;
                                                                       i <
                                                                       n * n;
                                                                       i++) {
                                                                    if (i % n ==
                                                                        0)
                                                                      cout
                                                                          << endl;
                                                                    cout
                                                                        << a[h[i]]
                                                                        << " ";
                                                                  }
                                                                  return 0;
                                                                }
                                                              }
                                                              mark[h[7]] =
                                                                  false;
                                                            }
                                                          }
                                                          mark[h[11]] = false;
                                                        }
                                                      }
                                                      mark[h[8]] = false;
                                                    }
                                                  }
                                                  mark[h[14]] = false;
                                                }
                                              }
                                              mark[h[2]] = false;
                                            }
                                          }
                                          mark[h[13]] = false;
                                          break;
                                        }
                                      }
                                      mark[h[1]] = false;
                                    }
                                  }
                                  mark[h[3]] = false;
                                  break;
                                }
                              }
                              mark[h[6]] = false;
                            }
                          }
                          mark[h[9]] = false;
                        }
                      }
                      mark[h[12]] = false;
                    }
                  }
                  mark[h[15]] = false;
                  break;
                }
              }
              mark[h[10]] = false;
            }
          }
          mark[h[5]] = false;
        }
      }
      mark[h[0]] = false;
    }
  }
}