#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[40][40];
int b[40][40];
void flip() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      swap(a[i][j], a[j][i]);
      swap(b[i][j], b[j][i]);
    }
  }
  swap(n, m);
}
vector<int> ans;
void pb(int x, int y) {
  ans.push_back(x);
  ans.push_back(y);
}
int sx, sy;
void move(int tx, int ty) {
  pb(tx, ty);
  swap(a[sx][sy], a[tx][ty]);
  sx = tx;
  sy = ty;
}
void movex(int tx) {
  while (sx < tx) {
    move(sx + 1, sy);
  }
}
void movex_(int tx) {
  while (sx > tx) {
    move(sx - 1, sy);
  }
}
void movey(int ty) {
  while (sy < ty) {
    move(sx, sy + 1);
  }
}
void movey_(int ty) {
  while (sy > ty) {
    move(sx, sy - 1);
  }
}
int hsrc(int r, int c, int tar = -1) {
  if (tar == -1) {
    tar = b[r][c];
  }
  for (int k = 0; k < c; k++) {
    if (a[r][k] == tar) {
      if (k == c - 1) {
        move(sx, sy - 1);
        return 1;
      }
      movey_(k);
      if (r) {
        while (sy < c - 1) {
          move(sx - 1, sy + 1);
          move(sx + 1, sy + 1);
          move(sx, sy - 1);
        }
      } else {
        while (sy < c - 1) {
          move(sx + 1, sy + 1);
          move(sx - 1, sy + 1);
          move(sx, sy - 1);
        }
      }
      return 1;
    }
  }
  return 0;
}
int hsrc_(int r, int c, int tar = -1) {
  if (tar == -1) {
    tar = b[r][c];
  }
  for (int k = m - 1; k > c; k--) {
    if (a[r][k] == tar) {
      if (k == c + 1) {
        move(sx, sy + 1);
        return 1;
      }
      movey(k);
      if (r) {
        while (sy > c + 1) {
          move(sx - 1, sy - 1);
          move(sx + 1, sy - 1);
          move(sx, sy + 1);
        }
      } else {
        while (sy > c + 1) {
          move(sx + 1, sy - 1);
          move(sx - 1, sy - 1);
          move(sx, sy + 1);
        }
      }
      return 1;
    }
  }
  return 0;
}
void gox(int r, int c) {
  if (hsrc(r, c)) return;
  int i, j;
  for (i = 0; i < r; i++) {
    for (j = 0; j < m; j++) {
      if (a[i][j] == b[r][c]) {
        goto F;
      }
    }
  }
F:;
  if (c < j) {
    movex_(i);
    hsrc_(i, c, b[r][c]);
    move(sx + 1, sy - 1);
    move(sx - 1, sy);
  } else if (c > j) {
    movex_(i);
    hsrc(i, c, b[r][c]);
    move(sx + 1, sy + 1);
    move(sx - 1, sy);
  } else {
    movex_(i);
  }
  if (i == r - 1) {
  } else if (c) {
    while (sx < r - 1) {
      move(sx + 1, sy - 1);
      move(sx + 1, sy + 1);
      move(sx - 1, sy);
    }
  } else {
    while (sx < r - 1) {
      move(sx + 1, sy + 1);
      move(sx + 1, sy - 1);
      move(sx - 1, sy);
    }
  }
  if (c) {
    movey_(c - 1);
    movex(r);
  }
}
void goy(int c) {
  if (a[0][c] == b[1][c]) {
    move(sx - 1, sy);
  } else if (hsrc(1, c)) {
    move(sx - 1, sy + 1);
  } else if (a[0][c - 1] == b[1][c]) {
    move(sx - 1, sy - 1);
    move(sx, sy + 1);
  } else {
    move(sx - 1, sy - 1);
    hsrc(0, c - 1, b[1][c]);
    move(sx + 1, sy + 1);
    move(sx, sy + 1);
    move(sx - 1, sy - 1);
    move(sx, sy + 1);
  }
  if (hsrc(0, c)) {
    move(sx + 1, sy);
  } else if (a[1][c - 1] == b[0][c]) {
    move(sx + 1, sy - 1);
  } else {
    move(sx + 1, sy - 1);
    hsrc(1, c - 1, b[0][c]);
    move(sx - 1, sy + 1);
    move(sx, sy + 1);
    move(sx + 1, sy - 1);
  }
}
void print(int fl) {
  printf("%d\n", ans.size() / 2 - 1);
  for (int i = 0; i < ans.size() / 2; i++) {
    int x = ans[i * 2], y = ans[i * 2 + 1];
    if (fl) {
      swap(x, y);
    }
    printf("%d %d\n", x + 1, y + 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  vector<int> va, vb;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
      va.push_back(a[i][j]);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &b[i][j]);
      vb.push_back(b[i][j]);
    }
  }
  sort(va.begin(), va.end());
  sort(vb.begin(), vb.end());
  for (int i = 0; i < n * m; i++) {
    if (va[i] != vb[i]) {
      puts("-1");
      return 0;
    }
  }
  int fl = 0;
  if (n > m) {
    fl = 1;
    flip();
  }
  if (n == 1) {
    vector<int> vaa[60];
    vector<int> vbb[60];
    for (int i = 0; i < m; i++) {
      vaa[i] = vector<int>(a[0], a[0] + m);
      vaa[i].erase(vaa[i].begin() + i);
      vbb[i] = vector<int>(b[0], b[0] + m);
      vbb[i].erase(vbb[i].begin() + i);
    }
    if (vaa[0] == vbb[0]) {
      pb(0, 0);
      pb(0, 1);
      pb(0, 0);
      print(fl);
      return 0;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        if (vaa[i] == vbb[j]) {
          int x = i, y = j;
          if (x < y) {
            while (x <= y) {
              pb(0, x);
              ++x;
            }
          } else {
            while (x >= y) {
              pb(0, x);
              --x;
            }
          }
          print(fl);
          return 0;
        }
      }
    }
    puts("-1");
    return 0;
  }
  if (fl) {
    fl = 0;
    flip();
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == b[0][0]) {
        sx = i;
        sy = j;
      }
    }
  }
  pb(sx, sy);
  movex(n - 1);
  movey(m - 1);
  for (int i = n - 1; i > 1; i--) {
    for (int j = m - 1; j >= 0; j--) {
      gox(i, j);
    }
    movey(m - 1);
  }
  for (int j = m - 1; j >= 1; j--) {
    goy(j);
  }
  move(sx - 1, sy);
  print(fl);
  return 0;
}
