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;
int A[20];
void Work1() { printf("%d\n%d\n", A[0], A[0]); }
void Work2() { printf("%d\n%d %d\n%d %d\n", A[0] * 2, A[0], A[0], A[0], A[0]); }
void Work3() {
  int i, i00, i01, i02, i11, i21, i20, i22, i10, i12;
  int sum = 0;
  for (i = 0; i < 9; ++i) {
    sum += A[i];
  }
  sum /= 3;
  for (i00 = 0; i00 < 9; ++i00) {
    swap(A[0], A[i00]);
    for (i01 = 1; i01 < 9; ++i01) {
      swap(A[1], A[i01]);
      for (i02 = 2; i02 < 9; ++i02) {
        if (A[0] + A[1] + A[i02] != sum) continue;
        swap(A[2], A[i02]);
        for (i11 = 3; i11 < 9; ++i11) {
          swap(A[3], A[i11]);
          for (i21 = 4; i21 < 9; ++i21) {
            if (A[1] + A[3] + A[i21] != sum) continue;
            swap(A[4], A[i21]);
            for (i20 = 5; i20 < 9; ++i20) {
              if (A[2] + A[3] + A[i20] != sum) continue;
              swap(A[5], A[i20]);
              for (i22 = 6; i22 < 9; ++i22) {
                if (A[0] + A[3] + A[i22] != sum) continue;
                if (A[5] + A[4] + A[i22] != sum) continue;
                swap(A[6], A[i22]);
                for (i10 = 7; i10 < 9; ++i10) {
                  if (A[0] + A[i10] + A[5] != sum) continue;
                  swap(A[7], A[i10]);
                  for (i12 = 8; i12 < 9; ++i12) {
                    if (A[7] + A[3] + A[i12] != sum) continue;
                    if (A[2] + A[i12] + A[6] != sum) continue;
                    printf("%d\n", sum);
                    printf("%d %d %d\n", A[0], A[1], A[2]);
                    printf("%d %d %d\n", A[7], A[3], A[8]);
                    printf("%d %d %d\n", A[5], A[4], A[6]);
                    return;
                  }
                  swap(A[7], A[i10]);
                  break;
                }
                swap(A[6], A[i22]);
                break;
              }
              swap(A[5], A[i20]);
              break;
            }
            swap(A[4], A[i21]);
            break;
          }
          swap(A[3], A[i11]);
        }
        swap(A[2], A[i02]);
        break;
      }
      swap(A[1], A[i01]);
    }
    swap(A[0], A[i00]);
  }
}
void Work4() {
  int j, i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15;
  int sum = 0;
  for (j = 0; j < 16; ++j) {
    sum += A[j];
  }
  sum /= 4;
  for (i0 = 0; i0 < 16; ++i0) {
    swap(A[0], A[i0]);
    for (i1 = 1; i1 < 16; ++i1) {
      swap(A[1], A[i1]);
      for (i2 = 2; i2 < 16; ++i2) {
        swap(A[2], A[i2]);
        for (i3 = 3; i3 < 16; ++i3) {
          if (A[0] + A[1] + A[2] + A[i3] != sum) continue;
          swap(A[3], A[i3]);
          for (i4 = 4; i4 < 16; ++i4) {
            swap(A[4], A[i4]);
            for (i5 = 5; i5 < 16; ++i5) {
              swap(A[5], A[i5]);
              for (i6 = 6; i6 < 16; ++i6) {
                if (A[0] + A[4] + A[5] + A[i6] != sum) continue;
                swap(A[6], A[i6]);
                for (i7 = 7; i7 < 16; ++i7) {
                  swap(A[7], A[i7]);
                  for (i8 = 8; i8 < 16; ++i8) {
                    if (A[2] + A[7] + A[5] + A[i8] != sum) continue;
                    swap(A[8], A[i8]);
                    for (i9 = 9; i9 < 16; ++i9) {
                      swap(A[9], A[i9]);
                      for (i10 = 10; i10 < 16; ++i10) {
                        if (A[1] + A[4] + A[9] + A[i10] != sum) continue;
                        swap(A[10], A[i10]);
                        for (i11 = 11; i11 < 16; ++i11) {
                          if (A[3] + A[7] + A[9] + A[i11] != sum) continue;
                          swap(A[11], A[i11]);
                          for (i12 = 12; i12 < 16; ++i12) {
                            swap(A[12], A[i12]);
                            for (i13 = 13; i13 < 16; ++i13) {
                              if (A[0] + A[12] + A[i13] + A[11] != sum)
                                continue;
                              swap(A[13], A[i13]);
                              for (i14 = 14; i14 < 16; ++i14) {
                                if (A[12] + A[4] + A[7] + A[14] != sum)
                                  continue;
                                swap(A[14], A[i14]);
                                for (i15 = 15; i15 < 16; ++i15) {
                                  if (A[3] + A[14] + A[i15] + A[6] != sum)
                                    continue;
                                  if (A[13] + A[9] + A[5] + A[i15] != sum)
                                    continue;
                                  printf("%d\n", sum);
                                  printf("%d %d %d %d\n", A[0], A[1], A[2],
                                         A[3]);
                                  printf("%d %d %d %d\n", A[12], A[4], A[7],
                                         A[14]);
                                  printf("%d %d %d %d\n", A[13], A[9], A[5],
                                         A[15]);
                                  printf("%d %d %d %d\n", A[11], A[10], A[8],
                                         A[6]);
                                  return;
                                }
                                swap(A[14], A[i14]);
                                break;
                              }
                              swap(A[13], A[i13]);
                              break;
                            }
                            swap(A[12], A[i12]);
                          }
                          swap(A[11], A[i11]);
                          break;
                        }
                        swap(A[10], A[i10]);
                        break;
                      }
                      swap(A[9], A[i9]);
                    }
                    swap(A[8], A[i8]);
                    break;
                  }
                  swap(A[7], A[i7]);
                }
                swap(A[6], A[i6]);
                break;
              }
              swap(A[5], A[i5]);
            }
            swap(A[4], A[i4]);
          }
          swap(A[3], A[i3]);
          break;
        }
        swap(A[2], A[i2]);
      }
      swap(A[1], A[i1]);
    }
    swap(A[0], A[i0]);
  }
}
int main() {
  int i;
  scanf("%d", &N);
  for (i = 0; i < N * N; ++i) {
    scanf("%d", &A[i]);
  }
  if (N == 1)
    Work1();
  else if (N == 2)
    Work2();
  else if (N == 3)
    Work3();
  else if (N == 4)
    Work4();
  return 0;
}