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:   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!

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

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Codeforces
{
#if _ONLINE_JUDGE_
	[OnlineJudge.Task("Codeforces 178D1")]
#endif
	class Task178D1
	{
		private InputTokenizer input = new InputTokenizer();
		private TextWriter output = Console.Out;

		int[,] tmpl;
		long[] partialSum;
		long s;
		long[] a;
		long[,] matrix;
		int n;

		private void Solve()
		{
			input.Line().Read(out n)
					 .Line().Read(n * n, out a);

			switch (n) {
				case 1:
					tmpl = new int[,] {
						{ 0, 0, g(new int[] { 0 }) },
					};
					break;

				case 2:
					tmpl = new int[,] {
						{ 0, 0, g(new int[] { }) },
						{ 0, 1, g(new int[] { 0 }) },
						{ 1, 0, g(new int[] { 2, 8 }) },
						{ 1, 1, g(new int[] { 1, 3, 5 }) }
					};
					break;

				case 3:
					tmpl = new int[,] {
						{ 0, 0, g(new int[] { }) },
						{ 0, 1, g(new int[] { }) },
						{ 0, 2, g(new int[] { 0 }) },
						{ 1, 0, g(new int[] { }) },
						{ 2, 0, g(new int[] { 3 }) },
						{ 1, 1, g(new int[] { 13 }) },
						{ 1, 2, g(new int[] { 1 }) },
						{ 2, 1, g(new int[] { 4 }) },
						{ 2, 2, g(new int[] { 2, 5, 8 }) }
					};
					break;

				case 4:
					tmpl = new int[,] {
						{ 0, 0, g(new int[] { }) },
						{ 0, 1, g(new int[] { }) },
						{ 0, 2, g(new int[] { }) },
						{ 0, 3, g(new int[] { 0 }) },

						{ 1, 0, g(new int[] { }) },
						{ 2, 0, g(new int[] { }) },
						{ 3, 0, g(new int[] { 4 }) },

						{ 1, 1, g(new int[] { }) },
						{ 1, 2, g(new int[] { }) },
						{ 2, 1, g(new int[] { 18 }) },

						{ 1, 3, g(new int[] { 1 }) },

						{ 3, 1, g(new int[] { 5 }) },

						{ 2, 2, g(new int[] { }) },
						{ 2, 3, g(new int[] { 2 }) },
						{ 3, 2, g(new int[] { 6 }) },
						{ 3, 3, g(new int[] { 3, 7, 11 }) }
					};
					break;
			}

			Array.Sort(a);

			long sum = a.Aggregate(0L, (x, y) => x + y);
			s = sum / n;
			partialSum = new long[30];

			matrix = new long[n, n];
			f(0);

			output.WriteLine(s);
			for (int i = 0; i < n; ++i) {
				output.Write(matrix[i, 0]);
				for (int j = 1; j < n; ++j)
					output.Write(" {0}", matrix[i, j]);
				output.WriteLine();
			}
		}

		private int g(int[] p)
		{
			int x = 0;
			foreach (var i in p) x |= 1 << i;
			return x;
		}

		private bool f(int i)
		{
			if (n * n == i) return true;

			for (int t = i; t < n * n; ++t)
				if (t == i || a[t] != a[t - 1]) {
					long h = a[i];
					a[i] = a[t];
					a[t] = h;

					int y = tmpl[i, 0], x = tmpl[i, 1], test = tmpl[i, 2];
					matrix[y, x] = a[i];
					partialSum[y] += a[i]; // |
					partialSum[x + n] += a[i]; // -
					partialSum[x - y + (n - 1) + n * 2] += a[i]; // \
					partialSum[x + y + n * 4 - 1] += a[i]; // /

					bool b = true;

					for (int z = 0; b && test != 0; ++z, test >>= 1)
						if ((test & 1) != 0 && partialSum[z] != s)
							b = false;

					if (!b) {
						partialSum[y] -= a[i]; // |
						partialSum[x + n] -= a[i]; // -
						partialSum[x - y + (n - 1) + n * 2] -= a[i]; // \
						partialSum[x + y + n * 4 - 1] -= a[i]; // /

						a[t] = a[i];
						a[i] = h;

						continue;
					}

					if (f(i + 1)) return true;

					partialSum[y] -= a[i]; // |
					partialSum[x + n] -= a[i]; // -
					partialSum[x - y + (n - 1) + n * 2] -= a[i]; // \
					partialSum[x + y + n * 4 - 1] -= a[i]; // /

					a[t] = a[i];
					a[i] = h;
				}

			return false;
		}

		public static void Main()
		{
			var obj = new Task178D1();
			obj.Solve();
		}

		#region
		private class InputTokenizer
		{
			private List<string> _tokens = new List<string>();
			private int _offset = 0;

			public InputTokenizer()
			{
			}

			public InputTokenizer String(String s)
			{
				_tokens.AddRange(s.Split(new char[] { ' ', '\n', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries));
				return this;
			}

			public InputTokenizer Line()
			{
				return String(Console.In.ReadLine());
			}

			public InputTokenizer Read(out int v)
			{
				v = int.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out int[] v)
			{
				v = new int[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out long v)
			{
				v = long.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out long[] v)
			{
				v = new long[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out ulong v)
			{
				v = ulong.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out ulong[] v)
			{
				v = new ulong[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out double v)
			{
				v = double.Parse(_tokens[_offset++].Replace('.', ','));
				return this;
			}

			public InputTokenizer Read(int n, out double[] v)
			{
				v = new double[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out string v)
			{
				v = _tokens[_offset++];
				return this;
			}

			public InputTokenizer Read(int n, out string[] v)
			{
				v = new string[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}
		}
		#endregion
	}
}

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Codeforces
{
    class D
    {
        private static ThreadStart s_threadStart = new D().Go;
        private static bool s_time = false;

        bool Solve3(bool[] used, long[] dig, long sum, long[,] tab, int level)
        {
            if (level == 9)
            {
                if (tab[0, 0] + tab[0, 1] + tab[0, 2] != sum) return false;
                if (tab[1, 0] + tab[1, 1] + tab[1, 2] != sum) return false;
                if (tab[2, 0] + tab[2, 1] + tab[2, 2] != sum) return false;
                if (tab[0, 0] + tab[1, 0] + tab[2, 0] != sum) return false;
                if (tab[0, 1] + tab[1, 1] + tab[2, 1] != sum) return false;
                if (tab[0, 2] + tab[1, 2] + tab[2, 2] != sum) return false;
                if (tab[0, 0] + tab[1, 1] + tab[2, 2] != sum) return false;
                if (tab[0, 2] + tab[1, 1] + tab[2, 0] != sum) return false;

                Wl(tab[0, 0] + " " + tab[0, 1] + " " + tab[0, 2]);
                Wl(tab[1, 0] + " " + tab[1, 1] + " " + tab[1, 2]);
                Wl(tab[2, 0] + " " + tab[2, 1] + " " + tab[2, 2]);
                return true;
            }
            else
            {
                for (int i = 0; i < 9; i++)
                {
                    if (!used[i])
                    {
                        used[i] = true;
                        tab[level / 3, level % 3] = dig[i];
                        if (Solve3(used, dig, sum, tab, level + 1))
                            return true;
                        used[i] = false;
                    }
                }
            }
            return false;
        }

        private void Go()
        {
            int n = GetInt();
            long[] dig = new long[n * n];
            long sum = 0;
            for (int i = 0; i < n*n; i++)
            {
                sum += (dig[i] = GetInt());
            }
            sum /= n;
            Wl(sum);
            if (n == 1)
            {
                Wl(dig[0]);
            }
            else if (n == 2)
            {
                Wl(dig[0] + " " + (sum - dig[0]));
                Wl((sum - dig[0]) + " " + dig[0]);
            }
            else if (n == 3)
            {
                bool[] used = new bool[9];
                long[,] tab = new long[3, 3];
                Solve3(used, dig, sum, tab, 0);
            }

        }

        #region Template

        public static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            Thread main = new Thread(new ThreadStart(s_threadStart), 512 * 1024 * 1024);
            timer.Start();
            main.Start();
            main.Join();
            timer.Stop();
            if (s_time)
                Wl(timer.ElapsedMilliseconds);
        }

        private static IEnumerator<string> ioEnum;
        private static string GetString()
        {
            while (ioEnum == null || !ioEnum.MoveNext())
            {
                ioEnum = Console.ReadLine().Split().AsEnumerable().GetEnumerator();
            }

            return ioEnum.Current;
        }

        private static int GetInt()
        {
            return int.Parse(GetString());
        }

        private static long GetLong()
        {
            return long.Parse(GetString());
        }

        private static double GetDouble()
        {
            return double.Parse(GetString());
        }

        private static List<int> GetIntArr(int n)
        {
            List<int> ret = new List<int>(n);
            for (int i = 0; i < n; i++)
            {
                ret.Add(GetInt());
            }
            return ret;
        }

        private static void Wl<T>(T o)
        {
            if (o is double)
            {
                Wld((o as double?).Value, "");
            }
            else if (o is float)
            {
                Wld((o as float?).Value, "");
            }
            else
                Console.WriteLine(o.ToString());
        }

        private static void Wl<T>(IEnumerable<T> enumerable)
        {
            Wl(string.Join(" ", enumerable.Select(e => e.ToString()).ToArray()));
        }

        private static void Wld(double d, string format)
        {
            Wl(d.ToString(format, CultureInfo.InvariantCulture));
        }

        public struct Tuple<T, K> : IComparable<Tuple<T, K>>
            where T : IComparable<T>
            where K : IComparable<K>
        {
            public T Item1;
            public K Item2;

            public Tuple(T t, K k)
            {
                Item1 = t;
                Item2 = k;
            }

            public override bool Equals(object obj)
            {
                var o = (Tuple<T, K>)obj;
                return o.Item1.Equals(Item1) && o.Item2.Equals(Item2);
            }

            public override int GetHashCode()
            {
                return Item1.GetHashCode() ^ Item2.GetHashCode();
            }

            public override string ToString()
            {
                return "(" + Item1 + " " + Item2 + ")";
            }

            #region IComparable<Tuple<T,K>> Members

            public int CompareTo(Tuple<T, K> other)
            {
                int ret = Item1.CompareTo(other.Item1);
                if (ret != 0) return ret;
                return Item2.CompareTo(other.Item2);
            }

            #endregion
        }

        #endregion
    }
}

using System;
using System.IO;
using System.Linq;
using System.Text;

namespace Magic_Squares
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            Solve(args);
            writer.Flush();
        }

        private static void Solve(string[] args)
        {
            int n = Next();

            var nn = new int[n*n];
            var indx = new int[n*n];
            for (int i = 0; i < nn.Length; i++)
            {
                nn[i] = Next();
                indx[i] = i;
            }

            if (n < 3)
            {
                writer.WriteLine(n*nn[0]);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write(nn[0]);
                        writer.Write(' ');
                    }
                    writer.WriteLine();
                }
                return;
            }

            int sum = nn.Sum()/3;

            do
            {
                bool ok = true;

                ok &= nn[indx[0]] + nn[indx[4]] + nn[indx[8]] == sum;
                ok &= nn[indx[2]] + nn[indx[4]] + nn[indx[6]] == sum;

                ok &= nn[indx[0]] + nn[indx[1]] + nn[indx[2]] == sum;
                ok &= nn[indx[3]] + nn[indx[4]] + nn[indx[5]] == sum;
                ok &= nn[indx[6]] + nn[indx[7]] + nn[indx[8]] == sum;

                ok &= nn[indx[0]] + nn[indx[3]] + nn[indx[6]] == sum;
                ok &= nn[indx[1]] + nn[indx[4]] + nn[indx[7]] == sum;
                ok &= nn[indx[2]] + nn[indx[5]] + nn[indx[8]] == sum;

                if (ok)
                {
                    writer.WriteLine(sum);

                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            writer.Write(nn[indx[i*3 + j]]);
                            writer.Write(' ');
                        }
                        writer.WriteLine();
                    }
                    break;
                }
            } while (NextPermutation(indx));
        }

        private static bool NextPermutation(int[] nn)
        {
            if (nn.Length <= 1) return false;
            int i = nn.Length - 1;
            int last = nn.Length;

            while (true)
            {
                int i1 = i;
                if (nn[--i] < nn[i1])
                {
                    int i2 = last;
                    while (!(nn[i] < nn[--i2]))
                    {
                        ;
                    }
                    int c = nn[i];
                    nn[i] = nn[i2];
                    nn[i2] = c;

                    Reverse(nn, i1, last);
                    return true;
                }
                if (i == 0)
                {
                    Reverse(nn, 0, last);
                    return false;
                }
            }
        }

        private static void Reverse(int[] nn, int first, int last)
        {
            while ((first != last) && (first != --last))
            {
                int c = nn[first];
                nn[first] = nn[last];
                nn[last] = c;
                first++;
            }
        }

        private static int Next()
        {
            int c;
            int m = 1;
            do
            {
                c = reader.Read();
                if (c == '-')
                    m = -1;
            } while (c < '0' || c > '9');
            int res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return m*res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Codeforces
{
#if _ONLINE_JUDGE_
	[OnlineJudge.Task("Codeforces 178D1")]
#endif
	class Task178D1
	{
		private InputTokenizer input = new InputTokenizer();
		private TextWriter output = Console.Out;

		int[,] tmpl;
		long[] partialSum;
		long s;
		long[] a;
		long[,] matrix;
		int n;

		private void Solve()
		{
			input.Line().Read(out n)
					 .Line().Read(n * n, out a);

			switch (n) {
				case 1:
					tmpl = new int[,] {
						{ 0, 0, g(new int[] { 0 }) },
					};
					break;

				case 2:
					tmpl = new int[,] {
						{ 0, 0, g(new int[] { }) },
						{ 0, 1, g(new int[] { 0 }) },
						{ 1, 0, g(new int[] { 2, 8 }) },
						{ 1, 1, g(new int[] { 1, 3, 5 }) }
					};
					break;

				case 3:
					tmpl = new int[,] {
						{ 0, 0, g(new int[] { }) },
						{ 0, 1, g(new int[] { }) },
						{ 0, 2, g(new int[] { 0 }) },
						{ 1, 0, g(new int[] { }) },
						{ 2, 0, g(new int[] { 3 }) },
						{ 1, 1, g(new int[] { 13 }) },
						{ 1, 2, g(new int[] { 1 }) },
						{ 2, 1, g(new int[] { 4 }) },
						{ 2, 2, g(new int[] { 2, 5, 8 }) }
					};
					break;
			}

			Array.Sort(a);

			long sum = a.Aggregate(0L, (x, y) => x + y);
			s = sum / n;
			partialSum = new long[30];

			matrix = new long[n, n];
			f(0);

			output.WriteLine(s);
			for (int i = 0; i < n; ++i) {
				output.Write(matrix[i, 0]);
				for (int j = 1; j < n; ++j)
					output.Write(" {0}", matrix[i, j]);
				output.WriteLine();
			}
		}

		private int g(int[] p)
		{
			int x = 0;
			foreach (var i in p) x |= 1 << i;
			return x;
		}

		private bool f(int i)
		{
			if (n * n == i) return true;

			for (int t = i; t < n * n; ++t)
				if (t == i || a[t] != a[t - 1]) {
					long h = a[i];
					a[i] = a[t];
					a[t] = h;

					int y = tmpl[i, 0], x = tmpl[i, 1], test = tmpl[i, 2];
					matrix[y, x] = a[i];
					partialSum[y] += a[i]; // |
					partialSum[x + n] += a[i]; // -
					partialSum[x - y + (n - 1) + n * 2] += a[i]; // \
					partialSum[x + y + n * 4 - 1] += a[i]; // /

					bool b = true;

					for (int z = 0; b && test != 0; ++z, test >>= 1)
						if ((test & 1) != 0 && partialSum[z] != s)
							b = false;

					if (!b) {
						partialSum[y] -= a[i]; // |
						partialSum[x + n] -= a[i]; // -
						partialSum[x - y + (n - 1) + n * 2] -= a[i]; // \
						partialSum[x + y + n * 4 - 1] -= a[i]; // /

						a[t] = a[i];
						a[i] = h;

						continue;
					}

					if (f(i + 1)) return true;

					partialSum[y] -= a[i]; // |
					partialSum[x + n] -= a[i]; // -
					partialSum[x - y + (n - 1) + n * 2] -= a[i]; // \
					partialSum[x + y + n * 4 - 1] -= a[i]; // /

					a[t] = a[i];
					a[i] = h;
				}

			return false;
		}

		public static void Main()
		{
			var obj = new Task178D1();
			obj.Solve();
		}

		#region
		private class InputTokenizer
		{
			private List<string> _tokens = new List<string>();
			private int _offset = 0;

			public InputTokenizer()
			{
			}

			public InputTokenizer String(String s)
			{
				_tokens.AddRange(s.Split(new char[] { ' ', '\n', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries));
				return this;
			}

			public InputTokenizer Line()
			{
				return String(Console.In.ReadLine());
			}

			public InputTokenizer Read(out int v)
			{
				v = int.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out int[] v)
			{
				v = new int[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out long v)
			{
				v = long.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out long[] v)
			{
				v = new long[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out ulong v)
			{
				v = ulong.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out ulong[] v)
			{
				v = new ulong[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out double v)
			{
				v = double.Parse(_tokens[_offset++].Replace('.', ','));
				return this;
			}

			public InputTokenizer Read(int n, out double[] v)
			{
				v = new double[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out string v)
			{
				v = _tokens[_offset++];
				return this;
			}

			public InputTokenizer Read(int n, out string[] v)
			{
				v = new string[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}
		}
		#endregion
	}
}