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;
long size = 10000111;
void dec_1() {
  long a;
  cin >> a;
  cout << a << "\n" << a << "\n";
}
void dec_2() {
  long a;
  cin >> a;
  cout << 2 * a << "\n" << a << " " << a << "\n" << a << " " << a << "\n";
}
inline bool f(vector<long> &mass, vector<bool> &used, long a) {
  for (long w = 0; w < 9; w++)
    if (mass[w] == a && used[w] == false) {
      used[w] = true;
      return true;
    }
  return false;
}
void dec_3() {
  vector<long> mass(9);
  vector<bool> used(9);
  for (long i = 0; i < 9; i++) cin >> mass[i];
  for (long i = 0; i < 9; i++) {
    long a1 = mass[i];
    for (long j = 0; j < 9; j++) {
      long a2 = mass[j];
      for (long k = 0; k < 9; k++) {
        long a3 = mass[k];
        for (long q = 0; q < 9; q++) {
          bool flag = true;
          long a5 = mass[q];
          used.assign(9, false);
          if (f(mass, used, a1) == false) flag = false;
          if (f(mass, used, a2) == false) flag = false;
          if (f(mass, used, a3) == false) flag = false;
          if (f(mass, used, a5) == false) flag = false;
          long sum = a1 + a2 + a3;
          long a7 = sum - a5 - a3;
          if (f(mass, used, a7) == false) flag = false;
          long a4 = sum - a1 - a7;
          if (f(mass, used, a4) == false) flag = false;
          long a8 = sum - a2 - a5;
          if (f(mass, used, a8) == false) flag = false;
          long a9 = sum - a1 - a5;
          if (f(mass, used, a9) == false) flag = false;
          long a6 = sum - a3 - a9;
          if (f(mass, used, a6) == false) flag = false;
          if (!(a1 + a2 + a3 == sum && a4 + a5 + a6 == sum &&
                a7 + a8 + a9 == sum && a1 + a4 + a7 == sum &&
                a2 + a5 + a8 == sum && a3 + a6 + a9 == sum &&
                a1 + a5 + a9 == sum && a3 + a5 + a7 == sum))
            flag = false;
          if (flag == true) {
            cout << sum << "\n";
            cout << a1 << " " << a2 << " " << a3 << "\n";
            cout << a4 << " " << a5 << " " << a6 << "\n";
            cout << a7 << " " << a8 << " " << a9 << "\n";
            return;
          }
        }
      }
    }
  }
}
void dec_4() {
  map<long, long> mass;
  long temp;
  vector<vector<long> > a(4, vector<long>(4, 0));
  for (long i = 0; i < 16; i++) {
    cin >> temp;
    mass[temp]++;
  }
  for (map<long, long>::iterator i = mass.begin(); i != mass.end(); i++) {
    (*i).second--;
    a[0][0] = i->first;
    for (map<long, long>::iterator j = mass.begin(); j != mass.end(); j++) {
      if (j->second <= 0) continue;
      (*j).second--;
      a[0][1] = j->first;
      for (map<long, long>::iterator k = mass.begin(); k != mass.end(); k++) {
        if (k->second <= 0) continue;
        (*k).second--;
        a[1][1] = k->first;
        for (map<long, long>::iterator l = mass.begin(); l != mass.end(); l++) {
          if (l->second <= 0) continue;
          (*l).second--;
          a[1][2] = l->first;
          for (map<long, long>::iterator p = mass.begin(); p != mass.end();
               p++) {
            if (p->second <= 0) continue;
            (*p).second--;
            a[2][2] = p->first;
            for (map<long, long>::iterator q = mass.begin(); q != mass.end();
                 q++) {
              if (q->second <= 0) continue;
              (*q).second--;
              a[2][3] = q->first;
              for (map<long, long>::iterator w = mass.begin(); w != mass.end();
                   w++) {
                if (w->second <= 0) continue;
                (*w).second--;
                a[3][3] = w->first;
                for (map<long, long>::iterator e = mass.begin();
                     e != mass.end(); e++) {
                  if (e->second <= 0) continue;
                  (*e).second--;
                  a[0][3] = e->first;
                  long long sum = a[0][0] + a[1][1] + a[2][2] + a[3][3];
                  a[0][2] = sum - a[0][0] - a[0][1] - a[0][3];
                  a[1][3] = sum - a[2][3] - a[3][3] - a[0][3];
                  a[3][2] = sum - a[2][2] - a[1][2] - a[0][2];
                  a[1][0] = sum - a[1][1] - a[1][2] - a[1][3];
                  if (mass.find(a[0][2]) != mass.end() &&
                      mass.find(a[1][3]) != mass.end() &&
                      mass.find(a[3][2]) != mass.end() &&
                      mass.find(a[1][0]) != mass.end()) {
                    mass[a[0][2]]--;
                    mass[a[1][3]]--;
                    mass[a[3][2]]--;
                    mass[a[1][0]]--;
                    for (map<long, long>::iterator r = mass.begin();
                         r != mass.end(); r++) {
                      if (r->second <= 0) continue;
                      r->second--;
                      a[3][0] = r->first;
                      a[2][0] = sum - a[0][0] - a[1][0] - a[3][0];
                      a[3][1] = sum - a[3][0] - a[3][2] - a[3][3];
                      a[2][1] = sum - a[0][1] - a[1][1] - a[3][1];
                      if (mass.find(a[2][0]) != mass.end() &&
                          mass.find(a[3][1]) != mass.end() &&
                          mass.find(a[2][1]) != mass.end()) {
                        mass[a[2][0]]--;
                        mass[a[3][1]]--;
                        mass[a[2][1]]--;
                        bool flag = true;
                        for (map<long, long>::iterator ee = mass.begin();
                             ee != mass.end(); ee++)
                          if (ee->second < 0) flag = false;
                        if (flag == true) {
                          for (long z = 0; z < 4; z++)
                            if (a[z][0] + a[z][1] + a[z][2] + a[z][3] != sum ||
                                a[0][z] + a[1][z] + a[2][z] + a[3][z] != sum)
                              flag = false;
                          if (a[0][0] + a[1][1] + a[2][2] + a[3][3] != sum)
                            flag = false;
                          if (a[3][0] + a[2][1] + a[1][2] + a[0][3] != sum)
                            flag = false;
                          if (flag == true) {
                            cout << sum << "\n";
                            for (long z = 0; z < 4; z++) {
                              for (long x = 0; x < 4; x++)
                                cout << a[z][x] << " ";
                              cout << "\n";
                            }
                            return;
                          }
                        }
                        mass[a[2][0]]++;
                        mass[a[3][1]]++;
                        mass[a[2][1]]++;
                      }
                      r->second++;
                    }
                    mass[a[0][2]]++;
                    mass[a[1][3]]++;
                    mass[a[3][2]]++;
                    mass[a[1][0]]++;
                  }
                  (*e).second++;
                }
                (*w).second++;
              }
              (*q).second++;
            }
            (*p).second++;
          }
          (*l).second++;
        }
        (*k).second++;
      }
      (*j).second++;
    }
    (*i).second++;
  }
}
int main() {
  long n;
  cin >> n;
  if (n == 1) dec_1();
  if (n == 2) dec_2();
  if (n == 3) dec_3();
  if (n == 4) dec_4();
}