#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> a, was;
vector<pair<pair<long long, long long>, long long>> ans;
long long cnt = 0;
void check() {
  for (long long j = 0; j < a[0].size(); j++) {
    if (a[1][j] && a[1][j] == a[0][j])
      ans.push_back({{a[1][j], 0}, j}), a[1][j] = 0, cnt++;
    if (a[2][j] && a[2][j] == a[3][j])
      ans.push_back({{a[2][j], 3}, j}), a[2][j] = 0, cnt++;
  }
}
void Solve() {
  cnt = 0;
  long long n, m;
  cin >> n >> m;
  was = a = vector<vector<long long>>(4, vector<long long>(n));
  for (long long i = 0; i < (4); ++i) {
    for (long long j = 0; j < n; ++j) cin >> a[i][j];
  }
  check();
  while (cnt != m && ans.size() <= 20000) {
    bool was = false;
    for (long long j = 0; j + 1 < n; ++j) {
      if (a[1][j] && !a[1][j + 1]) {
        ans.push_back({{a[1][j], 1}, j + 1});
        a[1][j + 1] = a[1][j];
        a[1][j] = 0;
        was = true;
        check();
        break;
      }
    }
    if (was) continue;
    if (a[1][n - 1] && !a[2][n - 1]) {
      ans.push_back({{a[1][n - 1], 2}, n - 1});
      a[2][n - 1] = a[1][n - 1];
      a[1][n - 1] = 0;
      check();
      continue;
    }
    for (long long j = n - 1; j > 0; --j) {
      if (a[2][j] && !a[2][j - 1]) {
        ans.push_back({{a[2][j], 2}, j - 1});
        a[2][j - 1] = a[2][j];
        a[2][j] = 0;
        was = true;
        check();
        break;
      }
    }
    if (was) continue;
    if (a[2][0] && !a[1][0]) {
      ans.push_back({{a[2][0], 1}, 0});
      a[1][0] = a[2][0];
      a[2][0] = 0;
      check();
      continue;
    }
    cout << -1;
    return;
  }
  if (ans.size() > 20000)
    cout << -1;
  else {
    cout << ans.size() << '\n';
    for (auto z : ans) {
      cout << z.first.first << ' ' << z.first.second + 1 << ' ' << z.second + 1
           << '\n';
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  Solve();
  return 0;
}
