#include <cstdio>
#include <algorithm>
#include <utility>
#include <vector>

#define st first
#define nd second

using namespace std;

typedef pair<int, int> PII;

const int MAXN = 1E2 + 8;

int n;
int p1, p2;
int b[MAXN][MAXN];

bool input(){
	static int a[MAXN][MAXN];
	vector<int> num1, num2;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= i; ++j)
			scanf("%d", b[i] + j), num2.push_back(b[i][j]);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= i; ++j)
			scanf("%d", a[i] + j), num1.push_back(a[i][j]);

	sort(num1.begin(), num1.end());
	sort(num2.begin(), num2.end());
	if (num1 != num2)
		return false;

	static int mark[MAXN * MAXN];
	for (int t = 0, i = 1; i <= n; ++i)
		for (int j = 1; j <= i; ++j){
			a[i][j] = lower_bound(num1.begin(), num1.end(), a[i][j]) - num1.begin();
			--num1[a[i][j]];
			mark[a[i][j]] = t++;
			b[i][j] = lower_bound(num2.begin(), num2.end(), b[i][j]) - num2.begin();
			--num2[b[i][j]];
		}
	p1 = p2 = -1;
	for (int i = 1; i < num2.size(); ++i)
		if (num2[i - 1] == num2[i])
			p1 = mark[i - 1], p2 = mark[i];
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= i; ++j)
			b[i][j] = mark[b[i][j]];
	return true;
}

int tot;
PII ans[MAXN * MAXN * MAXN];

int c[MAXN][MAXN];
PII loc[MAXN * MAXN];

inline void rotate(int i, int j){
	ans[tot++] = PII(i, j);
	int t = c[i][j];
	loc[c[i][j] = c[i + 1][j + 1]] = PII(i, j);
	loc[c[i + 1][j + 1] = c[i + 1][j]] = PII(i + 1, j + 1);
	loc[c[i + 1][j] = t] = PII(i + 1, j);
}

bool work(){
	tot = 0;
	if (n == 1)
		return true;

	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= i; ++j)
			loc[c[i][j] = b[i][j]] = PII(i, j);
	for (int num = n * (n + 1) / 2, i = n; i > 2; --i){
		for (int j = i; j > 1; --j){
			--num;
			if (c[i][j] == num)
				continue;

			int x0 = loc[num].st, y0 = loc[num].nd;
			for (; y0 >= j; --x0, --y0)
				rotate(x0 - 1, y0 - 1);
			for (; x0 < i; ++x0)
				rotate(x0, y0);
			for (; y0 < j; ++y0)
				rotate(x0 - 1, y0);
		}

		/* solve a[i][1] */
		--num;
		if (loc[num] != PII(i, 1)){
		   	if (loc[num] != PII(i - 1, 1)){
				int x0 = loc[num].st, y0 = loc[num].nd;

				for (; y0 > 1; --x0, --y0)
					rotate(x0 - 1, y0 - 1);
				for (; x0 < i - 1; ++x0)
					rotate(x0, y0);
			}
			rotate(i - 2, 1);
			rotate(i - 1, 1);
			rotate(i - 1, 1);
			rotate(i - 2, 1);
			rotate(i - 2, 1);
			rotate(i - 1, 1);
		}
	}
	for (; c[2][2] != 2; rotate(1, 1));
	return c[1][1] == 0;
}

void output(){
	printf("%d\n", tot);
	for (int i = 0; i < tot; ++i)
		printf("%d %d\n", ans[i].st, ans[i].nd);
}

int main(){
	while (scanf("%d", &n) != EOF){
		if (!input()){
			puts("-1");
			continue;
		}

		if (work())
			output();
		else if (p1 != -1){
			for (int i = 1; i <= n; ++i)
				for (int j = 1; j <= i; ++j){
					if (b[i][j] == p1)
						b[i][j] = p2;
					else if (b[i][j] == p2)
						b[i][j] = p1;
				}
			work();
			output();
		}
		else
			puts("-1");
	}
	return 0;
}

