#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
int tmp[33][33];
int source[33][33];
int target[33][33];
pair<int, int> last[33][33];
bool block[33][33];
vector<pair<int, int> > R;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
void Save() {
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++) tmp[i][j] = source[i][j];
}
void Load() {
  R.clear();
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++) source[i][j] = tmp[i][j];
}
void Check() {
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++)
      if (source[i][j] != target[i][j]) return;
  printf("%d\n", (int)R.size() - 1);
  for (pair<int, int> r : R) printf("%d %d\n", r.first + 1, r.second + 1);
  exit(0);
}
void Make(int first, int second) {
  swap(source[R.back().first][R.back().second], source[first][second]);
  R.push_back(make_pair(first, second));
  a = first;
  b = second;
}
bool in(int first, int second) {
  return 0 <= first && first < n && 0 <= second && second < m &&
         !block[first][second];
}
void Path(int first, int second) {
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++) last[i][j] = make_pair(-1, -1);
  queue<pair<int, int> > Q;
  Q.push(make_pair(a, b));
  pair<int, int> c = make_pair(first, second);
  while (!Q.empty()) {
    int first = Q.front().first;
    int second = Q.front().second;
    Q.pop();
    for (int _n(8), d(0); d < _n; d++) {
      int nx = first + dx[d];
      int ny = second + dy[d];
      if (in(nx, ny) && last[nx][ny] == make_pair(-1, -1)) {
        last[nx][ny] = make_pair(first, second);
        Q.push(make_pair(nx, ny));
      }
    }
  }
  vector<pair<int, int> > S;
  while (c != make_pair(a, b)) {
    S.push_back(c);
    c = last[c.first][c.second];
  }
  while (!S.empty()) {
    Make(S.back().first, S.back().second);
    S.pop_back();
  }
}
void Setp(int i, int j) {
  Path(i, j);
  pair<int, int> s = make_pair(i, j);
  block[i][j] = 1;
  for (int _n(n), p(0); p < _n; p++)
    for (int _n(m), q(0); q < _n; q++) {
      if (!block[p][q] && source[p][q] == target[i][j]) s = make_pair(p, q);
    }
  block[i][j] = 0;
  if (s == make_pair(i, j)) {
    cout << -1 << endl;
    exit(0);
  }
  for (;;) {
    Path(s.first, s.second);
    s = R[R.size() - 2];
    if (make_pair(i, j) == s) break;
    block[s.first][s.second] = 1;
    Path(i, j);
    block[s.first][s.second] = 0;
  }
  block[i][j] = 1;
}
int main() {
  cin >> n >> m;
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++) cin >> source[i][j];
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++) cin >> target[i][j];
  Save();
  if (n == 1 || m == 1) {
    for (int _n(max(n, m)), s(0); s < _n; s++) {
      for (int d = -1; d <= 1; d += 2) {
        Load();
        R.push_back(make_pair(n == 1 ? 0 : s, n == 1 ? s : 0));
        for (int i = s + d; 0 <= i && i < max(n, m); i += d) {
          Make(n == 1 ? 0 : i, n == 1 ? i : 0);
          Check();
        }
      }
    }
    cout << -1 << endl;
    return 0;
  }
  for (int _n(n), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++)
      if (source[i][j] == target[n - 1][m - 1]) a = i, b = j;
  R.push_back(make_pair(a, b));
  for (int _n(n - 2), i(0); i < _n; i++)
    for (int _n(m), j(0); j < _n; j++) Setp(i, j);
  for (int _n(m - 1), j(0); j < _n; j++) {
    Setp(n - 2, j);
    Setp(n - 1, j);
  }
  Check();
  if (a == n - 1)
    Make(a - 1, b);
  else
    Make(a + 1, b);
  Check();
  cout << -1 << endl;
  return 0;
}
