#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> park(5, vector<int>(n + 1, 0));
  for (int i = 1; i <= 4; i += 1) {
    for (int j = 1; j <= n; j += 1) {
      cin >> park[i][j];
    }
  }
  vector<vector<int>> ans;
  while (true) {
    for (int j = 1; j <= n; j += 1) {
      if (park[2][j] != 0 && park[2][j] == park[1][j]) {
        ans.push_back({park[2][j], 1, j});
        park[2][j] = 0;
      }
      if (park[3][j] != 0 && park[3][j] == park[4][j]) {
        ans.push_back({park[3][j], 4, j});
        park[3][j] = 0;
      }
    }
    bool full = true;
    bool empty = true;
    int si = 0, sj = 0;
    for (int i = 2; i <= 3; i += 1) {
      for (int j = 1; j <= n; j += 1) {
        if (park[i][j] == 0) {
          full = false;
          si = i;
          sj = j;
        } else
          empty = false;
      }
    }
    if (full) {
      cout << -1 << endl;
      return 0;
    }
    if (empty) break;
    int ti = si, tj = sj;
    while (true) {
      int ni = ti, nj = tj;
      if (ti == 2) {
        if (tj < n)
          nj += 1;
        else {
          ni += 1;
          nj = n;
        }
      } else {
        if (tj > 1)
          nj -= 1;
        else {
          ni -= 1;
          nj = 1;
        }
      }
      if (ni == si && nj == sj) break;
      if (park[ni][nj]) {
        ans.push_back({park[ni][nj], ti, tj});
        park[ti][tj] = park[ni][nj];
        park[ni][nj] = 0;
      }
      ti = ni;
      tj = nj;
    }
  }
  cout << ans.size() << endl;
  for (auto &v : ans) {
    cout << v[0] << " " << v[1] << " " << v[2] << endl;
  }
}
