#include <bits/stdc++.h>
using namespace std;

#define fi(a,b,c) for(int a=b;a<=c;a++)
#define fo(a,b,c) for(int a=b;a>=c;a--)
#define long long long
#define pii pair<int,int>
#define mp make_pair

const int N = 10000;

int n, m;
long ans;
int biggest_prime[N], par[N];
long d[N], c[N], s[N];
pii biggest_prime_number[N];
int counts_prime[N][700], counts_prime_[N][700];
vector<int> prime;
vector<pii> node[N];

void Find_Prime(){
    vector<int> h;
    fi(i,0,5000) h.push_back(0);

    fi(i,2,5000){
        if (!h[i]){
            h[i] = i; prime.push_back(i);
        }

        int j = 0;
        while (j < prime.size() && prime[j] <= h[i] && prime[j]*i <= 5000){
            h[prime[j]*i] = prime[j]; j++;
        }
    }
}

void Init(){
    Find_Prime();

    fi(i,2,5000){
        int x = i;
        fi(j,0,prime.size()-1){
            counts_prime[i][j] = counts_prime[i-1][j];

            if (x%prime[j] == 0) biggest_prime_number[i] = mp(j,0);
            while (x%prime[j] == 0){
                counts_prime[i][j]++, x /= prime[j];
                biggest_prime_number[i].second++;
            }

            counts_prime_[i][j] = counts_prime_[i][j-1]+counts_prime[i][j];

            if (counts_prime[i][j] > 0) biggest_prime[i] = j;
        }
    }
}

void New_Edge(int x, int y, int cost){
    node[x].push_back(mp(y,cost));
    node[y].push_back(mp(x,cost));
}

void Solve_Pack(int l, int r){
    int tmp = 0;
    fi(i,l,r-1){
        int j = biggest_prime_number[i+1].first;
        int counts_dif = biggest_prime_number[i+1].second;

        m++;
        New_Edge(i,m,counts_prime_[i][j-1]);

        if (tmp > 0)
            New_Edge(m-1,m,tmp-counts_prime_[i][j-1]);
        else
            New_Edge(1,m,counts_prime_[i][biggest_prime[i]]-counts_prime_[i][j-1]);

        tmp = counts_prime_[i+1][j-1]+counts_dif;
    }

    New_Edge(m,r,tmp);
}

void Make_New_Path(){
    New_Edge(1,2,1);

    m = 5000;
    int l = 3, r = 2;
    fi(i,1,prime.size()-1){
        while (r+1 <= 5000 && biggest_prime[r+1] == i)
            r++;

        Solve_Pack(l,r);

        l = r+1;
    }
}

void Dfs(int x){
    fi(i,0,node[x].size()-1){
        int y = node[x][i].first;
        long z = node[x][i].second;
        if (y == par[x]) continue;

        par[y] = x; Dfs(y);

        s[x] += s[y] + c[y]*z; c[x] += c[y];
    }
    c[x] += d[x];
}

void Dfs_Solve(int x, long res){
    ans = min(ans, res);
    fi(i,0,node[x][i].size()-1){
        int y = node[x][i].first;
        long z = node[x][i].second;
        if (y == par[x]) continue;

        res += (n-c[y]-c[y])&z;
        Dfs(y);
    }
}

main(){
    Init();
    Make_New_Path();

    cin >> n;
    fi(i,1,n){
        int x;
        cin >> x;
        if (x == 0) x = 1;

        d[x]++;
    }

    Dfs(1);
    ans = s[1];

    Dfs_Solve(1,d[1]);

    cout << ans;
}


#include <bits/stdc++.h>
#define int long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()

using namespace std;

const int K = 5007;

int cnt[K], rcnt[K];
int cl[K];

int get(int l, int r){
	if (l > r) return 0;
	return cnt[r] - cnt[l-1];
}

int pref[K];

vector<pair<int, int> > min_remainder[K];

pair<int, int> get_opt(pair<int, int> a, pair<int, int> b){
	if (a.first > b.first) return {a.first, max(a.second, b.first)};
	return {b.first, max(b.second, a.first)};

const int INF = -1;

main(){
#ifdef LOCAL
	freopen("C_input.txt", "r", stdin);
	//freopen("C_output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int> v(n);
	for (int i=0;i<K;++i){
		 cl[i] = INF;
	}
	for (int i=0; i < n; ++i) cin >> v[i];
	sort(v.begin(), v.end());
	int u = 0;
	for (int i=0;i<n;++i){
		cnt[v[i]]++;
		rcnt[v[i]]++;
		cl[v[i]] = v[i];
	}
	for (int i=2; i < K; ++i){
		if (cl[i] == i) continue;
		cl[i] = cl[i-1];
	}
	for (int i=2; i < K; ++i){
		vector<pair<int, int> > min_here;
		vector<int> starts;
		for (int j=i; j < K; j += i){
			starts.push_back(j);
		}
		starts.push_back(K);
		for (int j=starts.size()-2; j >= 0; j--){
			int nxt = starts[j+1];
			int el = starts[j];
			int A = cl[nxt-1];
			if (A < el) A = INF;
			int B = INF;
			if (A!=INF) {
				if (rcnt[A] > 1) B = A;
				else{
					B = cl[A-1];
					if (B < el) B = -1;
				}
			}
			if (A!=INF) A = A%i;
			if (B!=INF) B = B%i;
			//if (i==2005) cout << A << " " << B << endl;
			if (!min_here.size()) min_here.push_back({A, B});
			else min_here.push_back(get_opt(min_here.back(), {A, B}));
		}
		reverse(min_here.begin(), min_here.end());
		min_remainder[i] = min_here;
	}

	for (int i=1; i < K; ++i) cnt[i] += cnt[i-1];
	for (int i=2; i < K; ++i){
		int ss = 0;
		for (int j=i; j < K; j += i){
			int L = j, R = min(K-1, j+i-1);
			ss += get(L, R) * (j/i);
		}
		pref[i] = ss;
	}
	int ans = 0;
	for (int take=2; take < K; ++take){
		int tut = 0;
		int L = 1, R = K;
		while (R-L>1){
			int M = (L+R)/2;
			int how = pref[M];

			int closest = ((take+M-1)/M)*M;

			if (2*take < K){
				int closest = ((2*take+M-1)/M)*M;
				int A;
				if (closest >= K){
					closest = K-1;
					A = cl[closest-1];
					if (A < 2*take) A = INF;

					if (A < M) A = INF;
				}
				else{
					A = cl[closest-1];
					if (A < 2*take) A = INF;

					if (A < M) A = INF;

					if (A!=INF) A = A%M;
					pair<int, int> Q = min_remainder[M][closest/M-1];

					A = max(Q.first, A);
				}
				if (A!=INF){
					int spend = max(0ll, (2*take-A+M-1)/M);

					int e = how-spend;

					if (e >= take) L = M;
				}
			}

			int A, B;
			pair<int, int> Q;

			if (closest >= K){
				closest = K-1;
				A = cl[closest-1];
				if (A < take) A = INF;
				B = INF;
				if (A!=INF){
					if (rcnt[A] > 1) B = A;
					else{
						B = cl[A-1];
						if (B < take) B = INF;
					}
				}

				if (A!=INF) A = A%M;
				if (B!=INF) B = B%M;
				Q={A, B};
			}

			else{

				A = cl[closest-1];
				if (A < take) A = INF;
				B = INF;
				if (A!=INF){
					if (rcnt[A] > 1) B = A;
					else{
						B = cl[A-1];
						if (B < take) B = INF;
					}
				}

				if (A!=INF) A = A%M;
				if (B!=INF) B = B%M;

				Q = min_remainder[M][closest/M-1];
				Q = get_opt(Q, {A, B});
			}

			//if (take==2) cout << M << " " << A << " " << B << " " << Q.first << " " << Q.second << " " << how << endl;

			if (Q.second == INF){
				R = M;
				continue;
			}

			A = Q.first, B = Q.second;

			int spend = max(0LL, (take-A+M-1)/M) + max(0LL, (take-B+M-1)/M);

			//if (take==2) cout << spend << endl;

			how -= spend;

			if (how >= take) L = M;
			else R = M;
		}
		if (L > 1) ans = max(ans, L*take);

	}
	cout << ans;

}


	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	cin>>n;
	memset(pre,-1,sizeof(pre));
	memset(s,0,sizeof(s));
	memset(nxt,-1,sizeof(nxt));
	for (int i = 0; i < n; i++) {
		int x; cin>>x;
		s[x]++;
	}
	for (int i = N-2; i >= 0; i--)
		nxt[i] = s[i] ? i : nxt[i+1];
	for (int i = 1; i < N; i++) {
		pre[i] = s[i] ? i : pre[i-1];
		s[i] += s[i-1];
	}

	long long ans = 0;
	auto valid = [&](int x, int y) {
		if (x > 1 && y > 1)
			ans = max(ans, 1LL*x*y);
	};
	for (int y = 2; y < N/2; y++) {
		long long cnt = 0;
		for (int k = 1; 1LL*y*(k+1) < N; k++)
			cnt += 1LL * k * sum(y*k, y*(k+1)-1);
		int a = -1, b = -1;
		auto con = [&](int x) {
			x %= y;
			if (x > a) swap(a,x);
			if (x > b) swap(b,x);
		};
		if (cnt)
		for (int k = N/2/y; k >= 0; k--) {
			int x = pre[y*(k+1)-1];
			if (x >= y*k) {
				con(x);
				if (sum(x,x) == 1)
					x = pre[x-1];
				if (x >= y*k)
					con(x);
			}
			if (2*(cnt-k) >= k*y && a>=0) {
				if (cnt - k >= (a+k*y)/2)
					valid(y, (a+k*y)/2);
				else
					valid(y, (cnt-k)/2);
			}
			if (cnt - 2*k >= k*y && b>=0) {
				if (cnt - 2*k >= b+k*y)
					valid(y, b+k*y);
				else
					valid(y, cnt-2*k);

			}
			if (a >= 0 && a+k*y >= 1 && sum(a+k*y, N-1) > 1) {
				if (cnt - 2*k - 1 >= a+k*y)
					valid(y, a+k*y);
				else valid(y, cnt-2*k-1);
			}

		}
	}
	cout << ans << endl;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
template < typename T > inline T read()
{
	T x = 0, f = 0;
	char c = getchar();
	while (!isdigit(c)) f = c == '-', c = getchar();
	while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
	return f ? -x : x;
}
namespace run
{
	typedef long long ll;
	const int N = 500009,
	          inf = 1e9;
	int mx, _mx;
	inline void upd(int x)
	{
		if (x > mx) _mx = mx, mx = x;
		else if (x > _mx) _mx = x;
	}

	int x, c, k, n, a[N], sum[N << 1], pre[N << 1], A;
	ll ans, cnty;
	int main()
	{
		n = read < int > ();
		for (int i = 1; i <= n; ++i)
			++sum[a[i] = read < int > ()], A = max(A, a[i]);
		pre[0] = -1;
		for (int i = 1; i <= 2 * A; ++i)
			pre[i] = sum[i] ? i : pre[i - 1], sum[i] += sum[i - 1];

		for (int i = 1; i <= A; ++i)
		{
			cnty = 0;
			for (int j = A / i * i; j; j -= i) cnty += sum[A] - sum[j - 1];
			mx = -inf, _mx = -inf;
			for (int j = A / i * i; j >= 0; j -= i)
			{
				x = pre[j + i - 1];
				if (x >= j)
				{
					c = sum[x] - sum[x - 1];
					upd(x % i);
					if (c > 1) upd(x % i);
					else if (pre[x - 1] >= j) upd(pre[x - 1] % i);
				}

				x = (j + mx) / 2;
				if (x > 1 && cnty - j / i > 1)
					ans = max(ans, 1 ll * i * min(1 ll * x, cnty - j / i));

				if (mx >= 0 && _mx >= 0)
				{
					x = j + _mx;
					if (x > 1 && cnty - j / i * 2 > 1)
						ans = max(ans, 1 ll * i * min(1 ll * x, cnty - j / i * 2));
					x = j + mx;
					if (x > 1 && cnty - j / i * 2 - 1 > 1 && sum[A] - sum[x - 1] >= 2)
						ans = max(ans, 1 ll * i * min(1 ll * x, cnty - j / i * 2 - 1));
				}
			}
		}
		printf("%lld", ans);
		return 0;
	}
}
int main()
{
	freopen("math.in", "r", stdin);
	freopen("math.out", "w", stdout);
	return run::main();
}


#ifndef _GLIBCXX_DEBUG
#define NDEBUG
#endif
#include<cassert>
#include<vector>
#include<algorithm>
#include<iostream>

struct node{
	int a,b; // a: max, b: second max
	node():a(-1),b(-1){}
	node(node l,node r){
		if(l.a<r.a)std::swap(l,r);
		a=l.a;
		b=std::max(r.a,l.b);
	}

	void add(int x){
		if(x>b){
			auto [b_,a_]=std::minmax(a,x);
			b=b_;a=a_;
		}
	}
};

int main(){
	std::ios::sync_with_stdio(0);std::cin.tie(0);

	int n;std::cin>>n;
	std::vector<int> a(n);for(int& ai:a)std::cin>>ai;
	std::sort(begin(a),end(a));
	int const maxa=a.back();

	int const SMALL_THRES=700;
	std::array<std::vector<node>,SMALL_THRES> t;
	for(int y=2;y<SMALL_THRES;++y){
		auto& ty=t[y];ty.resize(y);
		for(int ai:a)
			ty[ai%y].add(ai);
		for(int mod=y-1;mod--;) // suffix sum
			ty[mod]=node(ty[mod],ty[mod+1]);
	}

	std::vector<int64_t> nseg(maxa+1); // nseg[y] = sum(ai/y for ai in a) for y>=2
	// right now adding x to [i] will affect all y <= i
	for(int ai:a){
		int q=1,y=ai;
		while(true){ // complexity: sqrt(ai)
			// NOTE can be optimized if there are multiple equal ai value
			// NOTE number of TKWEURS operation can be reduced
			int miny=ai/(q+1)+1;
			assert(ai/y==q && ai/miny==q);
			nseg[y]+=q;
			nseg[miny-1]-=q;
			y=miny-1;q=ai/y;
			if(y<=1)
				break;
		}
	}
	for(auto i=nseg.size();--i;)
		nseg[i-1]+=nseg[i];

	auto const countge=[&](int min){
		return end(a)-std::lower_bound(begin(a),end(a),min);
	};

	auto const countf_=[&](int min,int y,int minmod){
		// return min(2, number of ai values >= min, % y >= minmod)
		assert(minmod<y);
		if(minmod==0)
			return std::min(2,(int)countge(min));

		if(y<SMALL_THRES){
			assert(y>=2);
			return (t[y][minmod].a>=min) + (t[y][minmod].b>=min);
		}else{
			int cnt=0;
			for(int q=(maxa-1)/y+1;q>=0;--q){
				int minv=std::max(min,minmod+q*y);
				int maxv=(q+1)*y;
				if(minv>=maxv)
					break;
				cnt+=countge(minv)-countge(maxv);
				if(cnt>=2)
					break;
			}
			return std::min(2,cnt);
		}
	};

	auto const countf=[&](int min,int y,int minmod){
		auto ans=countf_(min,y,minmod);
		auto ans1=(int)std::count_if(begin(a),end(a),[&](int ai){return ai>=min&&ai%y>=minmod;});
		assert(ans==std::min(ans1,2));
		return ans;
	};

	auto const check=[&](int x,int y){
		assert(x<=maxa&&y<=maxa);
		assert(x>=2&&y>=2);

		int nsy=nseg[y];

		// case 1: take 2*x from one segment
		if(countge(2*x)>=1){
			int nswhole=2*x/y,rem=2*x%y;
			if(nsy-nswhole>=x+1)
				return true;
			if(nsy-nswhole>=x && nsy-nswhole>=x + std::max(0,1-countf(2*x,y,2*x%y)))
				return true;
		}

		// case 2: take x from 2 segments
		if(countge(x)>=2){
			int nswhole=x/y*2,rem=x%y;
			if(nsy-nswhole>=x+2)
				return true;
			if(nsy-nswhole>=x && nsy-nswhole>=x + std::max(0,2-countf(x,y,x%y)))
				return true;
		}

		return false;
	};

	int64_t ans=4;
	int x=2,y=maxa;
	while(true){
		if(check(x,y)){
			ans=std::max(ans,(int64_t)x*y);
			++x;
			if(x>maxa)break;
		}else{
			--y;
			if(y<=1)break;
		}
	}
	std::cout<<ans<<'\n';
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
//#include <sys/time.h>
using namespace std;
#define li        long long int
#define rep(i,to) for(li i=0;i<((li)(to));++i)
#define pb        push_back
#define sz(v)     ((li)(v).size())
#define bit(n)    (1ll<<(li)(n))
#define all(vec)  (vec).begin(),(vec).end()
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define MP        make_pair
#define F         first
#define S         second

#define MAX 55
li cnt = 1;
li ans[MAX][MAX][MAX];
li n;
li x, y, z;

li dx[] = {0,1,0,0,-1,0};
li dy[] = {0,0,-1,0,0,1};
li dz[] = {-1,0,0,1,0,0};

li tri(li n)
{
	return n * n * n;
}

void SET()
{
	if(ans[x][y][z] != -1) return;
	ans[x][y][z] = cnt++;
}

pair<li, pair<li, li> > get(set<pair<li, pair<li, li> > > s, li sx, li sy, li sz)
{
	pair<pair<li, li>, pair<li, pair<li, li> > > best;
	best = MP(MP(-1, -1), MP(-1, MP(-1, -1)));
	each(it, s){
		int sum = 0;
		sum += abs(x - it->F);
		sum += abs(y - it->S.F);
		sum += abs(z - it->S.S);
		if(sum != 1) continue;
		li d0 = max(abs(sx - it->F), max(abs(sy - it->S.F), abs(sz - it->S.S)));
		li d1 = abs(sx - it->F) + abs(sy - it->S.F) + abs(sz - it->S.S);
		best = max(best, MP(MP(d0, d1), *it));
	}
	return best.S;
}

void getNext(li &x, li &y, li &z, li dir)
{
	x += dx[dir];
	y += dy[dir];
	z += dz[dir];
}

int main()
{
	memset(ans, -1, sizeof(ans));
	cin >> n;
	x = 0;
	y = n - 2;
	z = n - 2;

	if(n == 1){
		cout << 1 << endl;
		return 0;
	}

	SET();
	for(li t = 0; t < n - 2; t++){
		set<pair<li, pair<li, li> > > s;
		if(t % 2){

			for(int turn = 0; t <= x; x--, turn++){
				SET();
				if(turn % 2){
					while(t <= --z) SET();
					z = t;
					while(++y <= n - 2) SET();
					y = n - 2;
				}else{
					while(t <= --y) SET();
					y = t;
					while(++z <= n - 2) SET();
					z = n - 2;
				}
			}
			x++;

			for(li yy = t + 1; yy < n - 1; yy++){
				for(li zz = t + 1 ; zz < n - 1; zz++){
					s.insert(MP(x, MP(yy, zz)));
				}
			}

		}else{

			for(int turn = 0; t <= z; z--, turn++){
				SET();
				if(turn % 2){
					while(t <= --x) SET();
					x = t;
					while(++y <= n - 2) SET();
					y = n - 2;
				}else{
					while(t <= --y) SET();
					y = t;
					while(++x <= n - 2) SET();
					x = n - 2;
				}
			}
			z++;

			for(li xx = t + 1; xx < n - 1; xx++){
				for(li yy = t + 1; yy < n - 1; yy++){
					s.insert(MP(xx, MP(yy, z)));
				}
			}

		}

		while(sz(s)){
			pair<li, pair<li, li> > p;
			if(t % 2 == 0) p = get(s, n - 1, n - 1, z);
			else p = get(s, x, n - 1, n - 1);
			s.erase(p);
			x = p.F;
			y = p.S.F;
			z = p.S.S;
			SET();
		}

		if(t % 2) x++;
		else z++;
		SET();

	}

	x++;
	SET();
	y++;
	SET();
	z++;
	SET();
	y--;
	SET();
	x--;
	SET();
	y++;
	SET();

	li dir = 0;

	while(cnt <= tri(n)){
		li next_dir = (dir + 1) % 6;
		{
			getNext(x, y, z, next_dir);
			if(0 <= x && x < n)
			if(0 <= y && y < n)
			if(0 <= z && z < n)
			if(x == n - 1 || y == n - 1 || z == n - 1)
			if(ans[x][y][z] == -1)
				dir = next_dir;
			getNext(x, y, z, (next_dir + 3) % 6);
		}
		getNext(x, y, z, dir);
		SET();
	}

	rep(i, n){
		rep(j, n){
			rep(k, n) cout << ans[i][j][k] << " ";
			cout << endl;
		}
		cout << endl;
	}
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
//#include <sys/time.h>
using namespace std;
#define li        long long int
#define rep(i,to) for(li i=0;i<((li)(to));++i)
#define pb        push_back
#define sz(v)     ((li)(v).size())
#define bit(n)    (1ll<<(li)(n))
#define all(vec)  (vec).begin(),(vec).end()
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define MP        make_pair
#define F         first
#define S         second

#define MAX 55
li cnt = 1;
li ans[MAX][MAX][MAX];
li n;
li x, y, z;

li dx[] = {0,1,0,0,-1,0};
li dy[] = {0,0,-1,0,0,1};
li dz[] = {-1,0,0,1,0,0};

li tri(li n)
{
	return n * n * n;
}

void SET()
{
	if(ans[x][y][z] != -1) return;
	ans[x][y][z] = cnt++;
}

pair<li, pair<li, li> > get(set<pair<li, pair<li, li> > > s, li sx, li sy, li sz)
{
	pair<pair<li, li>, pair<li, pair<li, li> > > best;
	best = MP(MP(-1, -1), MP(-1, MP(-1, -1)));
	rep(dir, 6){
		li xx = x + dx[dir];
		li yy = y + dy[dir];
		li zz = z + dz[dir];
		if(s.find(MP(xx, MP(yy, zz))) == s.end()) continue;
		li d0 = max(abs(sx - xx), max(sy - yy, sz - zz));
		li d1 = abs(sx - xx) + abs(sy - yy) + abs(sz - zz);
		best = max(best, MP(MP(d0, d1), MP(xx, MP(yy, zz))));
	}
	return best.S;
}

void getNext(li &x, li &y, li &z, li dir)
{
	x += dx[dir];
	y += dy[dir];
	z += dz[dir];
}

int main()
{
	memset(ans, -1, sizeof(ans));
	cin >> n;
	x = 0;
	y = n - 2;
	z = n - 2;

	if(n == 1){
		cout << 1 << endl;
		return 0;
	}

	SET();
	for(li t = 0; t < n - 2; t++){
		set<pair<li, pair<li, li> > > s;
		if(t % 2){

			for(int turn = 0; t <= x; x--, turn++){
				SET();
				if(turn % 2){
					while(t <= --z) SET();
					z = t;
					while(++y <= n - 2) SET();
					y = n - 2;
				}else{
					while(t <= --y) SET();
					y = t;
					while(++z <= n - 2) SET();
					z = n - 2;
				}
			}
			x++;

			for(li yy = t + 1; yy < n - 1; yy++){
				for(li zz = t + 1 ; zz < n - 1; zz++){
					s.insert(MP(x, MP(yy, zz)));
				}
			}

		}else{

			for(int turn = 0; t <= z; z--, turn++){
				SET();
				if(turn % 2){
					while(t <= --x) SET();
					x = t;
					while(++y <= n - 2) SET();
					y = n - 2;
				}else{
					while(t <= --y) SET();
					y = t;
					while(++x <= n - 2) SET();
					x = n - 2;
				}
			}
			z++;

			for(li xx = t + 1; xx < n - 1; xx++){
				for(li yy = t + 1; yy < n - 1; yy++){
					s.insert(MP(xx, MP(yy, z)));
				}
			}

		}

		while(sz(s)){
			pair<li, pair<li, li> > p;
			if(t % 2 == 0) p = get(s, n - 1, n - 1, z);
			else p = get(s, x, n - 1, n - 1);
			s.erase(p);
			x = p.F;
			y = p.S.F;
			z = p.S.S;
			SET();
		}

		if(t % 2) x++;
		else z++;
		SET();

	}

	x++;
	SET();
	y++;
	SET();
	z++;
	SET();
	y--;
	SET();
	x--;
	SET();
	y++;
	SET();

	li dir = 0;

	while(cnt <= tri(n)){
		li next_dir = (dir + 1) % 6;
		{
			getNext(x, y, z, next_dir);
			if(0 <= x && x < n)
			if(0 <= y && y < n)
			if(0 <= z && z < n)
			if(x == n - 1 || y == n - 1 || z == n - 1)
			if(ans[x][y][z] == -1)
				dir = next_dir;
			getNext(x, y, z, (next_dir + 3) % 6);
		}
		getNext(x, y, z, dir);
		SET();
	}

	rep(i, n){
		rep(j, n){
			rep(k, n) printf("%d ", (int)ans[i][j][k]);
			printf("\n");
		}
		printf("\n");
	}
}


#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
//#include <sys/time.h>
using namespace std;
#define li        long long int
#define rep(i,to) for(li i=0;i<((li)(to));++i)
#define pb        push_back
#define sz(v)     ((li)(v).size())
#define bit(n)    (1ll<<(li)(n))
#define all(vec)  (vec).begin(),(vec).end()
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define MP        make_pair
#define F         first
#define S         second

#define MAX 55
li cnt = 1;
li ans[MAX][MAX][MAX];
li n;
li x, y, z;

li dx[] = {0,1,0,0,-1,0};
li dy[] = {0,0,-1,0,0,1};
li dz[] = {-1,0,0,1,0,0};

li tri(li n)
{
	return n * n * n;
}

void SET()
{
	if(ans[x][y][z] != -1) return;
	ans[x][y][z] = cnt++;
}

pair<li, pair<li, li> > get(set<pair<li, pair<li, li> > > s, li sx, li sy, li sz)
{
	pair<pair<li, li>, pair<li, pair<li, li> > > best;
	best = MP(MP(-1, -1), MP(-1, MP(-1, -1)));
	rep(dir, 6){
		li xx = x + dx[dir];
		li yy = y + dy[dir];
		li zz = z + dz[dir];
		if(s.find(MP(xx, MP(yy, zz))) == s.end()) continue;
		li d0 = max(abs(sx - xx), max(sy - yy, sz - zz));
		li d1 = abs(sx - xx) + abs(sy - yy) + abs(sz - zz);
		best = max(best, MP(MP(d0, d1), MP(xx, MP(yy, zz))));
	}
	return best.S;
}

void getNext(li &x, li &y, li &z, li dir)
{
	x += dx[dir];
	y += dy[dir];
	z += dz[dir];
}

int main()
{
	memset(ans, -1, sizeof(ans));
	cin >> n;
	x = 0;
	y = n - 2;
	z = n - 2;

	if(n == 1){
		cout << 1 << endl;
		return 0;
	}

	SET();
	for(li t = 0; t < n - 2; t++){
		set<pair<li, pair<li, li> > > s;
		if(t % 2){

			for(int turn = 0; t <= x; x--, turn++){
				SET();
				if(turn % 2){
					while(t <= --z) SET();
					z = t;
					while(++y <= n - 2) SET();
					y = n - 2;
				}else{
					while(t <= --y) SET();
					y = t;
					while(++z <= n - 2) SET();
					z = n - 2;
				}
			}
			x++;

			for(li yy = t + 1; yy < n - 1; yy++){
				for(li zz = t + 1 ; zz < n - 1; zz++){
					s.insert(MP(x, MP(yy, zz)));
				}
			}

		}else{

			for(int turn = 0; t <= z; z--, turn++){
				SET();
				if(turn % 2){
					while(t <= --x) SET();
					x = t;
					while(++y <= n - 2) SET();
					y = n - 2;
				}else{
					while(t <= --y) SET();
					y = t;
					while(++x <= n - 2) SET();
					x = n - 2;
				}
			}
			z++;

			for(li xx = t + 1; xx < n - 1; xx++){
				for(li yy = t + 1; yy < n - 1; yy++){
					s.insert(MP(xx, MP(yy, z)));
				}
			}

		}

		while(sz(s)){
			pair<li, pair<li, li> > p;
			if(t % 2 == 0) p = get(s, n - 1, n - 1, z);
			else p = get(s, x, n - 1, n - 1);
			s.erase(p);
			x = p.F;
			y = p.S.F;
			z = p.S.S;
			SET();
		}

		if(t % 2) x++;
		else z++;
		SET();

	}

	x++;
	SET();
	y++;
	SET();
	z++;
	SET();
	y--;
	SET();
	x--;
	SET();
	y++;
	SET();

	li dir = 0;

	while(cnt <= tri(n)){
		li next_dir = (dir + 1) % 6;
		{
			getNext(x, y, z, next_dir);
			if(0 <= x && x < n)
			if(0 <= y && y < n)
			if(0 <= z && z < n)
			if(x == n - 1 || y == n - 1 || z == n - 1)
			if(ans[x][y][z] == -1)
				dir = next_dir;
			getNext(x, y, z, (next_dir + 3) % 6);
		}
		getNext(x, y, z, dir);
		SET();
	}

	rep(i, n){
		rep(j, n){
			rep(k, n) cout << ans[i][j][k] << " ";
			cout << endl;
		}
		cout << endl;
	}
}


#define LOCAL

/** ` Micro Mezzo Macro Flation -- Overheated Economy ., **/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)

#define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
#define DO(n) while(n--)
#define DO_C(n) int n____ = n; while(n____--)
#define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)
#define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)
#define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i<j;++i,--j)
#define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i<=j;++i,--j)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)

#define Rush int T____; RD(T____); DO(T____)
#pragma comment(linker, "/STACK:36777216")
//#pragma GCC optimize ("O2")
#define Ruby system("ruby main.rb")
#define Haskell system("runghc main.hs")
#define Pascal system("fpc main.pas")

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VD;
typedef set<int> SI;
typedef set<string> SS;
typedef set<LL> SL;
typedef set<DB> SD;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef map<LL, int> MLI;
typedef map<DB, int> MDI;
typedef map<int, bool> MIB;
typedef map<string, bool> MSB;
typedef map<LL, bool> MLB;
typedef map<DB, bool> MDB;
typedef pair<int, int> PII;
typedef pair<int, bool> PIB;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
typedef set<PII> SII;
typedef map<PII, int> MPIII;
typedef map<PII, bool> MPIIB;

/** I/O Accelerator **/

/* ... :" We are I/O Accelerator ... Use us at your own risk ;) ... " .. */

template<class T> inline void RD(T &);
template<class T> inline void OT(const T &);

inline int RD(){ int x; RD(x); return x;}
template<class T> inline T& _RD(T &x){ RD(x); return x;}
inline void RC(char &c){scanf(" %c", &c);}
inline void RS(char *s){scanf("%s", s);}

template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}
template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}

template<size_t T> inline void RST(bitset<T> &S){
    S.reset();
}

template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){
    while (!Q.empty()) Q.pop();
}

template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){
    while (!Q.empty()) Q.pop();
}

template<class T> inline void CLR(T &A){A.clear();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2){FLC(A0), FLC(A1), FLC(A2);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}

template<class T> inline void SRT(T &A){sort(ALL(A));}
template<class T, class C> inline void SRT(T &A, C B){sort(ALL(A), B);}

/** Add - On **/

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = 3.14159265358979323846264; //M_PI;

// <<= ` 0. Daily Use .,

template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(min(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
int Ceil(int x, int y){return (x - 1) / y + 1;}

// <<= ` 1. Bitwise Operation .,
inline bool _1(int x, int i){return x & 1<<i;}
inline bool _1(LL x, int i){return x & 1LL<<i;}
inline LL _1(int i){return 1LL<<i;}
//inline int _1(int i){return 1<<i;}
inline LL _U(int i){return _1(i) - 1;};
//inline int _U(int i){return _1(i) - 1;};

template<class T> inline T low_bit(T x) {
    return x & -x;
}

template<class T> inline T high_bit(T x) {
    T p = low_bit(x);
    while (p != x) x -= p, p = low_bit(x);
    return p;
}

inline int count_bits(int x){
    x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
    x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
    x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
    x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
    x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
    return x;
}

inline int count_bits(LL x){
    x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1);
    x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2);
    x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4);
    x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8);
    x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16);
    x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32);
    return x;
}

int reverse_bits(int x){
    x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
    x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
    x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
    x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
    x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
    return x;
}

LL reverse_bits(LL x){
    x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
    x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
    x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
    x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
    x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
    x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
    return x;
}

// <<= ` 2. Modular Arithmetic Basic .,

inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}
inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
inline int pdt(int a, int b){return (LL)a * b % MOD;}
inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}

inline int pow(int a, int b){
    int c = 1;
    while (b) {
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

template<class T>
inline int pow(T a, int b){
    T c(1);
    while (b) {
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

inline int _I(int b){
    int a = MOD, x1 = 0, x2 = 1, q;
    while (true){
        q = a / b, a %= b;
        if (!a) return (x2 + MOD) % MOD;
        DEC(x1, pdt(q, x2));

        q = b / a, b %= a;
        if (!b) return (x1 + MOD) % MOD;
        DEC(x2, pdt(q, x1));
    }
}

inline void DIV(int &a, int b){MUL(a, _I(b));}
inline int qtt(int a, int b){return pdt(a, _I(b));}

inline int sum(int a, int b, int MOD){
    a += b; if (a >= MOD) a -= MOD;
    return a;
}

inline int phi(int n){
    int res = n;
    for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
        DEC(res, qtt(res, i));
        do{n /= i;} while(!(n%i));
    }
    if (n != 1)
        DEC(res, qtt(res, n));
    return res;
}

// <<= '9. Comutational Geometry .,

struct Po; struct Line; struct Seg;

inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
inline int sgn(DB x, DB y){return sgn(x - y);}

struct Po{
    DB x, y;
    Po(DB _x = 0, DB _y = 0):x(_x), y(_y){}

    friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}
    friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}

    friend bool operator ==(Po, Po);
    friend bool operator !=(Po, Po);
    friend Po operator +(Po, Po);
    friend Po operator -(Po, Po);
    friend Po operator *(Po, DB);
    friend Po operator /(Po, DB);

    bool operator < (const Po &rhs) const{return sgn(x, rhs.x) < 0 || sgn(x, rhs.x) == 0 && sgn(y, rhs.y) < 0;}
    Po operator-() const{return Po(-x, -y);}
    Po& operator +=(Po rhs){x += rhs.x, y += rhs.y; return *this;}
    Po& operator -=(Po rhs){x -= rhs.x, y -= rhs.y; return *this;}
    Po& operator *=(DB k){x *= k, y *= k; return *this;}
    Po& operator /=(DB k){x /= k, y /= k; return *this;}

    DB length_sqr(){return sqr(x) + sqr(y);}
    DB length(){return sqrt(length_sqr());}

    DB atan(){
        return atan2(y, x);
    }

    void input(){
        scanf("%lf %lf", &x, &y);
    }
};

bool operator ==(Po a, Po b){return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}
bool operator !=(Po a, Po b){return sgn(a.x - b.x) != 0 || sgn(a.y - b.y) != 0;}
Po operator +(Po a, Po b){return Po(a.x + b.x, a.y + b.y);}
Po operator -(Po a, Po b){return Po(a.x - b.x, a.y - b.y);}
Po operator *(Po a, DB k){return Po(a.x * k, a.y * k);}
Po operator *(DB k, Po a){return a * k;}
Po operator /(Po a, DB k){return Po(a.x / k, a.y / k);}

struct Line{
    Po a, b;
    Line(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
    Line(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
    Line(Seg);

    friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}
};

struct Seg{
    Po a, b;
    Seg(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
    Seg(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
    Seg(Line l);

    friend ostream& operator <<(ostream& out, Seg p){return out << p.a << "-" << p.b;}
    DB length(){return (b - a).length();}
};

Line::Line(Seg l):a(l.a), b(l.b){}
Seg::Seg(Line l):a(l.a), b(l.b){}

#define innerProduct dot
#define scalarProduct dot
#define dotProduct dot
#define outerProduct det
#define crossProduct det

inline DB dot(DB x1, DB y1, DB x2, DB y2){return x1 * x2 + y1 * y2;}
inline DB dot(Po a, Po b){return dot(a.x, a.y, b.x, b.y);}
inline DB dot(Po p0, Po p1, Po p2){return dot(p1 - p0, p2 - p0);}
inline DB dot(Line l1, Line l2){return dot(l1.b - l1.a, l2.b - l2.a);}
inline DB det(DB x1, DB y1, DB x2, DB y2){return x1 * y2 - x2 * y1;}
inline DB det(Po a, Po b){return det(a.x, a.y, b.x, b.y);}
inline DB det(Po p0, Po p1, Po p2){return det(p1 - p0, p2 - p0);}
inline DB det(Line l1, Line l2){return det(l1.b - l1.a, l2.b - l2.a);}

template<class T1, class T2> inline DB dist(T1 x, T2 y){return sqrt(dist_sqr(x, y));}

inline DB dist_sqr(Po a, Po b){return sqr(a.x - b.x) + sqr(a.y - b.y);}
inline DB dist_sqr(Po p, Line l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}
inline DB dist_sqr(Po p, Seg l){
    Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
    if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));
    else return min(v1.length_sqr(), v2.length_sqr());
}

inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}
inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}

inline DB dist_sqr(Line l1, Line l2){
    if (sgn(det(l1, l2)) != 0) return 0;
    return dist_sqr(l1.a, l2);
}
inline DB dist_sqr(Line l1, Seg l2){
    Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);
    return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
}

bool isIntersect(Seg l1, Seg l2){

    //if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;

    return
        min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
        min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
        min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
        min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
    sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&
    sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;

}

inline DB dist_sqr(Seg l1, Seg l2){
    if (isIntersect(l1, l2)) return 0;
    else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));
}

inline bool isOnExtremePoint(const Po &p, const Seg &l){
    return p == l.a || p == l.b;
}

inline bool isOnseg(const Po &p, const Seg &l){

    //if (p == l.a || p == l.b) return false;

    return sgn(det(p, l.a, l.b)) == 0 &&
        sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
}

inline Po intersect(const Line &l1, const Line &l2){
    return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
}

// perpendicular foot
inline Po intersect(const Po & p, const Line &l){
    return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
}

inline Po rotate(Po p, DB alpha, Po o = Po()){
    p.x -= o.x, p.y -= o .y;
    return Po(p.x * cos(alpha) - p.y * sin(alpha), p.y * cos(alpha) + p.x * sin(alpha)) + o;
}

// <<= ' A. Random Event ..

inline int rand32(){return (bool(rand() & 1) << 30) | (rand() << 15) + rand();}
inline int random32(int l, int r){return rand32() % (r - l + 1) + l;}
inline int random(int l, int r){return rand() % (r - l + 1) + l;}
int dice(){return rand() % 6;}
bool coin(){return rand() % 2;}

// <<= ' 0. I/O Accelerator interface .,

template<class T> inline void RD(T &x){
    //cin >> x;
    scanf("%d", &x);
    //char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
}

template<class T> inline void OT(const T &p){
    printf("%.0lf\n", p);
}

/* .................................................................................................................................. */

const int N = 109;
int A[N][N][N], ox, oy, oz;
int n, p, q;

void watchFront(){
    n += 2; REP(i, n){
        REP(j, n) printf("%d ", A[ox + i][oy][oz + j] - q);
        puts("");
    }
}

void gao(){

    ox = n, oy = n, q = 0;

    A[ox + 0][oy + 0][oz + 0] = 1, A[ox + 0][oy + 0][oz + 1] = 4;
    A[ox + 0][oy + 1][oz + 0] = 2, A[ox + 0][oy + 1][oz + 1] = 3;

    A[ox + 1][oy + 0][oz + 0] = 6, A[ox + 1][oy + 0][oz + 1] = 5;
    A[ox + 1][oy + 1][oz + 0] = 7, A[ox + 1][oy + 1][oz + 1] = 8;

    if (n == 2) return;

    A[ox + 2][oy + 0][oz + 0] = 11, A[ox + 2][oy + 0][oz + 1] = 12;
    A[ox + 2][oy + 1][oz + 0] = 10, A[ox + 2][oy + 1][oz + 1] = 9;

    p = 13;

    int x, y, z; bool rev = false;

#define Blue (rev ? q-- : p++)
#define Red (rev ? p++ : q--)

    FOR_1(cur, 3, n){

        if (cur & 1){

            --oy; REP(i, cur-1) A[ox][oy][oz + i] = Red;

            DWN(i, cur-1, 0){
                if (i&1) DWN_1(j, cur-1, 1) A[ox + j][oy][oz + i] = Blue;
                else REP_1(j, cur-1) A[ox + j][oy][oz + i] = Blue;
            }

            x = 0, y = 0, z = cur - 1;
            A[ox + x][oy + y][oz + z] = Red;

            REP_1(i, cur - 1){
                if (i&1){
                    ++x; --y; REP(j, i+1) A[ox + x][oy + ++y][oz + z] = Red;
                    REP(j, i) A[ox + --x][oy + y][oz + z] = Red;
                }
                else {
                    ++y; --x; REP(j, i+1) A[ox + ++x][oy + y][oz + z] = Red;
                    REP(j, i) A[ox + x][oy + --y][oz + z] = Red;
                }
            }

            --oz;
            x = cur - 1, y = 0, z = 0;
            A[ox + x][oy + y][oz + z] = Blue;

            REP_1(i, cur - 1){
                if (i&1){
                    --x; --y; REP(j, i+1) A[ox + x][oy + ++y][oz + z] = Blue;
                    REP(j, i) A[ox + ++x][oy + y][oz + z] = Blue;
                }
                else {
                    ++y; ++x; REP(j, i+1) A[ox + --x][oy + y][oz + z] = Blue;
                    REP(j, i) A[ox + x][oy + --y][oz + z] = Blue;
                }
            }
        }
        else{

            --oy; REP(i, cur) A[ox][oy][oz + i] = Red;

            DWN(i, cur, 0){
                if (i&1) DWN_1(j, cur-2, 1) A[ox + j][oy][oz + i] = Blue;
                else REP_1(j, cur-2) A[ox + j][oy][oz + i] = Blue;
            }

            x = cur - 1, y = 0, z = 0;
            A[ox + x][oy + y][oz + z] = Blue;

            REP_1(i, cur - 1){
                if (i&1){
                    --z; ++y; REP(j, i+1) A[ox + x][oy + y][oz + ++z] = Blue;
                    REP(j, i) A[ox + x][oy + --y][oz + z] = Blue;
                }
                else {
                    ++z; --y; REP(j, i+1) A[ox + x][oy + ++y][oz + z] = Blue;
                    REP(j, i) A[ox + x][oy + y][oz + --z] = Blue;
                }
            }

            --ox, x = 0, y = 0, z = cur - 1;
            A[ox + x][oy + y][oz + z] = Red;

            REP_1(i, cur - 1){
                if (i&1){
                    ++y; ++z; REP(j, i+1) A[ox + x][oy + y][oz + --z] = Red;
                    REP(j, i) A[ox + x][oy + --y][oz + z] = Red;
                }
                else {
                    --z; --y; REP(j, i+1) A[ox + x][oy + ++y][oz + z] = Red;
                    REP(j, i) A[ox + x][oy + y][oz + ++z] = Red;
                }
            }

            rev = !rev;
        }

        rev = !rev;
    }
}

void print(){

    if (n % 4 == 3) ++oz; else if (n % 4 == 0) ++ox;

    REP(x, n){
        REP(y, n){
            REP(z, n) printf("%d ", A[ox + x][oy + y][oz + z] - q);
            puts("");
        }
        puts("");
    }
}

int main(){

#ifndef ONLINE_JUDGE
    //freopen("in.txt", "r", stdin);
#endif

    while (scanf("%d", &n) != EOF && n){
        if (n == 1) puts("1");
        else gao(), print();
    }
}

��һ�� n �������壬Ҫ���������������ʵ�·����
������� k < n, �������� 2 �� k ����������, �������е�����ȡ��һ��������䡣

������Ϊ������������˼ά���Ӷ���ߵ�һ�������������Ȼ��Ŀһ�������ס�����

���ȶԽ���һ���Ĳ²⣺
�������ս������ġ���(�����������ҲҪ�ǵݹ�ġ��ԳƵ�����)����
��������Ҫ��ż���ۡ�(���������ӵ����ۡ�����֮�Ƚ�����Ĺ��췽�����Ծ����������ۡ���������ǰ��ͬ�Ĳ��ֱȽ϶ࡣ)����

��ž����������һ�����򡣡���Ҫ���۵ĵط�Խ�ࡣ���������սⲻ�ǶԳƵĻ���������ѶȾͻ������ӡ�����

�ȽϿ�ѧ�ķ������ǹ��ɶ�ά�������

������ߺ����ߡ��ֱ�Ϊ������˵㡣��һ������һ���ݼ�����
��������ǰ��С�ڵ��� k ������Ѿ�ȫ�����㣬���ڵõ�һ�� k*(k+1) �ľ��Σ����һ�˵㴦�ڶԽ�λ�á�
��ô�������෴�����һ���߼��ɵõ� (k+1) �׵������������
��� n �Ժ���㿳��һ�ξ��ܵõ������

���벻��ʵ�֡���0w0��

��������ô�Ӷ�ά����ά������Ը�����һ�㡣����
�Ƚ�ֵ�ÿ��ǵ������������ǰ�����׵Ļ�˵㶼������ͬ��λ�á�����������Ҳ����ͬһ����˴˻�����λ�á�������
��������ά��������д�ԲеĻ��Ƚ����� Debug�����Һ���дһ���������۲�ĳ���Ƕȵ���ͼ������


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MAXN=6e5+51;
set<ll>res;
set<ll>::iterator it;
ll MOD,G,INVG;
ll fd,cnt,limit,inv2,v,p,q,r,s,sum,cur;
ll f[MAXN],g[MAXN],rev[MAXN],tmp[MAXN],delta[MAXN],delta2[MAXN],h[MAXN];
inline ll read()
{
    register ll num=0,neg=1;
    register char ch=getchar();
    while(!isdigit(ch)&&ch!='-')
    {
        ch=getchar();
    }
    if(ch=='-')
    {
        neg=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        num=(num<<3)+(num<<1)+(ch-'0');
        ch=getchar();
    }
    return num*neg;
}
inline ll qmul(ll x,ll y)
{
	return (x*y-(ll)((long double)x*y/MOD)*MOD+MOD)%MOD;
}
inline ll qpow(ll base,ll exponent)
{
    li res=1;
    while(exponent)
    {
        if(exponent&1)
        {
            res=qmul(res,base)%MOD;
        }
        base=qmul(base,base)%MOD,exponent>>=1;
    }
    return res;
}
inline void NTT(ll *cp,ll cnt,ll inv)
{
    ll cur=0,res=0,omg=0;
    for(register int i=0;i<cnt;i++)
    {
        if(i<rev[i])
        {
            swap(cp[i],cp[rev[i]]);
        }
    }
    for(register int i=2;i<=cnt;i<<=1)
    {
        cur=i>>1,res=qpow(inv==1?G:INVG,(MOD-1)/i);
        for(register ll *p=cp;p!=cp+cnt;p+=i)
        {
            omg=1;
            for(register int j=0;j<cur;j++)
            {
                ll t=qmul(omg,p[j+cur]%MOD),t2=p[j];
                p[j+cur]=(t2-t+MOD)%MOD,p[j]=(t2+t)%MOD;
                omg=qmul(omg,res);
            }
        }
    }
    if(inv==-1)
    {
        ll invl=qpow(cnt,MOD-2);
        for(register int i=0;i<=cnt;i++)
        {
            cp[i]=qmul(cp[i],invl)%MOD;
        }
    }
}
inline void setup()
{
	ll lcm,flg;
	cnt=1,limit=-1;
	while(cnt<=(fd<<1))
	{
		cnt<<=1,limit++;
	}
	lcm=fd/__gcd(fd,cnt)*cnt,MOD=lcm+1;
	while(MOD<1e5)
	{
		MOD+=lcm;
	}
	while(1)
	{
		flg=1;
		for(register int i=2;i<=sqrt(MOD);i++)
		{
			if(MOD%i==0)
			{
				flg=0;
				break;
			}
		}
		if(flg)
		{
			break;
		}
		MOD+=lcm;
	}
	for(G=2;;G++)
	{
		flg=1;
		for(register int i=2;i<=sqrt(MOD);i++)
		{
			if((MOD-1)%i==0)
			{
				if(qpow(G,i)==1||qpow(G,(MOD-1)/i)==1)
				{
					flg=0;
					break;
				}
			}
		}
		if(flg)
		{
			break;
		}
	}
	INVG=qpow(G,MOD-2);
	for(register int i=0;i<cnt;i++)
	{
		rev[i]=(rev[i>>1]>>1)|((i&1)<<limit);
	}
}
inline ll comb(ll x)
{
	return (li)x*(x-1)/2%fd;
}
inline void Bluestein(ll fd,ll *cp,ll inv)
{
	static ll tmp[MAXN],tmp2[MAXN];
	ll g=qpow(inv==1?G:INVG,(MOD-1)/fd);
	for(register int i=0;i<fd<<1;i++)
	{
		tmp[(fd<<1)-1-i]=qpow(g,comb(i));
	}
	for(register int i=fd<<1;i<cnt;i++)
	{
		tmp[i]=0;
	}
	for(register int i=0;i<fd;i++)
	{
		tmp2[i]=qmul(cp[i],qpow(g,(fd-comb(i))%fd));
	}
	for(register int i=fd;i<cnt;i++)
	{
		tmp2[i]=0;
	}
	NTT(tmp,cnt,1),NTT(tmp2,cnt,1);
	for(register int i=0;i<cnt;i++)
	{
		tmp[i]=qmul(tmp[i],tmp2[i]);
	}
	NTT(tmp,cnt,-1);
	for(register int i=0;i<fd;i++)
	{
		cp[i]=qmul(tmp[(fd<<1)-1-i],qpow(g,(fd-comb(i))%fd));
	}
	if(inv==-1)
	{
		ll invl=qpow(fd,MOD-2);
		for(register int i=0;i<fd;i++)
		{
			cp[i]=qmul(cp[i],invl)%MOD;
		}
	}
}
int main()
{
	fd=read();
	for(register int i=0;i<fd;i++)
	{
		f[i]=read();
	}
	for(register int i=0;i<fd;i++)
	{
		g[i]=read();
	}
	setup(),inv2=qpow(MOD-2,MOD-2),tmp[0]=f[0];
	for(register int i=1,j=fd-1;i<fd;i++,j--)
	{
		tmp[j]=f[i];
	}
	for(register int i=0;i<fd;i++)
	{
		delta[i]=qmul((g[(i+1)%fd]-g[i]+MOD)%MOD,inv2);
	}
	Bluestein(fd,tmp,1),Bluestein(fd,delta,1);
	for(register int i=0;i<fd;i++)
	{
		delta2[i]=qmul(delta[i],qpow(tmp[i],MOD-2));
	}
	Bluestein(fd,delta2,-1),r=-g[0];
	for(register int i=0;i<fd;i++)
	{
		v=delta2[i]<MOD-delta2[i]?delta2[i]:delta2[i]-MOD;
		if(abs(v)>50000)
		{
			return puts("0"),0;
		}
		h[i]=v;
	}
	for(register int i=0;i<fd;i++)
	{
		p++,q+=2*(sum-f[i]),r+=(sum-f[i])*(sum-f[i]);
		sum+=h[i];
	}
	if(sum!=0)
	{
		return puts("0"),0;
	}
	if(q*q-4*p*r>=0)
	{
		s=(ll)(sqrt(q*q-4*p*r)+0.5);
		if(s*s!=q*q-4*p*r)
		{
			return puts("0"),0;
		}
		if((-q+s)%(2*p)==0)
		{
			res.insert((-q+s)/(2*p));
		}
		if((-q-s)%(2*p)==0)
		{
			res.insert((-q-s)/(2*p));
		}
	}
	printf("%d\n",res.size());
	for(it=res.begin();it!=res.end();++it)
	{
		cur=*it;
		for(register int i=0;i<fd;i++)
		{
			printf("%lld ",cur),cur+=h[i];
		}
		puts("");
	}
}


//code sourced from kinesis
#include<bits/stdc++.h>
using namespace std;

#define _REP(i,a,b) for(int i = (a) ; i >= (int)(b) ; --i )
#define REP(i,a,b) for(int i = (a) ; i <= (int)(b) ; ++i )
#define UREP(i,u) for(int i = p[(u)] ; i + 1 ; i = edge[i].next)
//iterator: for(int u:x),x is container
#define x(p) (p).first
#define y(p) (p).second
#define pii pair<int,int>
#define mp(x,y) make_pair((x), (y))
#define sign(x) (fabs(x) < eps ? 0 : ((x) > 0 ? 1 : -1))
#define ll long long
#define L7 __int128//1<<7 bit
#define ull unsigned long long
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
#define ri1(x) scanf("%d", &(x))
#define ri2(x,y) scanf("%d%d", &(x), &(y))
#define ri3(x,y,z) scanf("%d%d%d", &(x), &(y), &(z))
#define ri4(a,b,c,d) scanf("%d%d%d%d", &(a), &(b), &(c), &(d))
//#define Debug
#ifdef Debug
#endif // Debug

const int maxn = 131072,maxx = 6000;
ll mod,g;

ll mul(ll a,ll b){return ((__int128)a * b % mod + mod) % mod;}

ll qp(ll base,ll n)
{
    base %= mod;
    ll res = 1;
    while(n){
        if(n&1) res = mul(res,base);
        base = mul(base,base);
        n >>= 1;
    }
    return res;
}

namespace NTT//优化过的ntt，使用时记得初始化
{
    ll w[maxn<<3],inv[maxn<<3],r[maxn<<3],last,p;
    void init()
    {
        p = mod;
        ll lim = maxn << 2;
        inv[1] = 1;
        for(ll i=2;i<=lim;i++) inv[i] = (p - mul((p / i),inv[p%i])%p)%p;
        for(ll i=1;i<lim;i<<=1)
        {
            ll wn = qp(g,(p - 1) / (i<<1));
            for(ll j=0,ww=1;j<i;j++,ww=mul(ww,wn)) w[i+j] = ww;
        }
    }
    void ntt(ll *f,int n,int op)
    {
        if(last!=n)
        {
            for(int i=1;i<n;i++) r[i] = (r[i>>1]>>1)|((i&1)?(n>>1):0);
            last=n;
        }
        for(int i=1;i<n;i++) if(i<r[i])swap(f[i],f[r[i]]);
        for(int i=1;i<n;i<<=1)
        for(int j=0;j<n;j+=i<<1)
            for(int k=0;k<i;k++)
            {
                ll x=f[j+k],y=mul(f[i+j+k],w[i+k]);
                f[j+k]=((x+y)%p+p)%p;f[i+j+k]=((x-y+p)%p+p)%p;
            }
        if(op==-1)
        {
            reverse(&f[1],&f[n]);
            for(int i=0;i<n;i++)f[i]=mul(f[i],inv[n]);
        }
    }
}
using NTT::ntt;

namespace get_G//求一般的原根、、多一步求phi的过程
{
    ll qp(ll base,ll n,ll mod)
    {
        ll res = 1;
        while(n){
            if(n&1) res = (__int128)res * base % mod;
            base = (__int128)base * base % mod;
            n >>= 1;
        }
        return res;
    }

    ll generator(ll p)
    {
        if(p<2) return -1;
        ll phi = p,tmp = p;
        for (ll i = 2; i * i <= tmp; ++i){
            if(tmp % i == 0){
                phi = phi / i * (i - 1);
                while(tmp % i == 0) tmp /= i;
            }
        }
        if(tmp > 1) phi = phi / tmp * (tmp - 1);
        ll n = phi;
        vector<ll> fact;
        for (ll i = 2; i * i <= n; ++i){
            if (n % i == 0){
                fact.push_back(i);
                while (n % i == 0) n /= i;
            }
        }
        if (n > 1) fact.push_back(n);
        for (ll res = 1; res <= p; ++res){
            if(__gcd(res,p) != 1) continue;
            bool ok = true;
            for (ll factor : fact){
                if (qp(res, phi / factor, p) == 1){
                    ok = false;
                    break;
                }
            }
            if (ok) return res;
        }
        return -1;
    }
}

void get_mod(int n)
{
    ll lim = 1,lcm,act,ok = 0;
    while(lim<(3*n)) lim <<= 1;
    act = lcm = 1LL * lim * n / __gcd(lim,1LL*n);
    while(act<maxx<<1) act += lcm;
    while(!ok){
        ok = 1;
        for(int i=2;1LL*i*i<=act+1;i++){
            if((act+1)%i==0){
                act += lcm;
                ok = 0;
                break;
            }
        }
    }
    mod = act + 1;
    g = get_G::generator(mod);
}

ll w[maxn];

void init(int n)
{
    ll wg = qp(g,1LL*(mod-1)/n);w[0] = 1;
    REP(i,1,n-1) w[i] = mul(w[i-1],wg);
}

void bluestein(ll *x,int n,int opt)//fft版本
{
    static ll f[maxn<<2],g[maxn<<2];
    int lim = 1;
    while(lim<(3*n)) lim <<= 1;
    REP(i,0,lim-1) f[i] = g[i] = 0;
    REP(i,0,n-1) f[i] = mul(x[i] , (opt == 1 ? w[(n-1LL*i*(i-1)/2%n)%n] : w[1LL*i*(i-1)/2%n]));
    reverse(f,f+n);
    REP(i,0,2*n-1) g[i] = (opt == -1 ? w[(n-1LL*i*(i-1)/2%n)%n] : w[1LL*i*(i-1)/2%n]);
    ntt(g,lim,1),ntt(f,lim,1);
    REP(i,0,lim-1) f[i] = mul( f[i] , g[i] );
    ntt(f,lim,-1);
    REP(i,0,n-1) x[i] = mul((opt == 1 ? w[(n-1LL*i*(i-1)/2%n)%n] : w[1LL*i*(i-1)/2%n]) , f[n-1+i]);
    ll _n = qp(n,mod-2);
    if(opt==-1) REP(i,0,n-1) x[i] = mul(x[i] , _n);
}

ll b[maxn],c[maxn],dtc[maxn],dta[maxn],s[maxn],ori_b[maxn];

vector<ll> get_a0(ll a,ll b,ll c)
{
    vector<ll> res;
    ll delta = b * b - 4LL * a * c;
    if(delta<0){
        res.push_back(0);
        return res;
    }
    ll st = floor(sqrt(delta)+0.5);
    if(st*st!=delta){
        res.push_back(0);
        return res;
    }
    if(delta==0){
        if(b%(2*a)){
            res.push_back(0);
            return res;
        }
        res.push_back(1);
        res.push_back(-b/(2*a));
    }
    else{
        if(((-b+st)%(a*2))&&((-b-st)%(a*2))){
            res.push_back(0);
            return res;
        }
        if(((-b+st)%(a*2)==0)&&((-b-st)%(a*2)==0)){
            res.push_back(2);
            res.push_back((-b+st)/(a*2));
            res.push_back((-b-st)/(a*2));
            return res;
        }
        if((-b+st)%(a*2)==0){
            res.push_back(1);
            res.push_back((-b+st)/(a*2));
            return res;
        }
        if((-b-st)%(a*2)==0){
            res.push_back(1);
            res.push_back((-b-st)/(a*2));
            return res;
        }
    }
}

ll judge(ll delta)
{
    return delta < maxx ? delta : delta - mod;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);//cf needn't delete this
    #endif // ONLINE_JUDGE
    int n;
    ri1(n);
    get_mod(n);
    NTT::init();
    init(n);
    REP(i,0,n-1) scanf("%lld",&b[i]),ori_b[i] = b[i];
    reverse(b,b+n);
    REP(i,0,n-1) scanf("%lld",&c[i]);
    ll A = n,B = 0,C = -c[0];
    int buf_b = b[n-1];
    _REP(i,n-1,1) b[i] = mul(b[i-1],-2);b[0] = mul(buf_b,-2);
    REP(i,1,n-1) dtc[i] = c[i] - c[i-1];dtc[0] = c[0] - c[n-1];
    bluestein(dtc,n,1),bluestein(b,n,1);
    REP(i,0,n-1) dta[i] = mul(dtc[i] , qp(b[i],mod-2));
    bluestein(dta,n,-1);
    REP(i,1,n-1) dta[i] = judge(dta[i]);
    REP(i,1,n-1) s[i] = s[i-1] + dta[i];
    REP(i,0,n-1) B += 2LL * (s[i] - ori_b[i]),C += (s[i] - ori_b[i]) * (s[i] - ori_b[i]);
    vector<ll> vec = get_a0(A,B,C);
    if(vec[0]==0) printf("0\n");
    else if(vec[0]==1){
        printf("1\n");
        printf("%lld ",vec[1]);
        ll pre = vec[1];
        REP(i,1,n-1) printf("%d%c",(pre = (dta[i]+pre)),i==n-1?'\n':' ');
    }
    else{
        printf("2\n");
        if(vec[1]>vec[2]) swap(vec[1],vec[2]);
        printf("%lld%c",vec[1],n==1?'\n':' ');
        ll pre = vec[1];
        REP(i,1,n-1) printf("%d%c",(pre = (dta[i]+pre)),i==n-1?'\n':' ');
        printf("%lld%c",vec[2],n==1?'\n':' ');
        pre = vec[2];
        REP(i,1,n-1) printf("%d%c",(pre = (dta[i]+pre)),i==n-1?'\n':' ');
    }
    return ~~(0-0);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef __int128 li;
const ll MAXN=6e5+51;
set<ll>res;
set<ll>::iterator it;
ll MOD,G,INVG;
ll fd,cnt,limit,inv2,v,p,q,r,s,sum,cur;
ll f[MAXN],g[MAXN],rev[MAXN],tmp[MAXN],delta[MAXN],delta2[MAXN],h[MAXN];
inline ll read()
{
    register ll num=0,neg=1;
    register char ch=getchar();
    while(!isdigit(ch)&&ch!='-')
    {
        ch=getchar();
    }
    if(ch=='-')
    {
        neg=-1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        num=(num<<3)+(num<<1)+(ch-'0');
        ch=getchar();
    }
    return num*neg;
}
inline ll qmul(ll x,ll y)
{
	return (x*y-(ll)((long double)x*y/MOD)*MOD+MOD)%MOD;
}
inline ll qpow(ll base,ll exponent)
{
    li res=1;
    while(exponent)
    {
        if(exponent&1)
        {
            res=qmul(res,base)%MOD;
        }
        base=qmul(base,base)%MOD,exponent>>=1;
    }
    return res;
}
inline void NTT(ll *cp,ll cnt,ll inv)
{
    ll cur=0,res=0,omg=0;
    for(register int i=0;i<cnt;i++)
    {
        if(i<rev[i])
        {
            swap(cp[i],cp[rev[i]]);
        }
    }
    for(register int i=2;i<=cnt;i<<=1)
    {
        cur=i>>1,res=qpow(inv==1?G:INVG,(MOD-1)/i);
        for(register ll *p=cp;p!=cp+cnt;p+=i)
        {
            omg=1;
            for(register int j=0;j<cur;j++)
            {
                ll t=qmul(omg,p[j+cur]%MOD),t2=p[j];
                p[j+cur]=(t2-t+MOD)%MOD,p[j]=(t2+t)%MOD;
                omg=qmul(omg,res);
            }
        }
    }
    if(inv==-1)
    {
        ll invl=qpow(cnt,MOD-2);
        for(register int i=0;i<=cnt;i++)
        {
            cp[i]=qmul(cp[i],invl)%MOD;
        }
    }
}
inline void setup()
{
	ll lcm,flg;
	cnt=1,limit=-1;
	while(cnt<=(fd<<1))
	{
		cnt<<=1,limit++;
	}
	lcm=fd/__gcd(fd,cnt)*cnt,MOD=lcm+1;
	while(MOD<1e5)
	{
		MOD+=lcm;
	}
	while(1)
	{
		flg=1;
		for(register int i=2;i<=sqrt(MOD);i++)
		{
			if(MOD%i==0)
			{
				flg=0;
				break;
			}
		}
		if(flg)
		{
			break;
		}
		MOD+=lcm;
	}
	for(G=2;;G++)
	{
		flg=1;
		for(register int i=2;i<=sqrt(MOD);i++)
		{
			if((MOD-1)%i==0)
			{
				if(qpow(G,i)==1||qpow(G,(MOD-1)/i)==1)
				{
					flg=0;
					break;
				}
			}
		}
		if(flg)
		{
			break;
		}
	}
	INVG=qpow(G,MOD-2);
	for(register int i=0;i<cnt;i++)
	{
		rev[i]=(rev[i>>1]>>1)|((i&1)<<limit);
	}
}
inline ll comb(ll x)
{
	return (li)x*(x-1)/2%fd;
}
inline void Bluestein(ll fd,ll *cp,ll inv)
{
	static ll tmp[MAXN],tmp2[MAXN];
	ll g=qpow(inv==1?G:INVG,(MOD-1)/fd);
	for(register int i=0;i<fd<<1;i++)
	{
		tmp[(fd<<1)-1-i]=qpow(g,comb(i));
	}
	for(register int i=fd<<1;i<cnt;i++)
	{
		tmp[i]=0;
	}
	for(register int i=0;i<fd;i++)
	{
		tmp2[i]=qmul(cp[i],qpow(g,(fd-comb(i))%fd));
	}
	for(register int i=fd;i<cnt;i++)
	{
		tmp2[i]=0;
	}
	NTT(tmp,cnt,1),NTT(tmp2,cnt,1);
	for(register int i=0;i<cnt;i++)
	{
		tmp[i]=qmul(tmp[i],tmp2[i]);
	}
	NTT(tmp,cnt,-1);
	for(register int i=0;i<fd;i++)
	{
		cp[i]=qmul(tmp[(fd<<1)-1-i],qpow(g,(fd-comb(i))%fd));
	}
	if(inv==-1)
	{
		ll invl=qpow(fd,MOD-2);
		for(register int i=0;i<fd;i++)
		{
			cp[i]=qmul(cp[i],invl)%MOD;
		}
	}
}
int main()
{
	fd=read();
	for(register int i=0;i<fd;i++)
	{
		f[i]=read();
	}
	for(register int i=0;i<fd;i++)
	{
		g[i]=read();
	}
	setup(),inv2=qpow(MOD-2,MOD-2),tmp[0]=f[0];
	for(register int i=1,j=fd-1;i<fd;i++,j--)
	{
		tmp[j]=f[i];
	}
	for(register int i=0;i<fd;i++)
	{
		delta[i]=qmul((g[(i+1)%fd]-g[i]+MOD)%MOD,inv2);
	}
	Bluestein(fd,tmp,1),Bluestein(fd,delta,1);
	for(register int i=0;i<fd;i++)
	{
		delta2[i]=qmul(delta[i],qpow(tmp[i],MOD-2));
	}
	Bluestein(fd,delta2,-1),r=-g[0];
	for(register int i=0;i<fd;i++)
	{
		v=delta2[i]<MOD-delta2[i]?delta2[i]:delta2[i]-MOD;
		if(abs(v)>50000)
		{
			return puts("0"),0;
		}
		h[i]=v;
	}
	for(register int i=0;i<fd;i++)
	{
		p++,q+=2*(sum-f[i]),r+=(sum-f[i])*(sum-f[i]);
		sum+=h[i];
	}
	if(sum!=0)
	{
		return puts("0"),0;
	}
	if(q*q-4*p*r>=0)
	{
		s=(ll)(sqrt(q*q-4*p*r)+0.5);
		if(s*s!=q*q-4*p*r)
		{
			return puts("0"),0;
		}
		if((-q+s)%(2*p)==0)
		{
			res.insert((-q+s)/(2*p));
		}
		if((-q-s)%(2*p)==0)
		{
			res.insert((-q-s)/(2*p));
		}
	}
	printf("%d\n",res.size());
	for(it=res.begin();it!=res.end();++it)
	{
		cur=*it;
		for(register int i=0;i<fd;i++)
		{
			printf("%lld ",cur),cur+=h[i];
		}
		puts("");
	}
}