#include <bits/stdc++.h>
const int MAXN = 33;
const int MAXV = 909;
int n, m;
int arr[2][MAXN][MAXN];
bool solved() {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (arr[0][i][j] != arr[1][i][j]) return false;
  return true;
}
int x, y;
std::vector<std::pair<int, int>> as;
int flip_cnt = 0;
void flip() {
  int siz = std::max(n, m);
  for (int i = 0; i < 2; ++i)
    for (int j = 1; j <= siz; ++j)
      for (int k = j + 1; k <= siz; ++k) std::swap(arr[i][j][k], arr[i][k][j]);
  std::swap(n, m);
  std::swap(x, y);
  flip_cnt += 1;
}
void write_as(int nx, int ny) {
  if (flip_cnt & 1)
    as.push_back({ny, nx});
  else
    as.push_back({nx, ny});
}
void STEP(int dx, int dy) {
  int nx = x + dx, ny = y + dy;
  std::swap(arr[0][x][y], arr[0][nx][ny]);
  write_as(nx, ny);
  x = nx, y = ny;
}
bool solve_line_left() {
  int bak[MAXN];
  for (int i = 1; i <= m; ++i) bak[i] = arr[0][1][i];
  for (int i = 1; i < m; ++i) {
    as.clear();
    x = 1, y = i;
    write_as(x, y);
    for (int j = i + 1; j <= m; ++j) {
      STEP(0, 1);
      if (solved()) return true;
    }
    for (int j = 1; j <= m; ++j) arr[0][1][j] = bak[j];
  }
  return false;
}
bool solve_line() {
  if (solve_line_left()) return true;
  for (int k = 0; k < 2; ++k)
    for (int i = 1, j = m; i < j; ++i, --j)
      std::swap(arr[k][1][i], arr[k][1][j]);
  if (not solve_line_left()) return false;
  if (flip_cnt & 1)
    for (auto &p : as) p.first = m + 1 - p.first;
  else
    for (auto &p : as) p.second = m + 1 - p.second;
  return true;
}
void jizz() {
  x = -1, y = -1;
  for (int i = 1; x == -1 and i <= n; ++i)
    for (int j = 1; y == -1 and j <= m; ++j)
      if (arr[0][i][j] == arr[1][1][1]) x = i, y = j;
  write_as(x, y);
  while (n > 2 or m > 2) {
    while (x < n) STEP(1, 0);
    while (y < m) STEP(0, 1);
    if (n < m) flip();
    int tx = n;
    for (int ty = m; ty >= 1; --ty) {
      int fx = -1, fy = -1;
      for (int i = 1; fx == -1 and i <= n; ++i)
        for (int j = 1; fx == -1 and j <= m; ++j)
          if (arr[0][i][j] == arr[1][tx][ty]) fx = i, fy = j;
      int x_bfr = fx == 1 ? 1 : -1;
      if (fy == ty) {
        while (fx + 1 < x) STEP(-1, 0);
        if (x_bfr == -1) {
          STEP(-1, 0);
          fx += 1;
        }
      } else {
        int fix_dir = fy < ty ? 1 : -1;
        while (fx < x) STEP(-1, 0);
        while (fy != y) STEP(0, -fix_dir);
        fy += fix_dir;
        while (fy != ty) {
          STEP(x_bfr, fix_dir);
          STEP(-x_bfr, fix_dir);
          STEP(0, -fix_dir);
          fy += fix_dir;
        }
        STEP(x_bfr, fix_dir);
      }
      int y_bfr = fy == 1 ? 1 : -1;
      while (fx != tx) {
        if (x < fx) {
          STEP(1, y_bfr);
          STEP(1, -y_bfr);
        }
        STEP(-1, 0);
        fx += 1;
      }
      if (ty != 1) STEP(1, -1);
    }
    n -= 1;
  }
  while (not solved()) {
    int dx = x == 1 ? 1 : -1, dy = y == 1 ? 1 : -1;
    int tmp = rand() % 3;
    STEP(tmp == 1 ? 0 : dx, tmp == 2 ? 0 : dy);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  std::vector<int> cnt(MAXV);
  for (int i = 0; i < 2; ++i) {
    for (int j = 1; j <= n; ++j) {
      for (int k = 1; k <= m; ++k) {
        int t;
        scanf("%d", &t);
        arr[i][j][k] = t, cnt[t] += i ? 1 : -1;
      }
    }
  }
  bool ok = true;
  for (int c : cnt) ok &= c == 0;
  if (not ok) {
    ;
  } else if (n == 1 or m == 1) {
    if (m == 1) flip();
    ok = solve_line();
  } else {
    jizz();
  }
  if (ok) {
    printf("%u\n", as.size() - 1);
    for (auto p : as) printf("%d %d\n", p.first, p.second);
  } else {
    puts("-1");
  }
  return 0;
}
