#include <bits/stdc++.h>
using namespace std;
int d[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
               {0, 1},   {1, -1}, {1, 0},  {1, 1}};
int n, m, cnt, a[40][40], b[40][40];
struct point {
  int x, y;
  point(int _x = -1, int _y = -1) {
    x = _x;
    y = _y;
  }
} p[1000005], target, cur;
bool check(int a[40][40]) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] != b[i][j]) return false;
  return true;
}
void output() {
  printf("%d\n", cnt - 1);
  for (int i = 0; i < cnt; i++) printf("%d %d\n", p[i].x + 1, p[i].y + 1);
}
inline void GoDirection(int k) {
  int dx = cur.x + d[k][0], dy = cur.y + d[k][1];
  swap(a[dx][dy], a[cur.x][cur.y]);
  cur = point(dx, dy);
  p[cnt++] = point(dx, dy);
}
point Find() {
  int val = b[target.x][target.y];
  for (int i = target.x - 1; i >= 0; i--) {
    if (i == cur.x && target.y == cur.y) continue;
    if (a[i][target.y] == val) return point(i, target.y);
  }
  for (int j = target.y - 1; j >= 0; j--)
    for (int i = n - 1; i >= 0; i--) {
      if (i == cur.x && j == cur.y) continue;
      if (a[i][j] == val) return point(i, j);
    }
}
void GoToPoint(int x, int y) {
  if (cur.x == x && cur.y == y) return;
  if (cur.y == y) {
    while (cur.x < x) GoDirection(6);
    while (cur.x > x) GoDirection(1);
  } else {
    while (cur.x > 0) GoDirection(1);
    while (cur.y > y) GoDirection(3);
    while (cur.y < y) GoDirection(4);
    while (cur.x < x) GoDirection(6);
  }
}
void GoDown(point q) {
  if (q.x == target.x && q.y == target.y) return;
  if (q.y > 0) {
    while (q.x < target.x) {
      GoDirection(5);
      GoDirection(7);
      GoDirection(1);
      q.x++;
    }
  } else {
    while (q.x < target.x) {
      GoDirection(7);
      GoDirection(5);
      GoDirection(1);
      GoDirection(4);
      GoDirection(0);
      GoDirection(6);
      q.x++;
    }
  }
}
void solve(point q) {
  if (q.y == target.y) {
    if (cur.y == target.y && cur.x > q.x) {
      GoToPoint(q.x, q.y);
      q.x++;
    } else if (q.x == 0) {
      GoToPoint(0, q.y - 1);
      GoDirection(7);
      GoDirection(1);
      q.x++;
    } else
      GoToPoint(q.x - 1, q.y);
    GoDown(q);
  } else {
    if (q.x > 0) {
      if (q.y == cur.y && cur.x > q.x)
        GoToPoint(q.x + 1, q.y);
      else {
        GoToPoint(q.x, q.y);
        q.x--;
      }
      while (q.x > 0) {
        if (q.y > 0) {
          GoDirection(0);
          GoDirection(2);
          GoDirection(6);
        } else {
          GoDirection(2);
          GoDirection(0);
          GoDirection(6);
          GoDirection(4);
          GoDirection(5);
          GoDirection(1);
        }
        q.x--;
      }
    }
    if (cur.y < q.y)
      GoToPoint(0, q.y - 1);
    else if (cur.y == q.y) {
      GoToPoint(1, q.y);
      GoDirection(2);
      GoDirection(3);
      q.y++;
    } else {
      GoToPoint(0, q.y);
      q.y++;
    }
    while (q.y < target.y) {
      GoDirection(7);
      GoDirection(2);
      GoDirection(3);
      q.y++;
    }
    if (q.x == target.x) return;
    GoDirection(7);
    GoDirection(1);
    q.x++;
    GoDown(q);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  int c[1005];
  memset(c, 0, sizeof(c));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      c[a[i][j]]++;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      scanf("%d", &b[i][j]);
      c[b[i][j]]--;
    }
  for (int i = 1; i <= 900; i++)
    if (c[i] != 0) {
      printf("-1\n");
      return 0;
    }
  if (n == 1 || m == 1) {
    if (check(a)) {
      printf("2\n");
      if (n == 1)
        printf("1 1\n1 2\n1 1\n");
      else
        printf("1 1\n2 1\n1 1\n");
      return 0;
    }
    int c[40][40];
    bool ok = false;
    if (n == 1) {
      for (int i = 0; i < m; i++) {
        cnt = 0;
        p[cnt++] = point(0, i);
        for (int j = 0; j < m; j++) c[0][j] = a[0][j];
        for (int j = i - 1; j >= 0; j--) {
          swap(c[0][j], c[0][j + 1]);
          p[cnt++] = point(0, j);
          if (check(c)) {
            ok = true;
            break;
          }
        }
        if (ok) break;
      }
      if (ok) {
        output();
        return 0;
      }
      for (int i = 0; i < m; i++) {
        cnt = 0;
        p[cnt++] = point(0, i);
        for (int j = 0; j < m; j++) c[0][j] = a[0][j];
        for (int j = i + 1; j < m; j++) {
          swap(c[0][j - 1], c[0][j]);
          p[cnt++] = point(0, j);
          if (check(c)) {
            ok = true;
            break;
          }
        }
        if (ok) break;
      }
      if (ok)
        output();
      else
        printf("-1\n");
    } else {
      for (int i = 0; i < n; i++) {
        cnt = 0;
        p[cnt++] = point(i, 0);
        for (int j = 0; j < n; j++) c[j][0] = a[j][0];
        for (int j = i - 1; j >= 0; j--) {
          swap(c[j][0], c[j + 1][0]);
          p[cnt++] = point(j, 0);
          if (check(c)) {
            ok = true;
            break;
          }
        }
        if (ok) break;
      }
      if (ok) {
        output();
        return 0;
      }
      for (int i = 0; i < n; i++) {
        cnt = 0;
        p[cnt++] = point(i, 0);
        for (int j = 0; j < n; j++) c[j][0] = a[j][0];
        for (int j = i + 1; j < n; j++) {
          swap(c[j - 1][0], c[j][0]);
          p[cnt++] = point(j, 0);
          if (check(c)) {
            ok = true;
            break;
          }
        }
        if (ok) break;
      }
      if (ok)
        output();
      else
        printf("-1\n");
    }
  } else {
    cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (a[i][j] == b[0][0]) {
          cur = point(i, j);
          break;
        }
      }
      if (cur.x >= 0) break;
    }
    p[cnt++] = cur;
    target = point(n - 1, m - 1);
    while (1) {
      if (a[target.x][target.y] != b[target.x][target.y] ||
          (target.x == cur.x && target.y == cur.y))
        solve(Find());
      target.x--;
      if (target.x == -1) {
        target.x = n - 1;
        target.y--;
      }
      if (target.y == 0 && target.x == 0) break;
    }
    output();
  }
  return 0;
}
