#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cassert>
#include <vector>
#include <iostream>

const int N = 33;

int n, m;
int s[N][N], t[N][N];

struct Pos {
	int row, col;
	Pos(int row, int col) : row(row), col(col) {}
	Pos() {}
	void show() {
		printf("%d %d\n", row + 1, col + 1);
	}
	friend inline bool operator == (const auto &a, const auto &b) {
		return a.row == b.row && a.col == b.col;
	}
};

std::vector<Pos > answer;
Pos cursor;
bool locked[N][N];

Pos find(int color) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!locked[i][j] && s[i][j] == color && !(Pos(i, j) == cursor)) {
				return Pos(i, j);
			}
		}
	}
	return Pos(-1, -1);
}

void step(Pos p) {
	if (p == cursor) return ;
	answer.push_back(p);
	assert(std::max(std::abs(p.row - cursor.row), std::abs(p.col - cursor.col)) <= 1);
	std::swap(s[cursor.row][cursor.col], s[p.row][p.col]);
	cursor = p;
}

void run(Pos target) {
	while (cursor.col < target.col) step(Pos(cursor.row, cursor.col + 1));
	while (cursor.col > target.col) step(Pos(cursor.row, cursor.col - 1));
	while (cursor.row > target.row) step(Pos(cursor.row - 1, cursor.col));
	while (cursor.row < target.row) step(Pos(cursor.row + 1, cursor.col));
}

void colMove(Pos &where, Pos target) {
	assert(cursor == Pos(n - 2, m - 1));
	if (where.row == n - 1) {
		run(Pos(where.row - 1, where.col));
		while (where.col != target.col) {
			if (where.col > target.col) step(Pos(where.row, where.col - 1));
			if (where.col < target.col) step(Pos(where.row, where.col + 1));
			step(where);
			step(Pos(where.row - 1, where.col));
			if (where.col > target.col) where.col--; else where.col++;
			step(Pos(where.row - 1, where.col));
		}
	} else {
		if (where.row == cursor.row) run(Pos(n - 1, m - 1));
		run(Pos(where.row + 1, where.col));
		while (where.col != target.col) {
			if (where.col > target.col) step(Pos(where.row, where.col - 1));
			if (where.col < target.col) step(Pos(where.row, where.col + 1));
			step(where);
			step(Pos(where.row + 1, where.col));
			if (where.col > target.col) where.col--; else where.col++;
			step(Pos(where.row + 1, where.col));
		}
	}
}

void rowMove(Pos &where, Pos target) {
	if (where.row == target.row) return ;
	while (where.row > target.row) {
		if (where.col < m - 1) {
			step(Pos(where.row, where.col + 1));
		} else {
			step(Pos(where.row, where.col - 1));
		}
		Pos bak = cursor;
		step(Pos(where.row - 1, where.col));
		step(where);
		step(bak);
		where.row--;
	}
}

bool solve() {
	answer.clear();
	if (n == 1) {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				if (s[0][i] != t[0][j]) continue;
				std::vector<int > sv, tv;
				for (int k = 0; k < m; k++) {
					if (k != i) sv.push_back(s[0][k]);
					if (k != j) tv.push_back(t[0][k]);
				}
				if (sv == tv) {
					answer.push_back(cursor = Pos(0, i));
					if (i < j) {
						for (int k = i; k < j; k++) step(Pos(cursor.row, cursor.col + 1));
					} else {
						for (int k = i; k > j; k--) step(Pos(cursor.row, cursor.col - 1));
					}
					return true;
				}
			}
		}
		return false;
	} else if (m == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (s[i][0] != t[j][0]) continue;
				std::vector<int > sv, tv;
				for (int k = 0; k < n; k++) {
					if (k != i) sv.push_back(s[k][0]);
					if (k != j) tv.push_back(t[k][0]);
				}
				if (sv == tv) {
					answer.push_back(cursor = Pos(i, 0));
					if (i < j) {
						for (int k = i; k < j; k++) step(Pos(cursor.row + 1, cursor.col));
					} else {
						for (int k = i; k > j; k--) step(Pos(cursor.row - 1, cursor.col));;
					}
					return true;
				}
			}
		}
		return false;
	}

	cursor = Pos(-1, -1);
	answer.push_back(cursor = find(t[n - 2][m - 1]));
	if (cursor == Pos(-1, -1)) return false;
	run(Pos(n - 2, m - 1));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (i == n - 2 && j == m - 1) continue;
			Pos where = find(t[i][j]);
			if (where == Pos(-1, -1)) return false;
			assert(!(where == cursor));
			// printf("i = %d j = %d\n", i, j);
			//
			// printf("cursor = %d %d\n", cursor.row, cursor.col);
			// for (int i = 0; i < n; i++, puts(""))
			// 	for (int j = 0; j < m; j++) {
			// 		printf("%d ", s[i][j]);
			// 	}
			// puts("");
			colMove(where, Pos(where.row, j));
			//
			// printf("cursor = %d %d\n", cursor.row, cursor.col);
			// for (int i = 0; i < n; i++, puts(""))
			// 	for (int j = 0; j < m; j++) {
			// 		printf("%d ", s[i][j]);
			// 	}
			// puts("");
			rowMove(where, Pos(i, j));
			run(Pos(n - 2, m - 1));
			// printf("cursor = %d %d\n", cursor.row, cursor.col);
			// for (int i = 0; i < n; i++, puts(""))
			// 	for (int j = 0; j < m; j++) {
			// 		printf("%d ", s[i][j]);
			// 	}
			// puts("");
			locked[i][j] = true;
			assert(s[i][j] == t[i][j]);
		}
	}
	return true;
}

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%d", &s[i][j]);
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%d", &t[i][j]);
		}
	}

	if (!solve()) {
		puts("-1");
		return 0;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			assert(s[i][j] == t[i][j]);
		}
	}
	printf("%d\n", answer.size() - 1);
	for (int i = 0; i < answer.size(); i++) {
		answer[i].show();
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cassert>
#include <vector>

const int N = 33;

int n, m;
int s[N][N], t[N][N];

struct Pos {
	int row, col;
	Pos(int row, int col) : row(row), col(col) {}
	Pos() {}
	void show() {
		printf("%d %d\n", row + 1, col + 1);
	}
	friend inline bool operator == (const auto &a, const auto &b) {
		return a.row == b.row && a.col == b.col;
	}
};

std::vector<Pos > answer;
Pos cursor;
bool locked[N][N];

Pos find(int color) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!locked[i][j] && s[i][j] == color) {
				return Pos(i, j);
			}
		}
	}
	return Pos(-1, -1);
}

void step(Pos p) {
	answer.push_back(p);
	assert(std::max(std::abs(p.row - cursor.row), std::abs(p.col - cursor.col)) <= 1);
	std::swap(s[cursor.row][cursor.col], s[p.row][p.col]);
	cursor = p;
}

void run(Pos target) {
	while (cursor.col < target.col) step(Pos(cursor.row, cursor.col + 1));
	while (cursor.col > target.col) step(Pos(cursor.row, cursor.col - 1));
	while (cursor.row > target.row) step(Pos(cursor.row - 1, cursor.col));
	while (cursor.row < target.row) step(Pos(cursor.row + 1, cursor.col));
}

bool solve() {
	answer.clear();
	if (n == 1) {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				if (s[0][i] != t[0][j]) continue;
				std::vector<int > sv, tv;
				for (int k = 0; k < m; k++) {
					if (k != i) sv.push_back(s[0][k]);
					if (k != j) tv.push_back(t[0][k]);
				}
				if (sv == tv) {
					answer.push_back(cursor = Pos(0, i));
					if (i < j) {
						for (int k = i; k < j; k++) step(Pos(cursor.row, cursor.col + 1));
					} else {
						for (int k = i; k > j; k--) step(Pos(cursor.row, cursor.col - 1));
					}
					return true;
				}
			}
		}
		return false;
	} else if (m == 1) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (s[i][0] != t[j][0]) continue;
				std::vector<int > sv, tv;
				for (int k = 0; k < n; k++) {
					if (k != i) sv.push_back(s[k][0]);
					if (k != j) tv.push_back(t[k][0]);
				}
				if (sv == tv) {
					answer.push_back(cursor = Pos(i, 0));
					if (i < j) {
						for (int k = i; k < j; k++) step(Pos(cursor.row + 1, cursor.col));
					} else {
						for (int k = i; k > j; k--) step(Pos(cursor.row - 1, cursor.col));;
					}
					return true;
				}
			}
		}
		return false;
	}
	answer.push_back(cursor = find(t[n - 2][m - 1]));
	if (cursor == Pos(-1, -1)) return false;

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < m; j++) {
			if (i == n - 2 && j == m - 1) continue;
			run(Pos(n - 1, m - 1));
			Pos where = find(t[i][j]);
			if (where == Pos(-1, -1)) return false;
			if (where.col == m - 1) {
				run(Pos(where.row, where.col - 1));
				step(where);
				where.col--;
			}

			assert(where.col < m - 1);
			run(Pos(where.row, where.col + 1));
			while (where.row > i) {
				step(Pos(where.row - 1, where.col));
				step(where);
				step(Pos(where.row, where.col + 1));
				where.row--;
				step(Pos(where.row, where.col + 1));
			}
			step(Pos(cursor.row + 1, cursor.col));
			step(Pos(cursor.row, cursor.col - 1));

			while (where.col > j) {
				step(Pos(where.row, where.col - 1));
				step(where);
				step(Pos(where.row + 1, where.col));
				where.col--;
				step(Pos(cursor.row, cursor.col - 1));
			}
			locked[i][j] = true;
		}
	}
	// for (int i = 0; i < n; i++, puts(""))
	// 	for (int j = 0; j < m; j++) {
	// 		printf("%d ", s[i][j]);
	// 	}

	locked[n - 2][m - 1] = true;
	for (int i = n - 1, j = 0; j < m; j++) {
		run(Pos(n - 2, m - 1));
		Pos where = find(t[i][j]);
		if (where == Pos(-1, -1)) return false;
		while (cursor.col > where.col) step(Pos(cursor.row, cursor.col - 1));
		while (where.col > j) {
			step(Pos(where.row, where.col - 1));
			step(where);
			step(Pos(where.row - 1, where.col));
			where.col--;
			step(Pos(cursor.row, cursor.col - 1));
		}
		locked[i][j] = 1;
	}
	return true;
}

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%d", &s[i][j]);
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%d", &t[i][j]);
		}
	}

	if (!solve()) {
		puts("-1");
		return 0;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			assert(s[i][j] == t[i][j]);
		}
	}
	printf("%d\n", answer.size() - 1);
	for (int i = 0; i < answer.size(); i++) {
		answer[i].show();
	}
	return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;

const int N = 42;

int a[N][N], b[N][N];
int old_a[N][N];
int ca[N * N], cb[N * N];
int new_a[N][N], new_b[N][N];

bool FLAG;

void print(vector < pair <int, int> > &moves) {
  int cnt = moves.size();
  printf("%d\n", cnt - 1);
  for (int i = 0; i < cnt; i++) {
    if (FLAG) {
      swap(moves[i].first, moves[i].second);
    }
    printf("%d %d\n", moves[i].first + 1, moves[i].second + 1);
  }
  exit(0);
}

vector < pair <int, int> > moves;
int h, w;

void add(int x, int y) {
  assert(0 <= x && x < h && 0 <= y && y < w);
  int sz = moves.size();
  if (sz > 0) {
    int px = moves[sz - 1].first;
    int py = moves[sz - 1].second;
    swap(a[x][y], a[px][py]);
  }
  moves.push_back(make_pair(x, y));
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < N * N; i++) {
    ca[i] = cb[i] = 0;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", a[i] + j);
      ca[a[i][j]]++;
      old_a[i][j] = a[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", b[i] + j);
      cb[b[i][j]]++;
    }
  }
  for (int i = 0; i < N * N; i++) {
    if (ca[i] != cb[i]) {
      printf("%d\n", -1);
      return 0;
    }
  }
  FLAG = false;
  if (w == 1) {
    FLAG = true;
    swap(h, w);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        new_a[i][j] = a[j][i];
        new_b[i][j] = b[j][i];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        a[i][j] = new_a[i][j];
        b[i][j] = new_b[i][j];
        old_a[i][j] = a[i][j];
      }
    }
  }
  if (h == 1) {
    for (int from = 0; from < w; from++) {
      for (int to = 0; to < w; to++) {
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
            a[i][j] = old_a[i][j];
          }
        }
        vector < pair <int, int> > moves;
        int i = 0;
        int j = from;
        moves.push_back(make_pair(i, j));
        while (j < to) {
          swap(a[i][j], a[i][j + 1]);
          j++;
          moves.push_back(make_pair(i, j));
        }
        while (j > to) {
          swap(a[i][j], a[i][j - 1]);
          j--;
          moves.push_back(make_pair(i, j));
        }
        bool ok = true;
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
            if (a[i][j] != b[i][j]) {
              ok = false;
            }
          }
        }
        if (ok) {
          print(moves);
        }
      }
    }
    printf("%d\n", -1);
    return 0;
  }
  int x = -1, y = -1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == b[h - 1][w - 1]) {
        add(i, j);
        x = i;
        y = j;
        break;
      }
    }
    if (x != -1) break;
  }
  while (x > 0) {
    add(x - 1, y);
    x--;
  }
  while (y > 0) {
    add(x, y - 1);
    y--;
  }
  vector < pair <int, int> > path;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i == h - 1 && j == w - 1) {
        continue;
      }
      int ri = -1, rj = -1;
      for (int pi = i; pi < h; pi++) {
        for (int pj = (pi > i ? 0 : j + 1); pj < w; pj++) {
          if (a[pi][pj] == b[i][j]) {
            ri = pi;
            rj = pj;
            break;
          }
        }
        if (ri != -1) break;
      }
      if (rj < j) {
        while (y > rj) {
          path.push_back(make_pair(x, y));
          add(x, y - 1);
          y--;
        }
        while (x < ri - 1) {
          path.push_back(make_pair(x, y));
          add(x + 1, y);
          x++;
        }
        while (y < j) {
          add(x + 1, y);
          add(x + 1, y + 1);
          add(x, y);
          rj++;
          path.push_back(make_pair(x, y));
          add(x, y + 1);
          y++;
        }
        for (int u = (int)path.size() - 1; u >= 0; u--) {
          add(path[u].first, path[u].second);
          x = path[u].first;
          y = path[u].second;
        }
        path.clear();
      }
      if (rj > j) {
        if (ri > i) {
          while (y < rj) {
            path.push_back(make_pair(x, y));
            add(x, y + 1);
            y++;
          }
          while (x < ri - 1) {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
          }
          while (y > j) {
            add(x + 1, y);
            add(x + 1, y - 1);
            add(x, y);
            rj--;
            path.push_back(make_pair(x, y));
            add(x, y - 1);
            y--;
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
        } else {
          if (x > 0) {
            path.push_back(make_pair(x, y));
            add(x - 1, y);
            x--;
            while (y < rj) {
              path.push_back(make_pair(x, y));
              add(x, y + 1);
              y++;
            }
            while (y > j + 1) {
              add(x + 1, y);
              add(x + 1, y - 1);
              add(x, y);
              rj--;
              path.push_back(make_pair(x, y));
              add(x, y - 1);
              y--;
            }
          } else {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
            while (y < rj) {
              path.push_back(make_pair(x, y));
              add(x, y + 1);
              y++;
            }
            while (y > j + 1) {
              add(x - 1, y);
              add(x - 1, y - 1);
              add(x, y);
              rj--;
              path.push_back(make_pair(x, y));
              add(x, y - 1);
              y--;
            }
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
          add(x, y + 1);
          y++;
        }
      }
      if (ri > i) {
        if (j == 0) {
          path.push_back(make_pair(x, y));
          add(x, y + 1);
          y++;
          while (x < ri) {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
          }
          while (x > i + 1) {
            add(x, y - 1);
            add(x - 1, y - 1);
            add(x, y);
            path.push_back(make_pair(x, y));
            add(x - 1, y);
            x--;
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
          add(x + 1, y);
          x++;
        } else {
          path.push_back(make_pair(x, y));
          add(x, y - 1);
          y--;
          while (x < ri) {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
          }
          while (x > i + 1) {
            add(x, y + 1);
            add(x - 1, y + 1);
            add(x, y);
            path.push_back(make_pair(x, y));
            add(x - 1, y);
            x--;
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
          add(x + 1, y);
          x++;
        }
        if (y < w - 1) {
          add(x - 1, y + 1);
          x--;
          y++;
        } else {
          while (y > 0) {
            add(x, y - 1);
            y--;
          }
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      assert(a[i][j] == b[i][j]);
    }
  }
  print(moves);
  return 0;
}


#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define x first
#define y second
#define pb push_back
#define re real()
#define im imag()

using namespace std;

typedef pair<int, int> PII;

int n, m, a, b;
int tmp[33][33];
int source[33][33];
int target[33][33];
PII prv[33][33];
bool block[33][33];
vector<PII> R;

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

void save(){
	FWD(i,0,n) FWD(j,0,m) tmp[i][j] = source[i][j];
}

void load(){
	R.clear();
	FWD(i,0,n) FWD(j,0,m) source[i][j] = tmp[i][j];
}

void check(){
	FWD(i,0,n) FWD(j,0,m) if(source[i][j] != target[i][j]) return;
	printf("%d\n", (int)R.size()-1);
	for(PII r : R) printf("%d %d\n", r.x+1, r.y+1);
	exit(0);
}

void make(int x, int y){
	swap(source[R.back().x][R.back().y], source[x][y]);
	R.push_back(PII(x,y));
	a = x; b = y;
}

bool in(int x, int y){
	return 0 <= x && x < n && 0 <= y && y < m && !block[x][y];
}

void path(int x, int y){
	FWD(i,0,n) FWD(j,0,m) prv[i][j] = PII(-1,-1);
	queue<PII> Q;
	Q.push(PII(a,b));
	PII c = PII(x, y);
	while(!Q.empty()){
		x = Q.front().x;
		y = Q.front().y;
		Q.pop();
		FWD(d,0,8){
			int nx = x+dx[d]; int ny = y+dy[d];
			if(in(nx, ny) && prv[nx][ny] == PII(-1,-1)){
				prv[nx][ny] = PII(x,y);
				Q.push(PII(nx, ny));
			}
		}
	}
	stack<PII> S;
	while(c != PII(a,b)){
		S.push(c);
		c = prv[c.x][c.y];
	}
	while(!S.empty()){
		make(S.top().x, S.top().y);
		S.pop();
	}
}

void setp(int i, int j){
	path(i, j);
	PII s = PII(i,j);
	block[i][j] = 1;
	FWD(p,0,n) FWD(q,0,m) if(!block[p][q] && source[p][q] == target[i][j]) s = PII(p,q);
	block[i][j] = 0;
	if(s == PII(i,j)){
		printf("-1\n");
		exit(0);
	}
	for(;;){
		path(s.x, s.y);
		s = R[R.size()-2];
		if(PII(i,j) == s) break;
		block[s.x][s.y] = 1;
		path(i, j);
		block[s.x][s.y] = 0;
	}
	block[i][j] = 1;
}

int main(){
	scanf("%d %d", &n, &m);
	FWD(i,0,n) FWD(j,0,m) scanf("%d", &source[i][j]);
	FWD(i,0,n) FWD(j,0,m) scanf("%d", &target[i][j]);
	save();
	if(n == 1 || m == 1){
		FWD(s,0,max(n,m)){
			for(int d=-1; d<=1; d+=2){
				load();
				R.push_back(PII(n==1?0:s,n==1?s:0));
				for(int i=s+d;-1<i&&i<max(n,m);i+=d){
					make(n==1?0:i,n==1?i:0);
					check();
				}
			}
		}
		printf("-1\n");
		return 0;
	}
	FWD(i,0,n) FWD(j,0,m) if(source[i][j] == target[n-1][m-1]) a = i, b = j;
	R.push_back(PII(a,b));
	FWD(i,0,n-2)
		FWD(j,0,m)
			setp(i, j);
	FWD(j,0,m-1){
		setp(n-2,j);
		setp(n-1,j);
	}
	check();
	if(a == n-1)
		make(a-1, b);
	else
		make(a+1, b);
	check();
	printf("-1\n");
	return 0;
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemE {
    int n, m;
    int[][] cur;
    int[][] fin;

    List<Integer> ansX = new ArrayList<Integer>();
    List<Integer> ansY = new ArrayList<Integer>();

    void printMove(int a, int b) {
        System.out.println((a+1)+" "+(b+1));
    }

    void solveOne() {
        for (int from = 0; from < n+m-1; from++) {
            for (int to = 0; to < n+m-1; to++) {
                if (from == to) {
                    continue;
                }

                int[][] res = new int[n][m];
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        res[i][j] = cur[i][j];
                    }
                }

                if (from < to) {
                    for (int i = from; i < to; i++) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i+1];
                            res[0][i+1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i+1][0];
                            res[i+1][0] = tmp;
                        }
                    }
                } else {
                    for (int i = from; i > to; i--) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i-1];
                            res[0][i-1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i-1][0];
                            res[i-1][0] = tmp;
                        }
                    }
                }

                boolean ok = true;
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        if (res[i][j] != fin[i][j]) {
                            ok = false;
                        }
                    }
                }

                if (ok) {
                    System.out.println(Math.abs(from - to));

                    if (n == 1) {
                        printMove(0, from);
                    } else {
                        printMove(from, 0);
                    }

                    if (from < to) {
                        for (int i = from; i < to; i++) {
                            if (n == 1) {
                                printMove(0, i + 1);
                            } else {
                                printMove(i + 1, 0);
                            }
                        }
                    } else {
                        for (int i = from; i > to; i--) {
                            if (n == 1) {
                                printMove(0, i - 1);
                            } else {
                                printMove(i - 1, 0);
                            }
                        }
                    }

                    System.exit(0);
                }
            }
        }

        System.out.println(-1);
        System.exit(0);
    }

    void goTo(int i, int j) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        int tmp = cur[curI][curJ];
        cur[curI][curJ] = cur[i][j];
        cur[i][j] = tmp;

        ansX.add(i);
        ansY.add(j);
    }

    void makeSwap(int i1, int j1, int i2, int j2) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        if (curI == i1 && curJ == j1) {
            goTo(i2, j2);
            return;
        }

        if (curI == i2 && curJ == j2) {
            goTo(i1, j1);
            return;
        }

        int dst1 = Math.max(Math.abs(curI - i1), Math.abs(curJ - j1));
        int dst2 = Math.max(Math.abs(curI - i2), Math.abs(curJ - j2));

        if (dst1 == 1 && dst2 == 1) {
            goTo(i1, j1);
            goTo(i2, j2);
            goTo(curI, curJ);
            return;
        }

        int nextI = -1, nextJ = -1, bestDst = 100;
        for (int di = -1; di <= 1; di++) {
            for (int dj = -1; dj <= 1; dj++) {
                if (di == 0 && dj == 0) {
                    continue;
                }
                int candI = curI + di, candJ = curJ + dj;
                if (candI < 0 || candI >= n || candJ < 0 || candJ >= m) {
                    continue;
                }
                if (candI == i1 && candJ == j1) {
                    continue;
                }
                if (candI == i2 && candJ == j2) {
                    continue;
                }
                int dst = Math.max(Math.abs(candI - i1), Math.abs(candJ - j1)) + Math.max(Math.abs(candI - i2), Math.abs(candJ - j2));
                if (dst < bestDst) {
                    bestDst = dst;
                    nextI = candI;
                    nextJ = candJ;
                }
            }
        }

        goTo(nextI, nextJ);
        makeSwap(i1, j1, i2, j2);
        goTo(curI, curJ);
    }

    public void solve() throws Exception {
        Scanner sc = new Scanner(new InputStreamReader(System.in));
        n = sc.nextInt();
        m = sc.nextInt();
        cur = new int[n][m];
        fin = new int[n][m];
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                cur[i][j] = sc.nextInt();
            }
        }
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                fin[i][j] = sc.nextInt();
            }
        }

        if (n == 1 || m == 1) {
            solveOne();
        }

        ansX.add(0);
        ansY.add(0);

        boolean[][] fixed = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int ii = -1, jj = -1;
                for (int x = 0; x < n && ii == -1; x++) {
                    for (int y = 0; y < m && ii == -1; y++) {
                        if (!fixed[x][y] && cur[x][y] == fin[i][j]) {
                            ii = x;
                            jj = y;
                        }
                    }
                }
                if (ii == -1) {
                    System.out.println(-1);
                    System.exit(0);
                }
                while (ii != i || jj != j) {
                    int iii = -1, jjj = -1;
                    int bestDst = 1000;
                    for (int di = -1; di <= 1; di++) {
                        for (int dj = -1; dj <= 1; dj++) {
                            if (di == 0 && dj == 0) {
                                continue;
                            }
                            if (ii + di >= 0 && ii + di < n && jj + dj >= 0 && jj + dj < m && !fixed[ii + di][jj + dj] && Math.abs(i - ii - di) + Math.abs(j - jj - dj) < bestDst) {
                                bestDst = Math.abs(i - ii - di) + Math.abs(j - jj - dj);
                                iii = ii + di;
                                jjj = jj + dj;
                            }
                        }
                    }
                    makeSwap(ii, jj, iii, jjj);
                    ii = iii; jj = jjj;
                }
                fixed[i][j] = true;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (cur[i][j] != fin[i][j]) {
                    System.out.println(-2);
                    System.exit(0);
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append(ansX.size() - 1);
        sb.append("\n");
        for (int i=0; i < ansX.size(); i++) {
            sb.append((ansX.get(i) + 1) + " " + (ansY.get(i) + 1) + "\n");
        }

        System.out.print(sb.toString());
    }

    public static void main(String[] args) throws Exception {
        new ProblemE().solve();
   }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemE {
    int n, m;
    int[][] cur;
    int[][] fin;

    List<Integer> ansX = new ArrayList<Integer>();
    List<Integer> ansY = new ArrayList<Integer>();

    void printMove(int a, int b) {
        System.out.println((a+1)+" "+(b+1));
    }

    void solveOne() {
        for (int from = 0; from < n+m-1; from++) {
            for (int to = 0; to < n+m-1; to++) {
                if (from == to) {
                    continue;
                }

                int[][] res = new int[n][m];
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        res[i][j] = cur[i][j];
                    }
                }

                if (from < to) {
                    for (int i = from; i < to; i++) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i+1];
                            res[0][i+1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i+1][0];
                            res[i+1][0] = tmp;
                        }
                    }
                } else {
                    for (int i = from; i > to; i--) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i-1];
                            res[0][i-1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i-1][0];
                            res[i-1][0] = tmp;
                        }
                    }
                }

                boolean ok = true;
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        if (res[i][j] != fin[i][j]) {
                            ok = false;
                        }
                    }
                }

                if (ok) {
                    System.out.println(Math.abs(from - to));

                    if (n == 1) {
                        printMove(0, from);
                    } else {
                        printMove(from, 0);
                    }

                    if (from < to) {
                        for (int i = from; i < to; i++) {
                            if (n == 1) {
                                printMove(0, i + 1);
                            } else {
                                printMove(i + 1, 0);
                            }
                        }
                    } else {
                        for (int i = from; i > to; i--) {
                            if (n == 1) {
                                printMove(0, i - 1);
                            } else {
                                printMove(i - 1, 0);
                            }
                        }
                    }

                    System.exit(0);
                }
            }
        }

        System.out.println(-1);
        System.exit(0);
    }

    void goTo(int i, int j) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        int tmp = cur[curI][curJ];
        cur[curI][curJ] = cur[i][j];
        cur[i][j] = tmp;

        ansX.add(i);
        ansY.add(j);
    }

    void makeSwap(int i1, int j1, int i2, int j2) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        if (curI == i1 && curJ == j1) {
            goTo(i2, j2);
            return;
        }

        if (curI == i2 && curJ == j2) {
            goTo(i1, j1);
            return;
        }

        int dst1 = Math.max(Math.abs(curI - i1), Math.abs(curJ - j1));
        int dst2 = Math.max(Math.abs(curI - i2), Math.abs(curJ - j2));

        if (dst1 == 1 && dst2 == 1) {
            goTo(i1, j1);
            goTo(i2, j2);
            goTo(curI, curJ);
            return;
        }

        int nextI = -1, nextJ = -1, bestDst = 100;
        for (int di = -1; di <= 1; di++) {
            for (int dj = -1; dj <= 1; dj++) {
                if (di == 0 && dj == 0) {
                    continue;
                }
                int candI = curI + di, candJ = curJ + dj;
                if (candI < 0 || candI >= n || candJ < 0 || candJ >= m) {
                    continue;
                }
                if (candI == i1 && candJ == j1) {
                    continue;
                }
                if (candI == i2 && candJ == j2) {
                    continue;
                }
                int dst = Math.max(Math.abs(candI - i1), Math.abs(candJ - j1)) + Math.max(Math.abs(candI - i2), Math.abs(candJ - j2));
                if (dst < bestDst) {
                    bestDst = dst;
                    nextI = candI;
                    nextJ = candJ;
                }
            }
        }

        goTo(nextI, nextJ);
        makeSwap(i1, j1, i2, j2);
        goTo(curI, curJ);
    }

    public void solve() throws Exception {
        Scanner sc = new Scanner(new InputStreamReader(System.in));
        n = sc.nextInt();
        m = sc.nextInt();
        cur = new int[n][m];
        fin = new int[n][m];
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                cur[i][j] = sc.nextInt();
            }
        }
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                fin[i][j] = sc.nextInt();
            }
        }

        if (n == 1 || m == 1) {
            solveOne();
        }

        ansX.add(0);
        ansY.add(0);

        boolean[][] fixed = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int ii = -1, jj = -1;
                for (int x = 0; x < n && ii == -1; x++) {
                    for (int y = 0; y < m && ii == -1; y++) {
                        if (!fixed[x][y] && cur[x][y] == fin[i][j]) {
                            ii = x;
                            jj = y;
                        }
                    }
                }
                if (ii == -1) {
                    System.out.println(-1);
                    System.exit(0);
                }
                while (ii != i || jj != j) {
                    int iii = -1, jjj = -1;
                    int bestDst = 1000;
                    for (int di = -1; di <= 1; di++) {
                        for (int dj = -1; dj <= 1; dj++) {
                            if (di == 0 && dj == 0) {
                                continue;
                            }
                            if (ii + di >= 0 && ii + di < n && jj + dj >= 0 && jj + dj < m && !fixed[ii + di][jj + dj] && Math.abs(i - ii - di) + Math.abs(j - jj - dj) < bestDst) {
                                bestDst = Math.abs(i - ii - di) + Math.abs(j - jj - dj);
                                iii = ii + di;
                                jjj = jj + dj;
                            }
                        }
                    }
                    makeSwap(ii, jj, iii, jjj);
                    ii = iii; jj = jjj;
                }
                fixed[i][j] = true;
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append(ansX.size() - 1);
        sb.append("\n");
        for (int i=0; i < ansX.size(); i++) {
            sb.append((ansX.get(i) + 1) + " " + (ansY.get(i) + 1) + "\n");
        }

        System.out.print(sb.toString());
    }

    public static void main(String[] args) throws Exception {
        new ProblemE().solve();
   }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemE {
    int n, m;
    int[][] cur;
    int[][] fin;

    List<Integer> ansX = new ArrayList<Integer>();
    List<Integer> ansY = new ArrayList<Integer>();

    void printMove(int a, int b) {
        System.out.println((a+1)+" "+(b+1));
    }

    void solveOne() {
        for (int from = 0; from < n+m-1; from++) {
            for (int to = 0; to < n+m-1; to++) {
                if (from == to) {
                    continue;
                }

                int[][] res = new int[n][m];
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        res[i][j] = cur[i][j];
                    }
                }

                if (from < to) {
                    for (int i = from; i < to; i++) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i+1];
                            res[0][i+1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i+1][0];
                            res[i+1][0] = tmp;
                        }
                    }
                } else {
                    for (int i = from; i > to; i--) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i-1];
                            res[0][i-1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i-1][0];
                            res[i-1][0] = tmp;
                        }
                    }
                }

                boolean ok = true;
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        if (res[i][j] != fin[i][j]) {
                            ok = false;
                        }
                    }
                }

                if (ok) {
                    System.out.println(Math.abs(from - to));

                    if (n == 1) {
                        printMove(0, from);
                    } else {
                        printMove(from, 0);
                    }

                    if (from < to) {
                        for (int i = from; i < to; i++) {
                            if (n == 1) {
                                printMove(0, i + 1);
                            } else {
                                printMove(i + 1, 0);
                            }
                        }
                    } else {
                        for (int i = from; i > to; i--) {
                            if (n == 1) {
                                printMove(0, i - 1);
                            } else {
                                printMove(i - 1, 0);
                            }
                        }
                    }

                    System.exit(0);
                }
            }
        }

        System.out.println(-1);
        System.exit(0);
    }

    void goTo(int i, int j) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        int tmp = cur[curI][curJ];
        cur[curI][curJ] = cur[i][j];
        cur[i][j] = tmp;

        ansX.add(i);
        ansY.add(j);
    }

    void makeSwap(int i1, int j1, int i2, int j2) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        if (curI == i1 && curJ == j1) {
            goTo(i2, j2);
            return;
        }

        if (curI == i2 && curJ == j2) {
            goTo(i1, j1);
            return;
        }

        int dst1 = Math.max(Math.abs(curI - i1), Math.abs(curJ - j1));
        int dst2 = Math.max(Math.abs(curI - i2), Math.abs(curJ - j2));

        if (dst1 == 1 && dst2 == 1) {
            goTo(i1, j1);
            goTo(i2, j2);
            goTo(curI, curJ);
            return;
        }

        int nextI = -1, nextJ = -1, bestDst = 100;
        for (int di = -1; di <= 1; di++) {
            for (int dj = -1; dj <= 1; dj++) {
                if (di == 0 && dj == 0) {
                    continue;
                }
                int candI = nextI + di, candJ = nextJ + dj;
                if (candI < 0 || candI >= n || candJ < 0 || candJ >= m) {
                    continue;
                }
                int dst = Math.max(Math.abs(candI - i1), Math.abs(candJ - j1)) + Math.max(Math.abs(candI - i2), Math.abs(candJ - j2));
                if (dst < bestDst) {
                    bestDst = dst;
                    nextI = candI;
                    nextJ = candJ;
                }
            }
        }

        goTo(nextI, nextJ);
        makeSwap(i1, j1, i2, j2);
        goTo(curI, curJ);
    }

    public void solve() throws Exception {
        Scanner sc = new Scanner(new InputStreamReader(System.in));
        n = sc.nextInt();
        m = sc.nextInt();
        cur = new int[n][m];
        fin = new int[n][m];
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                cur[i][j] = sc.nextInt();
            }
        }
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                fin[i][j] = sc.nextInt();
            }
        }

        if (n == 1 || m == 1) {
            solveOne();
        }

        ansX.add(0);
        ansY.add(0);

        boolean[][] fixed = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int ii = -1, jj = -1;
                for (int x = 0; x < n && ii == -1; x++) {
                    for (int y = 0; y < m && ii == -1; y++) {
                        if (!fixed[x][y] && cur[x][y] == fin[i][j]) {
                            ii = x;
                            jj = y;
                        }
                    }
                }
                if (ii == -1) {
                    System.out.println(-1);
                    System.exit(0);
                }
                while (ii != i || jj != j) {
                    int iii = -1, jjj = -1;
                    int bestDst = 1000;
                    for (int di = -1; di <= 1; di++) {
                        for (int dj = -1; dj <= 1; dj++) {
                            if (di == 0 && dj == 0) {
                                continue;
                            }
                            if (ii + di >= 0 && ii + di < n && jj + dj >= 0 && jj + dj < m && !fixed[ii + di][jj + dj] && Math.abs(i - ii - di) + Math.abs(j - jj - dj) < bestDst) {
                                bestDst = Math.abs(i - ii - di) + Math.abs(j - jj - dj);
                                iii = ii + di;
                                jjj = jj + dj;
                            }
                        }
                    }
                    makeSwap(ii, jj, iii, jjj);
                    ii = iii; jj = jjj;
                }
                fixed[i][j] = true;
            }
        }

        System.out.println(ansX.size() - 1);
        for (int i=0; i < ansX.size(); i++) {
            printMove(ansX.get(i), ansY.get(i));
        }
    }

    public static void main(String[] args) throws Exception {
        new ProblemE().solve();
   }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class ProblemE {
    int n, m;
    int[][] cur;
    int[][] fin;

    List<Integer> ansX = new ArrayList<Integer>();
    List<Integer> ansY = new ArrayList<Integer>();

    void printMove(int a, int b) {
        System.out.println((a+1)+" "+(b+1));
    }

    void solveOne() {
        for (int from = 0; from < n+m-1; from++) {
            for (int to = 0; to < n+m-1; to++) {
                if (from == to) {
                    continue;
                }

                int[][] res = new int[n][m];
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        res[i][j] = cur[i][j];
                    }
                }

                if (from < to) {
                    for (int i = from; i < to; i++) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i+1];
                            res[0][i+1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i+1][0];
                            res[i+1][0] = tmp;
                        }
                    }
                } else {
                    for (int i = from; i > to; i--) {
                        if (n == 1) {
                            int tmp = res[0][i];
                            res[0][i] = res[0][i-1];
                            res[0][i-1] = tmp;
                        } else {
                            int tmp = res[i][0];
                            res[i][0] = res[i-1][0];
                            res[i-1][0] = tmp;
                        }
                    }
                }

                boolean ok = true;
                for (int i=0; i<n; i++) {
                    for (int j=0; j<m; j++) {
                        if (res[i][j] != fin[i][j]) {
                            ok = false;
                        }
                    }
                }

                if (ok) {
                    System.out.println(Math.abs(from - to));

                    if (n == 1) {
                        printMove(0, from);
                    } else {
                        printMove(from, 0);
                    }

                    if (from < to) {
                        for (int i = from; i < to; i++) {
                            if (n == 1) {
                                printMove(0, i + 1);
                            } else {
                                printMove(i + 1, 0);
                            }
                        }
                    } else {
                        for (int i = from; i > to; i--) {
                            if (n == 1) {
                                printMove(0, i - 1);
                            } else {
                                printMove(i - 1, 0);
                            }
                        }
                    }

                    System.exit(0);
                }
            }
        }

        System.out.println(-1);
        System.exit(0);
    }

    void goTo(int i, int j) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        int tmp = cur[curI][curJ];
        cur[curI][curJ] = cur[i][j];
        cur[i][j] = tmp;

        ansX.add(i);
        ansY.add(j);
    }

    void makeSwap(int i1, int j1, int i2, int j2) {
        int curI = ansX.get(ansX.size() - 1);
        int curJ = ansY.get(ansY.size() - 1);

        if (curI == i1 && curJ == j1) {
            goTo(i2, j2);
            return;
        }

        if (curI == i2 && curJ == j2) {
            goTo(i1, j1);
            return;
        }

        int dst1 = Math.max(Math.abs(curI - i1), Math.abs(curJ - j1));
        int dst2 = Math.max(Math.abs(curI - i2), Math.abs(curJ - j2));

        if (dst1 == 1 && dst2 == 1) {
            goTo(i1, j1);
            goTo(i2, j2);
            goTo(curI, curJ);
            return;
        }

        int nextI = -1, nextJ = -1, bestDst = 100;
        for (int di = -1; di <= 1; di++) {
            for (int dj = -1; dj <= 1; dj++) {
                if (di == 0 && dj == 0) {
                    continue;
                }
                int candI = curI + di, candJ = curJ + dj;
                if (candI < 0 || candI >= n || candJ < 0 || candJ >= m) {
                    continue;
                }
                int dst = Math.max(Math.abs(candI - i1), Math.abs(candJ - j1)) + Math.max(Math.abs(candI - i2), Math.abs(candJ - j2));
                if (dst < bestDst) {
                    bestDst = dst;
                    nextI = candI;
                    nextJ = candJ;
                }
            }
        }

        goTo(nextI, nextJ);
        makeSwap(i1, j1, i2, j2);
        goTo(curI, curJ);
    }

    public void solve() throws Exception {
        Scanner sc = new Scanner(new InputStreamReader(System.in));
        n = sc.nextInt();
        m = sc.nextInt();
        cur = new int[n][m];
        fin = new int[n][m];
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                cur[i][j] = sc.nextInt();
            }
        }
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                fin[i][j] = sc.nextInt();
            }
        }

        if (n == 1 || m == 1) {
            solveOne();
        }

        ansX.add(0);
        ansY.add(0);

        boolean[][] fixed = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int ii = -1, jj = -1;
                for (int x = 0; x < n && ii == -1; x++) {
                    for (int y = 0; y < m && ii == -1; y++) {
                        if (!fixed[x][y] && cur[x][y] == fin[i][j]) {
                            ii = x;
                            jj = y;
                        }
                    }
                }
                if (ii == -1) {
                    System.out.println(-1);
                    System.exit(0);
                }
                while (ii != i || jj != j) {
                    int iii = -1, jjj = -1;
                    int bestDst = 1000;
                    for (int di = -1; di <= 1; di++) {
                        for (int dj = -1; dj <= 1; dj++) {
                            if (di == 0 && dj == 0) {
                                continue;
                            }
                            if (ii + di >= 0 && ii + di < n && jj + dj >= 0 && jj + dj < m && !fixed[ii + di][jj + dj] && Math.abs(i - ii - di) + Math.abs(j - jj - dj) < bestDst) {
                                bestDst = Math.abs(i - ii - di) + Math.abs(j - jj - dj);
                                iii = ii + di;
                                jjj = jj + dj;
                            }
                        }
                    }
                    makeSwap(ii, jj, iii, jjj);
                    ii = iii; jj = jjj;
                }
                fixed[i][j] = true;
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append(ansX.size() - 1);
        sb.append("\n");
        for (int i=0; i < ansX.size(); i++) {
            sb.append((ansX.get(i) + 1) + " " + (ansY.get(i) + 1) + "\n");
        }

        System.out.print(sb.toString());
    }

    public static void main(String[] args) throws Exception {
        new ProblemE().solve();
   }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @author Pavel Mavrin
 */
public class E {

    private boolean[][] z;
    private boolean[][] zz;
    private int x,  y;
    private int n, m;
    private int[][] a;
    private int[][] b;
    private int[][] px;
    private int[][] py;

    private void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        a = new int[n][m];
        z = new boolean[n][m];
        zz = new boolean[n][m];
        px = new int[n][m];
        py = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = nextInt();
            }
        }
        b = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                b[i][j] = nextInt();
            }
        }
        if (n == 1) {
            for (int i = 0; i < m; i++) {
                x = 0;
                y = i;
                mn = 0;
                makeMove(x, y);
                for (int j = 0; j < m; j++) if (j != i) {
                    moveTo(0, j);
                    boolean ok = true;
                    for (int k = 0; k < m; k++) {
                        if (a[0][k] != b[0][k]) {
                            ok = false;
                        }
                    }
                    if (ok) {
                        out.println(mn - 1);
                        for (int t = 0; t < mn; t++) {
                            out.println((moves[0][t] + 1) + " " + (moves[1][t] + 1));
                        }
                        return;
                    }
                }
                moveTo(0, i);
            }
            out.println(-1);
            return;
        }
        if (m == 1) {
            for (int i = 0; i < n; i++) {
                x = i;
                y = 0;
                mn = 0;
                makeMove(x, y);
                for (int j = 0; j < n; j++) if (j != i) {
                    moveTo(j, 0);
                    boolean ok = true;
                    for (int k = 0; k < n; k++) {
                        if (a[k][0] != b[k][0]) {
                            ok = false;
                        }
                    }
                    if (ok) {
                        out.println(mn - 1);
                        for (int t = 0; t < mn; t++) {
                            out.println((moves[0][t] + 1) + " " + (moves[1][t] + 1));
                        }
                        return;
                    }
                }
                moveTo(i, 0);
            }
            out.println(-1);
            return;
        }
        x = -1;
        y = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j] == b[0][0]) {
                    x = i;
                    y = j;
                }
            }
        }
        if (x == -1) {
            out.println(-1);
            return;
        }
        makeMove(x, y);
        for (int d = n + m - 2; d > 0; d--) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (i + j == d) {
                            if (!put(i, j)) {
                            out.println(-1);
                            return;
                        }
                    }
                }
            }
        }
        out.println(mn - 1);
        for (int i = 0; i < mn; i++) {
            out.println((moves[0][i] + 1) + " " + (moves[1][i] + 1));
        }
    }

    private boolean put(int xx, int yy) {
        z[x][y] = true;
        int cx = -1;
        int cy = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!z[i][j] && a[i][j] == b[xx][yy]) {
                    cx = i;
                    cy = j;
                }
            }
        }
        if (cx == -1) {
            return false;
        }
        z[x][y] = false;
        while (cx != xx || cy != yy) {
            int dx = -1;
            int dy = -1;
            int min = 10000;
            for (int i = cx - 1; i <= cx + 1; i++) {
                for (int j = cy - 1; j <= cy + 1; j++) {
                    if (i >= 0 && i < n && j >= 0 && j < m && !z[i][j]) {
                        int d = Math.abs(xx - i) + Math.abs(yy - j);
                        if (d < min) {
                            min = d;
                            dx = i;
                            dy = j;
                        }
                    }
                }
            }
            z[cx][cy] = true;
            moveTo(dx, dy);
            z[cx][cy] = false;
            makeMove(cx, cy);
            cx = dx;
            cy = dy;
        }
        z[xx][yy] = true;
        return true;
    }

    int[] qx = new int[10000];
    int[] qy = new int[10000];

    private void moveTo(int dx, int dy) {
        for (boolean[] booleans : zz) {
            Arrays.fill(booleans, false);
        }
        zz[dx][dy] = true;
        int h = 0;
        int t = 1;
        qx[0] = dx;
        qy[0] = dy;
        while (t > h) {
            int i = qx[h];
            int j = qy[h];
            if (i == x && j == y) {
                while (x != dx || y != dy) {
                    makeMove(px[x][y], py[x][y]);
                }
                return;
            }
            h++;
            for (int ii = i - 1; ii <= i + 1; ii++) {
                for (int jj = j - 1; jj <= j + 1; jj++) {
                    if (ii >= 0 && ii < n && jj >= 0 && jj < m && !zz[ii][jj] && !z[ii][jj]) {
                        px[ii][jj] = i;
                        py[ii][jj] = j;
                        zz[ii][jj] = true;
                        qx[t] = ii;
                        qy[t] = jj;
                        t++;
                    }
                }
            }
        }
        throw new RuntimeException();
    }

    int mn = 0;
    int[][] moves = new int[2][1000000];

    private void makeMove(int xx, int yy) {
//        System.out.println(xx + " " + yy);
        moves[0][mn] = xx;
        moves[1][mn] = yy;
        int t = a[x][y];
        a[x][y] = a[xx][yy];
        a[xx][yy] = t;
        x = xx;
        y = yy;
        mn++;
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    PrintWriter out = new PrintWriter(System.out);

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) throws IOException {
        new E().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @author Pavel Mavrin
 */
public class E {

    private boolean[][] z;
    private boolean[][] zz;
    private int x, y;
    private int n, m;
    private int[][] a;
    private int[][] b;
    private int[][] px;
    private int[][] py;

    private void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        a = new int[n][m];
        z = new boolean[n][m];
        zz = new boolean[n][m];
        px = new int[n][m];
        py = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = nextInt();
            }
        }
        b = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                b[i][j] = nextInt();
            }
        }
        if (n == 1) {
            for (int i = 0; i < m; i++) {
                x = 0;
                y = i;
                mn = 0;
                makeMove(x, y);
                for (int j = 0; j < m; j++)
                    if (j != i) {
                        moveTo(0, j);
                        boolean ok = true;
                        for (int k = 0; k < m; k++) {
                            if (a[0][k] != b[0][k]) {
                                ok = false;
                            }
                        }
                        if (ok) {
                            out.println(mn - 1);
                            for (int t = 0; t < mn; t++) {
                                out.println((moves[0][t] + 1) + " " + (moves[1][t] + 1));
                            }
                            return;
                        }
                    }
                moveTo(0, i);
            }
            out.println(-1);
            return;
        }
        if (m == 1) {
            for (int i = 0; i < n; i++) {
                x = i;
                y = 0;
                mn = 0;
                makeMove(x, y);
                for (int j = 0; j < n; j++)
                    if (j != i) {
                        moveTo(j, 0);
                        boolean ok = true;
                        for (int k = 0; k < n; k++) {
                            if (a[k][0] != b[k][0]) {
                                ok = false;
                            }
                        }
                        if (ok) {
                            out.println(mn - 1);
                            for (int t = 0; t < mn; t++) {
                                out.println((moves[0][t] + 1) + " " + (moves[1][t] + 1));
                            }
                            return;
                        }
                    }
                moveTo(i, 0);
            }
            out.println(-1);
            return;
        }
        x = -1;
        y = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j] == b[0][0]) {
                    x = i;
                    y = j;
                }
            }
        }
        if (x == -1) {
            out.println(-1);
            return;
        }
        makeMove(x, y);
        for (int i = n - 1; i >= 2; i--) {
            for (int j = m - 1; j >= 0; j--) {
                if (!put(i, j)) {
                    out.println(-1);
                    return;
                }
            }
        }
        for (int j = m - 1; j >= 2; j--) {
            for (int i = 1; i >= 0; i--) {
                if (!put(i, j)) {
                    out.println(-1);
                    return;
                }
            }
        }
        for (int j = 1; j >= 0; j--) {
            for (int i = 1; i >= 0; i--) {
                if (i + j > 0 && !put(i, j)) {
                    out.println(-1);
                    return;
                }
            }
        }

        out.println(mn - 1);
        for (int i = 0; i < mn; i++) {
            out.println((moves[0][i] + 1) + " " + (moves[1][i] + 1));
        }
    }

    private boolean put(int xx, int yy) {
        z[x][y] = true;
        int cx = -1;
        int cy = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!z[i][j] && a[i][j] == b[xx][yy]) {
                    cx = i;
                    cy = j;
                }
            }
        }
        if (cx == -1) {
            return false;
        }
        z[x][y] = false;
//        System.out.println(xx + " " + yy);
        while (cx != xx || cy != yy) {
            int dx = -1;
            int dy = -1;
            int min = 10000;
            for (int i = cx - 1; i <= cx + 1; i++) {
                for (int j = cy - 1; j <= cy + 1; j++) {
                    if (i >= 0 && i < n && j >= 0 && j < m && !z[i][j]) {
                        int d = Math.abs(xx - i) + Math.abs(yy - j);
                        if (d < min) {
                            min = d;
                            dx = i;
                            dy = j;
                        }
                    }
                }
            }
            z[cx][cy] = true;
            moveTo(dx, dy);
            z[cx][cy] = false;
            makeMove(cx, cy);
            cx = dx;
            cy = dy;
        }
        z[xx][yy] = true;
        return true;
    }

    int[] qx = new int[10000];
    int[] qy = new int[10000];

    private void moveTo(int dx, int dy) {
        for (boolean[] booleans : zz) {
            Arrays.fill(booleans, false);
        }
        zz[dx][dy] = true;
        int h = 0;
        int t = 1;
        qx[0] = dx;
        qy[0] = dy;
        while (t > h) {
            int i = qx[h];
            int j = qy[h];
            if (i == x && j == y) {
                while (x != dx || y != dy) {
                    makeMove(px[x][y], py[x][y]);
                }
                return;
            }
            h++;
            for (int ii = i - 1; ii <= i + 1; ii++) {
                for (int jj = j - 1; jj <= j + 1; jj++) {
                    if (ii >= 0 && ii < n && jj >= 0 && jj < m && !zz[ii][jj] && !z[ii][jj]) {
                        px[ii][jj] = i;
                        py[ii][jj] = j;
                        zz[ii][jj] = true;
                        qx[t] = ii;
                        qy[t] = jj;
                        t++;
                    }
                }
            }
        }
        throw new RuntimeException();
    }

    int mn = 0;
    int[][] moves = new int[2][1000000];

    private void makeMove(int xx, int yy) {
//        System.out.println(xx + " " + yy);
        moves[0][mn] = xx;
        moves[1][mn] = yy;
        int t = a[x][y];
        a[x][y] = a[xx][yy];
        a[xx][yy] = t;
        x = xx;
        y = yy;
        mn++;
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    PrintWriter out = new PrintWriter(System.out);

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) throws IOException {
        new E().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @author Pavel Mavrin
 */
public class E {

    private boolean[][] z;
    private boolean[][] zz;
    private int x,  y;
    private int n, m;
    private int[][] a;
    private int[][] b;
    private int[][] px;
    private int[][] py;

    private void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        a = new int[n][m];
        z = new boolean[n][m];
        zz = new boolean[n][m];
        px = new int[n][m];
        py = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = nextInt();
            }
        }
        b = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                b[i][j] = nextInt();
            }
        }
        if (n == 1) {
            for (int i = 0; i < m; i++) {
                x = 0;
                y = i;
                mn = 0;
                moveTo(x, y);
                for (int j = 0; j < m; j++) if (j != i) {
                    moveTo(0, j);
                    boolean ok = true;
                    for (int k = 0; k < m; k++) {
                        if (a[0][k] != b[0][k]) {
                            ok = false;
                        }
                    }
                    if (ok) {
                        out.println(mn - 1);
                        for (int t = 0; t < mn; t++) {
                            out.println((moves[0][t] + 1) + " " + (moves[1][t] + 1));
                        }
                        return;
                    }
                }
            }
            out.println(-1);
            return;
        }
        if (m == 1) {
            for (int i = 0; i < n; i++) {
                x = i;
                y = 0;
                mn = 0;
                moveTo(x, y);
                for (int j = 0; j < n; j++) if (j != i) {
                    moveTo(j, 0);
                    boolean ok = true;
                    for (int k = 0; k < n; k++) {
                        if (a[k][0] != b[k][0]) {
                            ok = false;
                        }
                    }
                    if (ok) {
                        out.println(mn - 1);
                        for (int t = 0; t < mn; t++) {
                            out.println((moves[0][t] + 1) + " " + (moves[1][t] + 1));
                        }
                        return;
                    }
                }
            }
            out.println(-1);
            return;
        }
        x = -1;
        y = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (a[i][j] == b[0][0]) {
                    x = i;
                    y = j;
                }
            }
        }
        if (x == -1) {
            out.println(-1);
            return;
        }
        makeMove(x, y);
        for (int d = n + m - 2; d > 0; d--) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (i + j == d) {
                            if (!put(i, j)) {
                            out.println(-1);
                            return;
                        }
                    }
                }
            }
        }
        out.println(mn - 1);
        for (int i = 0; i < mn; i++) {
            out.println((moves[0][i] + 1) + " " + (moves[1][i] + 1));
        }
    }

    private boolean put(int xx, int yy) {
        z[x][y] = true;
        int cx = -1;
        int cy = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!z[i][j] && a[i][j] == b[xx][yy]) {
                    cx = i;
                    cy = j;
                }
            }
        }
        if (cx == -1) {
            return false;
        }
        z[x][y] = false;
        while (cx != xx || cy != yy) {
            int dx = -1;
            int dy = -1;
            int min = 10000;
            for (int i = cx - 1; i <= cx + 1; i++) {
                for (int j = cy - 1; j <= cy + 1; j++) {
                    if (i >= 0 && i < n && j >= 0 && j < m && !z[i][j]) {
                        int d = Math.abs(xx - i) + Math.abs(yy - j);
                        if (d < min) {
                            min = d;
                            dx = i;
                            dy = j;
                        }
                    }
                }
            }
            z[cx][cy] = true;
            moveTo(dx, dy);
            z[cx][cy] = false;
            makeMove(cx, cy);
            cx = dx;
            cy = dy;
        }
        z[xx][yy] = true;
        return true;
    }

    int[] qx = new int[10000];
    int[] qy = new int[10000];

    private void moveTo(int dx, int dy) {
        for (boolean[] booleans : zz) {
            Arrays.fill(booleans, false);
        }
        zz[dx][dy] = true;
        int h = 0;
        int t = 1;
        qx[0] = dx;
        qy[0] = dy;
        while (t > h) {
            int i = qx[h];
            int j = qy[h];
            if (i == x && j == y) {
                while (x != dx || y != dy) {
                    makeMove(px[x][y], py[x][y]);
                }
                return;
            }
            h++;
            for (int ii = i - 1; ii <= i + 1; ii++) {
                for (int jj = j - 1; jj <= j + 1; jj++) {
                    if (ii >= 0 && ii < n && jj >= 0 && jj < m && !zz[ii][jj] && !z[ii][jj]) {
                        px[ii][jj] = i;
                        py[ii][jj] = j;
                        zz[ii][jj] = true;
                        qx[t] = ii;
                        qy[t] = jj;
                        t++;
                    }
                }
            }
        }
        throw new RuntimeException();
    }

    int mn = 0;
    int[][] moves = new int[2][1000000];

    private void makeMove(int xx, int yy) {
//        System.out.println(xx + " " + yy);
        moves[0][mn] = xx;
        moves[1][mn] = yy;
        int t = a[x][y];
        a[x][y] = a[xx][yy];
        a[xx][yy] = t;
        x = xx;
        y = yy;
        mn++;
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    PrintWriter out = new PrintWriter(System.out);

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) throws IOException {
        new E().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


//package round270;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.Random;

public class E {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	int[] tor(int[][] a)
	{
		int[] t = new int[a.length];
		for(int i = 0;i < a.length;i++){
			t[i] = a[i][0];
		}
		return t;
	}

	void solve()
	{
		int n = ni(), m = ni();
		int[][] a = new int[n][];
		for(int i = 0;i < n;i++){
			a[i] = na(m);
		}
		int[][] b = new int[n][];
		for(int i = 0;i < n;i++){
			b[i] = na(m);
		}
		int[] f = new int[901];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				f[a[i][j]]++;
				f[b[i][j]]--;
			}
		}
		for(int i = 0;i < 901;i++){
			if(f[i] != 0){
				out.println(-1);
				return;
			}
		}

		if(n == 1){
			if(Arrays.equals(a[0], b[0])){
				out.println(0);
				return;
			}

			for(int i = 0;i < m;i++){
				for(int j = i;j < m;j++){
					int[] t = Arrays.copyOf(a[0], m);
					int x = t[i];
					for(int k = i;k < j;k++){
						t[k] = t[k+1];
					}
					t[j] = x;
					if(Arrays.equals(t, b[0])){
						out.println(j-i);
						for(int k = i;k <= j;k++){
							out.println(1 + " " + (k+1));
						}
						return;
					}
				}
			}
			for(int i = 0;i < m;i++){
				for(int j = 0;j < i;j++){
					int[] t = Arrays.copyOf(a[0], m);
					int x = t[i];
					for(int k = i;k > j;k--){
						t[k] = t[k-1];
					}
					t[j] = x;
					if(Arrays.equals(t, b[0])){
						out.println(i-j);
						for(int k = i;k >= j;k--){
							out.println(1 + " " + (k+1));
						}
						return;
					}
				}
			}
			out.println(-1);
			return;
		}

		if(m == 1){
			int[] tob = tor(b);
			if(Arrays.equals(tor(a), tob)){
				out.println(0);
				return;
			}

			for(int i = 0;i < n;i++){
				for(int j = i;j < n;j++){
					int[] t = tor(a);
					int x = t[i];
					for(int k = i;k < j;k++){
						t[k] = t[k+1];
					}
					t[j] = x;
					if(Arrays.equals(t, tob)){
						out.println(j-i);
						for(int k = i;k <= j;k++){
							out.println((k+1) + " " + 1);
						}
						return;
					}
				}
			}
			for(int i = 0;i < n;i++){
				for(int j = 0;j < i;j++){
					int[] t = tor(a);
					int x = t[i];
					for(int k = i;k > j;k--){
						t[k] = t[k-1];
					}
					t[j] = x;
					if(Arrays.equals(t, tob)){
						out.println(i-j);
						for(int k = i;k >= j;k--){
							out.println((k+1) + " " + 1);
						}
						return;
					}
				}
			}
			out.println(-1);
			return;
		}

		int[][] pa = new int[n*m][];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				pa[i*m+j] = new int[]{a[i][j], i, j};
			}
		}
		int[][] pb = new int[n*m][];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				pb[i*m+j] = new int[]{b[i][j], i, j};
			}
		}
		Arrays.sort(pa, new Comparator<int[]>() {
			public int compare(int[] a, int[] b) {
				return a[0] - b[0];
			}
		});
		Arrays.sort(pb, new Comparator<int[]>() {
			public int compare(int[] a, int[] b) {
				return a[0] - b[0];
			}
		});
		int hr = -1, hc = -1;
		int[][][] map = new int[n][m][];
		for(int i = 0;i < n*m;i++){
			map[pa[i][1]][pa[i][2]] = new int[]{pb[i][1], pb[i][2]};
			if(pb[i][1] == 0 && pb[i][2] == 0){
				hr = pa[i][1]; hc = pa[i][2];
			}
		}
		path = new int[1000000];
		pp = 0;
		path[pp++] = hr<<16|hc;
		solve(map);
		out.println(pp-1);
		for(int i = 0;i < pp;i++){
			out.println(((path[i]>>>16)+1) + " " + ((path[i]&65535)+1));
		}
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				if(map[i][j][0] == i && map[i][j][1] == j){
				}else{
					throw new RuntimeException();
				}
			}
		}
	}

	void solve(int[][][] map)
	{
		int n = map.length;
		int m = map[0].length;
		int[][][] imap = new int[n][m][];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				imap[map[i][j][0]][map[i][j][1]] = new int[]{i, j};
			}
		}
		boolean[][] wall = new boolean[n][m];

		for(int i = Math.max(n,m)-1;i >= 1;i--){
			if(i < n){
				for(int j = Math.min(i, m-1);j >= 0;j--){
					// (i,j)
					if(wall[i][j])continue;
					fill(i, j, map, imap, wall);
					wall[i][j] = true;
				}
			}

			if(i < m){
				for(int j = Math.min(n-1, i-1);j >= 0;j--){
					// (j,i)
					if(wall[j][i])continue;
					fill(j, i, map, imap, wall);
					wall[j][i] = true;
				}
			}
		}
	}

	int[] path;
	int pp;

	void fill(int gr, int gc, int[][][] map, int[][][] imap, boolean[][] wall)
	{
//		tr("go", gr, gc);
		int n = map.length;
		int m = map[0].length;
//		print(imap, wall, gr, gc);
		while(true){
			int hr = imap[0][0][0], hc = imap[0][0][1];
			int tr = imap[gr][gc][0], tc = imap[gr][gc][1];
			if(tr == gr && tc == gc)return;

			int dtr = Integer.signum(gr-tr), dtc = Integer.signum(gc-tc);
			int xdtr = -1, xdtc = -1;
			{
				int[][] DIR = {
						{2, 1, 0},
						{3, -99, 7},
						{4, 5, 6}
				};
				int basedir = DIR[dtr+1][dtc+1];
				int clo = 100;
				for(int k = -1;k <= 1;k++){
					for(int l = -1;l <= 1;l++){
						if(k == 0 && l == 0)continue;
						int ldir = DIR[k+1][l+1];
						int lclo = Math.min(Math.abs(ldir-basedir), Math.abs(ldir-basedir+8));
						lclo = Math.min(lclo, Math.abs(ldir-basedir-8));
						if(tr+k >= 0 && tr+k < n && tc+l >= 0 && tc+l < m && !wall[tr+k][tc+l]){
							if(lclo < clo){
								xdtr = k; xdtc = l;
								clo = lclo;
							}
						}
					}
				}
			}
			dtr = xdtr; dtc = xdtc;

			int thr = tr + dtr, thc = tc + dtc;
			if(wall[thr][thc]){
				throw new AssertionError(thr + " " + thc + " " + gr + " " + gc + " " + hr + " " + hc);
			}

			int nhr = -1, nhc = -1;
			if(hr == thr && hc == thc){
				nhr = tr; nhc = tc;
			}else{
				int dhr = Integer.signum(thr-hr), dhc = Integer.signum(thc-hc);
				int[][] DIR = {
						{2, 1, 0},
						{3, -99, 7},
						{4, 5, 6}
				};
				int basedir = DIR[dhr+1][dhc+1];
				int clo = 100;
				for(int k = -1;k <= 1;k++){
					for(int l = -1;l <= 1;l++){
						if(k == 0 && l == 0)continue;
						int ldir = DIR[k+1][l+1];
						int lclo = Math.min(Math.abs(ldir-basedir), Math.abs(ldir-basedir+8));
						lclo = Math.min(lclo, Math.abs(ldir-basedir-8));
						if(hr+k >= 0 && hr+k < n && hc+l >= 0 && hc+l < m && !wall[hr+k][hc+l] && !(tr == hr+k && tc == hc+l)){
							if(lclo < clo){
								nhr = hr + k; nhc = hc + l;
								clo = lclo;
							}
						}
					}
				}
			}
			if(nhr == -1)throw new AssertionError();
			int[] to = map[nhr][nhc];
			int[] dum = imap[to[0]][to[1]]; imap[to[0]][to[1]] = imap[0][0]; imap[0][0] = dum;
			map[nhr][nhc] = map[hr][hc]; map[hr][hc] = to;

			path[pp++] = nhr<<16|nhc;
			hr = nhr; hc = nhc;

//			print(imap, wall, gr, gc);
		}
	}

	void print(int[][][] imap, boolean[][] wall, int gr, int gc)
	{
		int n = imap.length, m = imap[0].length;
		for(int i = 0;i < n;i++){
			for(int j = 0;j < m;j++){
				if(wall[i][j]){
					out.print("#");
				}else if(i == imap[0][0][0] && j == imap[0][0][1]){
					out.print("o");
				}else if(i == imap[gr][gc][0] && j == imap[gr][gc][1]){
					out.print("t");
				}else if(i == gr && j == gc){
					out.print("g");
				}else{
					out.print(".");
				}
			}
			out.println();
		}
		out.println();
	}

	void run() throws Exception
	{
//		int n = 30, m = 2;
//		Random gen = new Random(1);
//		StringBuilder sb = new StringBuilder();
//		sb.append(n + " ");
//		sb.append(m + " ");
//		int[] a = new int[n*m];
//		for(int i = 0;i < n*m;i++)a[i] = i+1;
//		a = shuffle(a, gen);
//		for (int i = 0; i < n*m; i++) {
//			sb.append(a[i] + " ");
//		}
//		a = shuffle(a, gen);
//		for (int i = 0; i < n*m; i++) {
//			sb.append(a[i] + " ");
//		}
//		INPUT = sb.toString();

		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

	public static int[] shuffle(int[] a, Random gen){ for(int i = 0, n = a.length;i < n;i++){ int ind = gen.nextInt(n-i)+i; int d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; }
	public static void main(String[] args) throws Exception { new E().run(); }

	private byte[] inbuf = new byte[1024];
	private int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


import java.io.IOException;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Collection;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author George Marcus
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskE {
    int N;
    int M;
    int[][] A;
    int[][] target;
    boolean[][] banned;
    int ci, cj;
    ArrayList<Integer> moves;

    int[] di = { 0, 1, 0, -1, 1, -1, 1, -1 };
    int[] dj = { 1, 0, -1, 0, 1, -1, -1, 1 };

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        N = in.nextInt();
        M = in.nextInt();
        A = new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                A[i][j] = in.nextInt();
            }
        }
        target = new int[N][M];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                target[i][j] = in.nextInt();
            }
        }

        moves = new ArrayList<Integer>();

        // edge cases
        if (N == 1 || M == 1) {
            handleEdgeCase(out);
            return;
        }

        banned = new boolean[N][M];
        ci = -1;
        cj = -1;
        for (int i = 0; i < N && ci == -1; i++) {
            for (int j = 0; j < M && ci == -1; j++) {
                if (A[i][j] == target[0][0]) {
                    ci = i;
                    cj = j;
                }
            }
        }
        if (ci == -1) {
            out.println(-1);
            return;
        }
        moves.add(ci);
        moves.add(cj);
        for (int i = N - 1; i > 1; i--) {
            for (int j = 0; j < M; j++) {
                if (!placeCell(i, j)) {
                    out.println(-1);
                    return;
                }
            }
        }

        // 2xM
        for (int j = M - 1; j >= 0; j--) {
            for (int i = 1; i >= 0; i--) {
                if (!placeCell(i, j)) {
                    out.println(-1);
                    return;
                }
            }
        }

        out.println(moves.size() / 2 - 1);
        for (int i = 0; i < moves.size(); i += 2) {
            int pi = moves.get(i) + 1;
            int pj = moves.get(i + 1) + 1;
            out.println(pi + " " + pj);
        }
    }

    private void handleEdgeCase(PrintWriter out) {
        boolean rev = false;
        if (M == 1) {
            int[][] B = new int[M][N];
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    B[j][i] = A[i][j];
                }
            }
            A = B;

            int[][] BT = new int[M][N];
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < M; j++) {
                    BT[j][i] = target[i][j];
                }
            }
            target = BT;

            rev = true;
            int aux = N; N = M; M = aux;
        }

        boolean ok = false;
        for (int a = 0; a < M && !ok; a++) {
            for (int b = 0; b < M && !ok; b++) {
                if (valid(a, b)) {
                    ok = true;
                }
            }
        }

        if (!ok) {
            out.println(-1);
            return;
        }

        out.println(moves.size() / 2 - 1);
        for (int i = 0; i < moves.size(); i += 2) {
            int pi = moves.get(i) + 1;
            int pj = moves.get(i + 1) + 1;
            if (!rev) {
                out.println(pi + " " + pj);
            }
            else {
                out.println(pj + " " + pi);
            }
        }
    }

    private boolean valid(int a, int b) {
        moves.clear();
        if (a == b) {
            return false;
        }

        int dir = a > b ? -1 : 1;
        int[] start = new int[M];
        for (int i = 0; i < M; i++) {
            start[i] = A[0][i];
        }
        moves.add(0); moves.add(a);
        for (int i = a; i != b; i += dir) {
            int aux = start[i];
            start[i] = start[i + dir];
            start[i + dir] = aux;
            moves.add(0); moves.add(i + dir);
        }

        boolean ok = true;
        for (int i = 0; i < M; i++) {
            if (start[i] != target[0][i]) {
                ok = false;
            }
        }

        return ok;
    }

    private boolean placeCell(int ti, int tj) {
        if (ti == 0 && tj == 0) {
            return true;
        }

        int t = target[ti][tj];
        int pi = -1;
        int pj = -1;
        for (int i = 0; i < N && pi == -1; i++) {
            for (int j = 0; j < M && pi == -1; j++) {
                if (!banned[i][j] && (i != ci || j != cj) && A[i][j] == t) {
                    pi = i;
                    pj = j;
                }
            }
        }

        if (pi == -1) {
            return false;
        }

        int[][] simplePath = computePath(pi, pj, ti, tj);
        for (int p = 1; p < simplePath.length; p++) {
            int previ = simplePath[p - 1][0];
            int prevj = simplePath[p - 1][1];
            int crti = simplePath[p][0];
            int crtj = simplePath[p][1];
            if (ci != previ || cj != prevj) {
                banned[previ][prevj] = true;
                int[][] path = computePath(ci, cj, crti, crtj);
                executeMoves(path);
                banned[previ][prevj] = false;
                int aux = A[ci][cj];
                A[ci][cj] = A[previ][prevj];
                A[previ][prevj] = aux;
                ci = previ;
                cj = prevj;
                moves.add(ci);
                moves.add(cj);
            }
            else {
                int aux = A[ci][cj];
                A[ci][cj] = A[crti][crtj];
                A[crti][crtj] = aux;
                ci = crti;
                cj = crtj;
                moves.add(ci);
                moves.add(cj);
            }
        }

        banned[ti][tj] = true;
        return true;
    }

    private void executeMoves(int[][] path) {
        if (ci != path[0][0] || cj != path[0][1]) {
            throw new RuntimeException();
        }

        int K = path.length;
        for (int m = 0; m < K - 1; m++) {
            int crti = path[m][0];
            int crtj = path[m][1];
            int nexti = path[m + 1][0];
            int nextj = path[m + 1][1];
            int aux = A[crti][crtj];
            A[crti][crtj] = A[nexti][nextj];
            A[nexti][nextj] = aux;
            moves.add(nexti);
            moves.add(nextj);
        }

        ci = path[K - 1][0];
        cj = path[K - 1][1];
    }

    private int[][] computePath(int si, int sj, int ti, int tj) {
        Queue<Integer> Q = new LinkedList<Integer>();
        boolean[][] v = new boolean[N][M];
        int[][] prev = new int[N][M];

        Q.add(si); Q.add(sj);
        v[si][sj] = true;
        prev[si][sj] = -1;
        while (!Q.isEmpty()) {
            int pi = Q.remove();
            int pj = Q.remove();

            for (int d = 0; d < 8; d++) {
                int ni = pi + di[d];
                int nj = pj + dj[d];

                if (ni >= 0 && ni < N && nj >= 0 && nj < M && !banned[ni][nj] && !v[ni][nj]) {
                    Q.add(ni); Q.add(nj);
                    v[ni][nj] = true;
                    prev[ni][nj] = pi * M + pj;
                }
            }
        }

        if (!v[ti][tj]) {
            throw new RuntimeException();
        }

        int i = ti;
        int j = tj;
        int cnt = 0;
        while (i != si || j != sj) {
            cnt++;
            int ni = prev[i][j] / M;
            int nj = prev[i][j] % M;
            i = ni;
            j = nj;
        }

        int K = cnt + 1;
        int[][] ret = new int[K][2];
        cnt = K - 1;
        i = ti;
        j = tj;
        while (i != si || j != sj) {
            ret[cnt][0] = i;
            ret[cnt][1] = j;
            int ni = prev[i][j] / M;
            int nj = prev[i][j] % M;
            i = ni;
            j = nj;
            cnt--;
        }
        ret[cnt][0] = si;
        ret[cnt][1] = sj;

        return ret;
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt() {
        return Integer.parseInt(nextString());
    }

    public String nextString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));

        return res.toString();
    }

    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

}