/*
    author:  Maksim1744
    created: 18.08.2020 23:51:33
*/

#include <bits/stdc++.h>

using namespace std;

#define ll   long long
#define ld   long double

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define TAG_LENGTH       25
#define LR_LEFT          left
#define LR_RIGHT         right
#define LR_VALUE         value
#define LR_SECOND_VALUE  mn
#include "C:/C++ libs/print.cpp"
#else
#define showl            42;
#define shows            42;
#define show(...)        42;
#define showm(...)       42;
#define showt(...)       42;
#define printTree(...)   42;
#define printGraph(...)  42;
#define printLRTree(...) 42;
#define printMatrix(...) 42;
#define printWGraph(...) 42;
#define debug     if (false)
#endif

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> v(4, vector<int>(n));
    cin >> v;
    if (n == 1 && k == 2) {
        if (v[1][0] == v[0][0]) {
            cout << 2 << '\n';
            cout << "1 1 1\n";
            cout << "2 4 1\n";
        } else {
            cout << -1 << '\n';
        }
        return 0;
    }

    vector<pair<int, pair<int, int>>> ans;

    auto spin = [&]() {
        int x = ans.size();
        for (int i = 0; i < n - 1; ++i) {
            if (v[1][i + 1] == 0 && v[1][i] != 0) {
                ans.eb(v[1][i], mp(1, i + 1));
            }
        }
        if (v[2][n - 1] == 0 && v[1][n - 1] != 0) {
            ans.eb(v[1][n - 1], mp(2, n - 1));
        }
        for (int i = 1; i < n; ++i) {
            if (v[2][i - 1] == 0 && v[2][i] != 0) {
                ans.eb(v[2][i], mp(2, i - 1));
            }
        }
        if (v[1][0] == 0 && v[1][0] != 0) {
            ans.eb(v[2][0], mp(1, 0));
        }
        for (; x < ans.size(); ++x) {
            for (int i = 1; i < 3; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (v[i][j] == ans[x].first) {
                        v[i][j] = 0;
                    }
                }
            }
            v[ans[x].second.first][ans[x].second.second] = ans[x].first;
        }
    };

    if (k == n * 2) {
        int p1 = v[0][0];
        int c1 = v[1][0];
        ans.eb(v[1][0], mp(0, 0));
        v[1][0] = 0;
        v[0][0] = -1;
        int done = 0;
        while (done < k - 2) {
            for (int i = 0; i < n; ++i) {
                if (v[0][i] == v[1][i] && v[0][i] != 0) {
                    ans.eb(v[0][i], mp(0, i));
                    v[1][i] = 0;
                    ++done;
                }
                if (v[2][i] == v[3][i] && v[2][i] != 0) {
                    ans.eb(v[2][i], mp(3, i));
                    v[2][i] = 0;
                    ++done;
                }
            }
            spin();
        }
        while (v[1][0] != 0)
            spin();
        ans.eb(c1, mp(1, 0));
        v[0][0] = p1;
        v[1][0] = c1;
        while (done < k) {
            for (int i = 0; i < n; ++i) {
                if (v[0][i] == v[1][i] && v[0][i] != 0) {
                    ans.eb(v[0][i], mp(0, i));
                    v[1][i] = 0;
                    ++done;
                }
                if (v[2][i] == v[3][i] && v[2][i] != 0) {
                    ans.eb(v[2][i], mp(3, i));
                    v[2][i] = 0;
                    ++done;
                }
            }
            spin();
        }
    } else {
        int done = 0;
        while (done < k) {
            for (int i = 0; i < n; ++i) {
                if (v[0][i] == v[1][i] && v[0][i] != 0) {
                    ans.eb(v[0][i], mp(0, i));
                    v[1][i] = 0;
                    ++done;
                }
                if (v[2][i] == v[3][i] && v[2][i] != 0) {
                    ans.eb(v[2][i], mp(3, i));
                    v[2][i] = 0;
                    ++done;
                }
            }
            spin();
        }
    }

    assert(ans.size() <= 20000);
    cout << ans.size() << '\n';
    for (auto [n, p] : ans) {
        cout << n << ' ' << p.first + 1 << ' ' << p.second + 1 << '\n';
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
#include <functional>
#include <stack>
#include <cmath>
#include <cstring>
#include <random>
#include <chrono>
#include <memory>
#include <cassert>
#include <numeric>
#include <array>
#define int long long
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
#define x first
#define y second
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize ("O3")

using namespace std;

const int N = 50 + 10;
const int inf = 1e18 + 10ll;
const int mod = 1e9 + 7;
const int eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

int n, k;
int a[4][N];
pii pred[4][N];
bool used[4][N];
vector<array<int, 3>> ans;
vector<pii> moves = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
const int mxans = 20000;

void op(int fx, int fy, int nx, int ny)
{
	if (!a[fx][fy])
		return;

	ans.push_back({ a[fx][fy], nx, ny });
	a[nx][ny] = a[fx][fy];
	a[fx][fy] = 0;
}

void rotate()
{
	for(int i = 0; i < n; ++i)
		if (!a[1][i])
		{
			for (int j = i + 1; j < n; ++j)
				op(1, j, 1, j - 1);

			op(2, n - 1, 1, n - 1);

			for (int j = n - 2; j >= 0; --j)
				op(2, j, 2, j + 1);

			op(1, 0, 2, 0);

			for (int j = 1; j < i; ++j)
				op(1, j, 1, j - 1);

			return;
		}

	for (int i = 0; i < n; ++i)
		if (!a[2][i])
		{
			for (int j = i - 1; j >= 0; --j)
				op(2, j, 2, j + 1);

			op(1, 0, 2, 0);

			for (int j = 1; j < n; ++j)
				op(1, j, 1, j - 1);

			op(2, n - 1, 1, n - 1);

			for (int j = n - 2; j > i; --j)
				op(2, j, 2, j + 1);

			return;
		}

	cout << "-1" << endl;
	exit(0);
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	cin >> n >> k;

	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < n; ++j)
			cin >> a[i][j];

	while(true)
	{
		for(int i = 0; i < n; ++i)
			if (a[0][i] == a[1][i] && a[0][i])
			{
				ans.push_back({ a[1][i], 0, i });
				a[1][i] = 0;
			}

		for (int i = 0; i < n; ++i)
			if (a[2][i] == a[3][i] && a[2][i])
			{
				ans.push_back({ a[2][i], 3, i });
				a[2][i] = 0;
			}

		bool was = false;

		for (int i = 0; i < n; ++i)
			if (a[1][i] || a[2][i])
				was = true;

		if (!was)
			break;

		rotate();
	}

	if (ans.size() > mxans)
	{
		cout << -1 << endl;
		return 0;
	}

	cout << ans.size() << endl;

	for (auto it : ans)
		cout << it[0] << " " << (it[1] + 1) << " " << (it[2] + 1) << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
#include <functional>
#include <stack>
#include <cmath>
#include <cstring>
#include <random>
#include <chrono>
#include <memory>
#include <cassert>
#include <numeric>
#include <array>
#define int long long
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
#define x first
#define y second
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize ("O3")

using namespace std;

const int N = 50 + 10;
const int inf = 1e18 + 10ll;
const int mod = 1e9 + 7;
const int eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

int n, k;
int a[4][N];
pii pred[4][N];
bool used[4][N];
vector<array<int, 3>> ans;
vector<pii> moves = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
const int mxans = 20000;

bool chk(int x, int y)
{
	return x >= 0 && x < 4 && y >= 0 && y < n;
}

void onewaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if ((nx == 1 || nx == 2) && nx != p.x)
				continue;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur != start)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));

		for (auto it : v)
			ans.push_back({ a[start.x][start.y], it.x, it.y });

		a[start.x][start.y] = 0;
	}
}

vector<vector<pii>> results;
vector<pii> bstresults;

void twowaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur.x != -1)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));
		results.push_back(v);
		bstresults.emplace_back(results.back().size(), (int)results.size() - 1);
	}
}

void apply(int ind)
{
	pii start = results[ind][0];

	for(int i = 1; i < results[ind].size(); ++i)
		ans.push_back({ a[start.x][start.y], results[ind][i].x, results[ind][i].y });

	a[start.x][start.y] = 0;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	cin >> n >> k;

	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < n; ++j)
			cin >> a[i][j];

	while (ans.size() <= mxans)
	{
		bool wascar = false;
		int pans = ans.size();

		for (int i = 1; i <= 2; ++i)
			for (int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					onewaybfs({ i, j });
				}

		if (ans.size() != pans)
			continue;

		results.clear();
		bstresults.clear();

		for(int i = 1; i <= 2; ++i)
			for(int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					twowaybfs({i, j});
				}

		if (!wascar)
			break;

		if (!results.empty())
		{
			sort(all(bstresults));
			apply(bstresults[0].y);
		}

		if (pans == ans.size())
		{
			bool was = false;

			for (int i = 1; i <= 2 && !was; ++i)
				for (int j = 0; j < n && !was; ++j)
					if (a[i][j])
					{
						for (auto dlt : moves)
						{
							int nx = i + dlt.x;
							int ny = j + dlt.y;

							if (chk(nx, ny) && nx != 0 && nx != 3 && !a[nx][ny])
							{
								was = true;
								ans.push_back({ a[i][j], nx, ny });
								a[nx][ny] = a[i][j];
								a[i][j] = 0;
								break;
							}
						}

					}

			if (!was)
				break;

			results.clear();
			bstresults.clear();

			for (int i = 1; i <= 2; ++i)
				for (int j = 0; j < n; ++j)
					if (a[i][j])
					{
						twowaybfs({ i, j });
					}

			if (!results.empty())
			{
				sort(all(bstresults));
				apply(bstresults[0].y);
			}

			if (ans.size() == pans)
				break;
		}
	}

	for(int i = 1; i <= 2; ++i)
		for(int j = 0; j < n; ++j)
			if (a[i][j])
			{
				cout << -1 << endl;
				return 0;
			}

	if (ans.size() > mxans)
	{
		cout << -1 << endl;
		return 0;
	}

	cout << ans.size() << endl;

	for (auto it : ans)
		cout << it[0] << " " << (it[1] + 1) << " " << (it[2] + 1) << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
#include <functional>
#include <stack>
#include <cmath>
#include <cstring>
#include <random>
#include <chrono>
#include <memory>
#include <cassert>
#include <numeric>
#include <array>
#define int long long
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
#define x first
#define y second
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize ("O3")

using namespace std;

const int N = 50 + 10;
const int inf = 1e18 + 10ll;
const int mod = 1e9 + 7;
const int eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

int n, k;
int a[4][N];
pii pred[4][N];
bool used[4][N];
vector<array<int, 3>> ans;
vector<pii> moves = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
const int mxans = 20000;

void op(int fx, int fy, int nx, int ny)
{
	if (!a[fx][fy])
		return;

	ans.push_back({ a[fx][fy], nx, ny });
	a[nx][ny] = a[fx][fy];
	a[fx][fy] = 0;
}

void rotate()
{
	for(int i = 0; i < n; ++i)
		if (!a[1][i])
		{
			for (int j = i + 1; j < n; ++j)
				op(1, j, 1, j - 1);

			op(2, n - 1, 1, n - 1);

			for (int j = n - 2; j >= 0; --j)
				op(2, j, 2, j + 1);

			if(i > 0)
				op(1, 0, 2, 0);

			for (int j = 1; j < i; ++j)
				op(1, j, 1, j - 1);

			return;
		}

	for (int i = 0; i < n; ++i)
		if (!a[2][i])
		{
			for (int j = i - 1; j >= 0; --j)
				op(2, j, 2, j + 1);

			op(1, 0, 2, 0);

			for (int j = 1; j < n; ++j)
				op(1, j, 1, j - 1);

			if(i < n - 1)
				op(2, n - 1, 1, n - 1);

			for (int j = n - 2; j > i; --j)
				op(2, j, 2, j + 1);

			return;
		}

	cout << "-1" << endl;
	exit(0);
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	cin >> n >> k;

	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < n; ++j)
			cin >> a[i][j];

	while(true)
	{
		for(int i = 0; i < n; ++i)
			if (a[0][i] == a[1][i] && a[0][i])
			{
				ans.push_back({ a[1][i], 0, i });
				a[1][i] = 0;
			}

		for (int i = 0; i < n; ++i)
			if (a[2][i] == a[3][i] && a[2][i])
			{
				ans.push_back({ a[2][i], 3, i });
				a[2][i] = 0;
			}

		bool was = false;

		for (int i = 0; i < n; ++i)
			if (a[1][i] || a[2][i])
				was = true;

		if (!was)
			break;

		rotate();
	}

	if (ans.size() > mxans)
	{
		cout << -1 << endl;
		return 0;
	}

	cout << ans.size() << endl;

	for (auto it : ans)
		cout << it[0] << " " << (it[1] + 1) << " " << (it[2] + 1) << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
#include <functional>
#include <stack>
#include <cmath>
#include <cstring>
#include <random>
#include <chrono>
#include <memory>
#include <cassert>
#include <numeric>
#include <array>
#define int long long
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
#define x first
#define y second
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize ("O3")

using namespace std;

const int N = 50 + 10;
const int inf = 1e18 + 10ll;
const int mod = 1e9 + 7;
const int eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

int n, k;
int a[4][N];
pii pred[4][N];
bool used[4][N];
vector<array<int, 3>> ans;
vector<pii> moves = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
const int mxans = 20000;

bool chk(int x, int y)
{
	return x >= 0 && x < 4 && y >= 0 && y < n;
}

void onewaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if ((nx == 1 || nx == 2) && nx != p.x)
				continue;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur != start)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));

		for (auto it : v)
			ans.push_back({ a[start.x][start.y], it.x, it.y });

		a[start.x][start.y] = 0;
	}
}

void twowaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur != start)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));

		for (auto it : v)
			ans.push_back({ a[start.x][start.y], it.x, it.y });

		a[start.x][start.y] = 0;
	}
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	cin >> n >> k;

	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < n; ++j)
			cin >> a[i][j];

	while (ans.size() <= mxans)
	{
		bool wascar = false;
		int pans = ans.size();

		for (int i = 1; i <= 2; ++i)
			for (int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					onewaybfs({ i, j });
				}

		if (ans.size() != pans)
			continue;

		bool endcyc = false;

		for(int i = 1; i <= 2 && !endcyc; ++i)
			for(int j = 0; j < n && !endcyc; ++j)
				if (a[i][j])
				{
					wascar = true;
					twowaybfs({i, j});

					if (ans.size() != pans)
					{
						endcyc = true;
						break;
					}
				}

		if (!wascar)
			break;

		if (pans == ans.size())
		{
			bool was = false;

			for (int i = 1; i <= 2 && !was; ++i)
				for (int j = 0; j < n && !was; ++j)
					if (a[i][j])
					{
						for (auto dlt : moves)
						{
							int nx = i + dlt.x;
							int ny = j + dlt.y;

							if (chk(nx, ny) && nx != 0 && nx != 3 && !a[nx][ny])
							{
								was = true;
								ans.push_back({ a[i][j], nx, ny });
								a[nx][ny] = a[i][j];
								a[i][j] = 0;
								break;
							}
						}

					}

			if (!was)
				break;

			endcyc = false;

			for (int i = 1; i <= 2 && !endcyc; ++i)
				for (int j = 0; j < n && !endcyc; ++j)
					if (a[i][j])
					{
						twowaybfs({ i, j });

						if (ans.size() != pans)
						{
							endcyc = true;
							break;
						}
					}

			if (ans.size() == pans)
				break;
		}
	}

	for(int i = 1; i <= 2; ++i)
		for(int j = 0; j < n; ++j)
			if (a[i][j])
			{
				cout << -1 << endl;
				return 0;
			}

	if (ans.size() > mxans)
	{
		cout << -1 << endl;
		return 0;
	}

	cout << ans.size() << endl;

	for (auto it : ans)
		cout << it[0] << " " << (it[1] + 1) << " " << (it[2] + 1) << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
#include <functional>
#include <stack>
#include <cmath>
#include <cstring>
#include <random>
#include <chrono>
#include <memory>
#include <cassert>
#include <numeric>
#include <array>
#define int long long
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
#define x first
#define y second
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize ("O3")

using namespace std;

const int N = 50 + 10;
const int inf = 1e18 + 10ll;
const int mod = 1e9 + 7;
const int eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

int n, k;
int a[4][N];
pii pred[4][N];
bool used[4][N];
vector<array<int, 3>> ans;
vector<pii> moves = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
const int mxans = 20000;

bool chk(int x, int y)
{
	return x >= 0 && x < 4 && y >= 0 && y < n;
}

void bfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur != start)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));

		for (auto it : v)
			ans.push_back({ a[start.x][start.y], it.x, it.y });

		a[start.x][start.y] = 0;
	}
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	cin >> n >> k;

	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < n; ++j)
			cin >> a[i][j];

	while (ans.size() <= mxans)
	{
		bool wascar = false;
		int pans = ans.size();

		for(int i = 1; i <= 2; ++i)
			for(int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					bfs({i, j});
				}

		if (!wascar)
			break;

		if (pans == ans.size())
		{
			bool was = false;

			for (int i = 1; i <= 2 && !was; ++i)
				for (int j = 0; j < n && !was; ++j)
					if (a[i][j])
					{
						for (auto dlt : moves)
						{
							int nx = i + dlt.x;
							int ny = j + dlt.y;

							if (chk(nx, ny) && nx != 0 && nx != 3 && !a[nx][ny])
							{
								was = true;
								ans.push_back({ a[i][j], nx, ny });
								a[nx][ny] = a[i][j];
								a[i][j] = 0;
								break;
							}
						}

					}

			if (!was)
				break;

			for (int i = 1; i <= 2; ++i)
				for (int j = 0; j < n; ++j)
					if (a[i][j])
						bfs({ i, j });

			if (ans.size() == pans)
				break;
		}
	}

	for(int i = 1; i <= 2; ++i)
		for(int j = 0; j < n; ++j)
			if (a[i][j])
			{
				cout << -1 << endl;
				return 0;
			}

	if (ans.size() > mxans)
	{
		cout << -1 << endl;
		return 0;
	}

	cout << ans.size() << endl;

	for (auto it : ans)
		cout << it[0] << " " << (it[1] + 1) << " " << (it[2] + 1) << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
#include <functional>
#include <stack>
#include <cmath>
#include <cstring>
#include <random>
#include <chrono>
#include <memory>
#include <cassert>
#include <numeric>
#include <array>
#define int long long
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
#define x first
#define y second
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize ("O3")

using namespace std;

const int N = 50 + 10;
const int inf = 1e18 + 10ll;
const int mod = 1e9 + 7;
const int eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

int n, k;
int a[4][N];
pii pred[4][N];
bool used[4][N];
vector<array<int, 3>> ans;
vector<pii> moves = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
const int mxans = 20000;

bool chk(int x, int y)
{
	return x >= 0 && x < 4 && y >= 0 && y < n;
}

void onewaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if ((nx == 1 || nx == 2) && nx != p.x)
				continue;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur != start)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));

		for (auto it : v)
			ans.push_back({ a[start.x][start.y], it.x, it.y });

		a[start.x][start.y] = 0;
	}
}

vector<vector<pii>> results;
vector<pii> bstresults;

void twowaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur.x != -1)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));
		results.push_back(v);
		bstresults.emplace_back(results.back().size(), (int)results.size() - 1);
	}
}

void apply(int ind)
{
	pii start = results[ind][0];

	for(int i = 1; i < results[ind].size(); ++i)
		ans.push_back({ a[start.x][start.y], results[ind][i].x, results[ind][i].y });

	a[start.x][start.y] = 0;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	cin >> n >> k;

	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < n; ++j)
			cin >> a[i][j];

	for(int its = 0; its <= 10000; ++its)
	{
		bool wascar = false;
		int pans = ans.size();

		for (int i = 1; i <= 2; ++i)
			for (int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					onewaybfs({ i, j });
				}

		if (ans.size() != pans)
			continue;

		results.clear();
		bstresults.clear();

		for(int i = 1; i <= 2; ++i)
			for(int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					twowaybfs({i, j});
				}

		if (!wascar)
			break;

		if (!results.empty())
		{
			sort(all(bstresults));
			apply(bstresults[0].y);
		}

		if (pans == ans.size())
		{
			bool was = false;

			for (int i = 1; i <= 2 && !was; ++i)
				for (int j = 0; j < n && !was; ++j)
					if (a[i][j])
					{
						for (auto dlt : moves)
						{
							int nx = i + dlt.x;
							int ny = j + dlt.y;

							if (chk(nx, ny) && nx != 0 && nx != 3 && !a[nx][ny])
							{
								was = true;
								ans.push_back({ a[i][j], nx, ny });
								a[nx][ny] = a[i][j];
								a[i][j] = 0;
								break;
							}
						}

					}

			if (!was)
				break;

			results.clear();
			bstresults.clear();

			for (int i = 1; i <= 2; ++i)
				for (int j = 0; j < n; ++j)
					if (a[i][j])
					{
						twowaybfs({ i, j });
					}

			if (!results.empty())
			{
				sort(all(bstresults));
				apply(bstresults[0].y);
			}
		}
	}

	for(int i = 1; i <= 2; ++i)
		for(int j = 0; j < n; ++j)
			if (a[i][j])
			{
				cout << -1 << endl;
				return 0;
			}

	if (ans.size() > mxans)
	{
		cout << -1 << endl;
		return 0;
	}

	cout << ans.size() << endl;

	for (auto it : ans)
		cout << it[0] << " " << (it[1] + 1) << " " << (it[2] + 1) << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>
#include <queue>
#include <map>
#include <functional>
#include <stack>
#include <cmath>
#include <cstring>
#include <random>
#include <chrono>
#include <memory>
#include <cassert>
#include <numeric>
#include <array>
#define int long long
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
#define x first
#define y second
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize ("O3")

using namespace std;

const int N = 50 + 10;
const int inf = 1e18 + 10ll;
const int mod = 1e9 + 7;
const int eps = 1e-9;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

int n, k;
int a[4][N];
pii pred[4][N];
bool used[4][N];
vector<array<int, 3>> ans;
vector<pii> moves = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} };
const int mxans = 20000;

bool chk(int x, int y)
{
	return x >= 0 && x < 4 && y >= 0 && y < n;
}

void onewaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if ((nx == 1 || nx == 2) && nx != p.x)
				continue;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur != start)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));

		for (auto it : v)
			ans.push_back({ a[start.x][start.y], it.x, it.y });

		a[start.x][start.y] = 0;
	}
}

vector<vector<pii>> results;
vector<pii> bstresults;

void twowaybfs(pii start)
{
	queue<pii> q;
	fill(*used, *used + 4 * N, false);

	q.push(start);

	pii end = { -1, -1 };
	pred[start.x][start.y] = { -1, -1 };

	while (!q.empty() && end.x == -1)
	{
		auto p = q.front();
		used[p.x][p.y] = true;
		q.pop();

		for (auto dlt : moves)
		{
			int nx = p.x + dlt.x;
			int ny = p.y + dlt.y;

			if (chk(nx, ny))
			{
				if (nx == 0 || nx == 3)
				{
					if (a[nx][ny] == a[start.x][start.y])
					{
						end = { nx, ny };
						pred[nx][ny] = p;
						break;
					}
				}
				else if (!a[nx][ny] && !used[nx][ny])
				{
					pred[nx][ny] = p;
					used[nx][ny] = true;
					q.emplace(nx, ny);
				}
			}
		}
	}

	if (end.x != -1)
	{
		vector<pii> v;
		pii cur = end;

		while (cur.x != -1)
		{
			v.push_back(cur);
			cur = pred[cur.x][cur.y];
		}

		reverse(all(v));
		results.push_back(v);
		bstresults.emplace_back(results.back().size(), (int)results.size() - 1);
	}
}

void apply(int ind)
{
	pii start = results[ind][0];

	for(int i = 1; i < results[ind].size(); ++i)
		ans.push_back({ a[start.x][start.y], results[ind][i].x, results[ind][i].y });

	a[start.x][start.y] = 0;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10);

	cin >> n >> k;

	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < n; ++j)
			cin >> a[i][j];

	for(int its = 0; its <= 10000; ++its)
	{
		bool wascar = false;
		int pans = ans.size();

		for (int i = 1; i <= 2; ++i)
			for (int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					onewaybfs({ i, j });
				}

		if (ans.size() != pans)
			continue;

		results.clear();
		bstresults.clear();

		for(int i = 1; i <= 2; ++i)
			for(int j = 0; j < n; ++j)
				if (a[i][j])
				{
					wascar = true;
					twowaybfs({i, j});
				}

		if (!wascar)
			break;

		if (!results.empty())
		{
			sort(all(bstresults));
			apply(bstresults[0].y);
		}

		if (pans == ans.size())
		{
			bool was = false;

			for (int i = 1; i <= 2 && !was; ++i)
				for (int j = 0; j < n && !was; ++j)
					if (a[i][j])
					{
						for (auto dlt : moves)
						{
							int nx = i + dlt.x;
							int ny = j + dlt.y;

							if (chk(nx, ny) && nx != 0 && nx != 3 && !a[nx][ny])
							{
								ans.push_back({ a[i][j], nx, ny });
								a[nx][ny] = a[i][j];
								a[i][j] = 0;

							}
						}

					}

			if (!was)
				break;

			results.clear();
			bstresults.clear();

			for (int i = 1; i <= 2; ++i)
				for (int j = 0; j < n; ++j)
					if (a[i][j])
					{
						twowaybfs({ i, j });
					}

			if (!results.empty())
			{
				sort(all(bstresults));
				apply(bstresults[0].y);
			}
		}
	}

	for(int i = 1; i <= 2; ++i)
		for(int j = 0; j < n; ++j)
			if (a[i][j])
			{
				cout << -1 << endl;
				return 0;
			}

	if (ans.size() > mxans)
	{
		cout << -1 << endl;
		return 0;
	}

	cout << ans.size() << endl;

	for (auto it : ans)
		cout << it[0] << " " << (it[1] + 1) << " " << (it[2] + 1) << endl;
}


#include<bits/stdc++.h>
/*---------------------Define--------------------*/
#define int long long
#define next nextt
#define pii pair<int,int>
#define fi first
#define se second
#define rd random
#define ll long long
#define taskname ""
#define pb push_back
#define mp make_pair
#define pb push_back
#define popb pop_back
#define popf pop_front
#define pf push_front
#define res(a,z,n) for(int i = 1 ; i<=n ; i ++) a[i] = z;
/*-------------------necesary---------------------*/
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*------------------limit-------------------------*/
const int base  = 1e9+7;
const int oo = 1e3 + 10;
const int gh = 1e3 + 3 ;
const int inf = 1e15 + 5 ;
/*-----------------globalvari-----------------------*/
int n,m,k;
int a[5][oo];
pii c[oo];
int ok[5][oo];
int p[oo];
int nc;
int nex[oo];
int pas[oo];
int cnt = 0;
queue<pair<int,pii> > q;
/*-----------------code-----------------------*/

void input()
{
   // printf("1\n");
    //read
    cin>>n>>k;

    for(int i = 1 ; i <= 4 ; i ++)
        for(int j = 1 ; j <= n; j ++ )
            {
                cin>>a[i][j];
                if(i == 2 || i == 3) if(a[i][j]) c[a[i][j]] = mp(i,j),ok[i][j] = a[i][j];
            }
    nc = k;
}

void cre()
{

}

bool park()
{
    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) return 1;
        if(x == 3) if(a[4][y] == i) return 1;
    }
    return fl;
}

void oupark()
{
//    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(i,mp(1,y)));//cout<<i<<" "<<1<<" "<<y<<endl ;
        if(x == 3) if(a[4][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(i,mp(4,y)));
    }
    //return fl;
}

pii nx(int x,int y)
{
    if(x == 2) y ++;
    if(x == 3) y --;
    if(y == n+1) y -- , x++;
    if(y == 0) y ++ , x --;
    return mp(x,y);
}

void ou(int k)
{

    cnt++;
    pas[k] = 1;
    pii x = nx(c[k].fi,c[k].se);

   // cerr<<x.fi<<" "<<x.se<<" "<<k<<endl;
    //cerr<<ok[x.fi][x.se] <<endl;
    ok[c[k].fi][c[k].se] = 0;

    c[k] = x;
    if(pas[nex[k]] == 1) q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
    else ou(nex[k]),q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
    ok[x.fi][x.se] = k;
     //cerr<<ok[2][2]<<endl;
}

void cyc()
{
    pas[0] = 1;
    for(int i = 1 ; i <= k ; i ++)
        pas[i] = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        pii q = nx(x,y);
       // cerr<<i<<" "<<q.fi<<" "<<q.se<<" "<<ok[q.fi][q.se]<<endl;
        nex[i] = ok[q.fi][q.se];
    }

    for(int i = 1 ; i <= k ; i ++)
    {
      //  cerr<<i<<" "<<ok[2][2]<<endl;
        if(p[i] == 1) continue ;
        if(pas[i] == 1) continue ;
        else ou(i);
    }

}

void solve()
{
    cre();
    if(park() == 0) if(2*n == k) {cout<<"-1";exit(0);}
    oupark();
    while(nc)
    {
        cyc();
        oupark();
    }
    cout<<cnt<<endl;
    while(!q.empty())
    {
        pair<int,pii> u = q.front();
        cout<<u.fi<<" "<<u.se.fi<<" "<<u.se.se<<endl;
        q.pop();
    }
    //main code here :))
}

void ouput()
{

    //write
}

int32_t main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //#ifndef ONLINE_JUDGES
    if (ifstream("test.txt"))
    {
        freopen("test.txt", "r", stdin);
        #ifdef CHECKING
            freopen(taskname".out", "w", stdout);
        #else
            //\freopen("test.out", "w", stdout);
        #endif //CHECKING
    }
    else if (ifstream(taskname".inp"))
    {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    //#endif // ONLINE_JUDGES
    int t=1;
   // cin>>t;

    while(t--)
    {
    input();
    solve();
    ouput();
    }
}

//ilove??<3


#include<bits/stdc++.h>
/*---------------------Define--------------------*/
#define int long long
#define next nextt
#define pii pair<int,int>
#define fi first
#define se second
#define rd random
#define ll long long
#define taskname ""
#define pb push_back
#define mp make_pair
#define pb push_back
#define popb pop_back
#define popf pop_front
#define pf push_front
#define res(a,z,n) for(int i = 1 ; i<=n ; i ++) a[i] = z;
/*-------------------necesary---------------------*/
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*------------------limit-------------------------*/
const int base  = 1e9+7;
const int oo = 1e3 + 10;
const int gh = 1e3 + 3 ;
const int inf = 1e15 + 5 ;
/*-----------------globalvari-----------------------*/
int n,m,k;
int a[5][oo];
pii c[oo];
int ok[5][oo];
int p[oo];
int nc;
int nex[oo];
int pas[oo];
int cnt = 0;
queue<pair<int,pii> > q;
/*-----------------code-----------------------*/

void input()
{
   // printf("1\n");
    //read
    cin>>n>>k;

    for(int i = 1 ; i <= 4 ; i ++)
        for(int j = 1 ; j <= n; j ++ )
            {
                cin>>a[i][j];
                if(i == 2 || i == 3) if(a[i][j]) c[a[i][j]] = mp(i,j),ok[i][j] = a[i][j];
            }
    nc = k;
}

void cre()
{

}

bool park()
{
    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) return 1;
        if(x == 3) if(a[4][y] == i) return 1;
    }
    return fl;
}

void oupark()
{
//    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(i,mp(1,y)));//cout<<i<<" "<<1<<" "<<y<<endl ;
        if(x == 3) if(a[4][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(i,mp(4,y)));
    }
    //return fl;
}

pii nx(int x,int y)
{
    if(x == 2) y ++;
    if(x == 3) y --;
    if(y == n+1) y -- , x++;
    if(y == 0) y ++ , x --;
    return mp(x,y);
}

void ou(int k)
{
    cnt++;
    pas[k] = 1;
    pii x = nx(c[k].fi,c[k].se);
    ok[x.fi][x.se] = k;
    ok[c[k].fi][c[k].se] = 0;
    c[k] = x;
    if(pas[nex[k]] == 1) q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
    else ou(nex[k]),q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
}

void cyc()
{
    pas[0] = 1;
    for(int i = 1 ; i <= k ; i ++)
        pas[i] = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        pii q = nx(x,y);
        nex[i] = ok[q.fi][q.se];
    }
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        if(pas[i] == 1) continue ;
        else ou(i);
    }
}

void solve()
{
    cre();

    if(park() == 0) if(2*n == k) cout<<"-1";
    oupark();
    while(nc)
    {
        cyc();
        oupark();
    }
    cout<<cnt<<endl;
    while(!q.empty())
    {
        pair<int,pii> u = q.front();
        cout<<u.fi<<" "<<u.se.fi<<" "<<u.se.se<<endl;
        q.pop();
    }
    //main code here :))
}

void ouput()
{

    //write
}

int32_t main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //#ifndef ONLINE_JUDGES
    if (ifstream("test.txt"))
    {
        freopen("test.txt", "r", stdin);
        #ifdef CHECKING
            freopen(taskname".out", "w", stdout);
        #else
            //\freopen("test.out", "w", stdout);
        #endif //CHECKING
    }
    else if (ifstream(taskname".inp"))
    {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    //#endif // ONLINE_JUDGES
    int t=1;
   // cin>>t;

    while(t--)
    {
    input();
    solve();
    ouput();
    }
}

//ilove??<3


#include<bits/stdc++.h>
/*---------------------Define--------------------*/
#define int long long
#define next nextt
#define pii pair<int,int>
#define fi first
#define se second
#define rd random
#define ll long long
#define taskname ""
#define pb push_back
#define mp make_pair
#define pb push_back
#define popb pop_back
#define popf pop_front
#define pf push_front
#define res(a,z,n) for(int i = 1 ; i<=n ; i ++) a[i] = z;
/*-------------------necesary---------------------*/
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*------------------limit-------------------------*/
const int base  = 1e9+7;
const int oo = 1e3 + 10;
const int gh = 1e3 + 3 ;
const int inf = 1e15 + 5 ;
/*-----------------globalvari-----------------------*/
int n,m,k;
int a[5][oo];
pii c[oo];
int ok[5][oo];
int p[oo];
int nc;
int nex[oo];
int pas[oo];
int cnt = 0;
queue<pair<int,pii> > q;
/*-----------------code-----------------------*/

void input()
{
   // printf("1\n");
    //read
    cin>>n>>k;

    for(int i = 1 ; i <= 4 ; i ++)
        for(int j = 1 ; j <= n; j ++ )
            {
                cin>>a[i][j];
                if(i == 2 || i == 3) if(a[i][j]) c[a[i][j]] = mp(i,j),ok[i][j] = a[i][j];
            }
    nc = k;
}

void cre()
{

}

bool park()
{
    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) return 1;
        if(x == 3) if(a[4][y] == i) return 1;
    }
    return fl;
}

void oupark()
{
//    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(i,mp(1,y)));//cout<<i<<" "<<1<<" "<<y<<endl ;
        if(x == 3) if(a[4][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(i,mp(4,y)));
    }
    //return fl;
}

pii nx(int x,int y)
{
    if(x == 2) y ++;
    if(x == 3) y --;
    if(y == n+1) y -- , x++;
    if(y == 0) y ++ , x --;
    return mp(x,y);
}

void ou(int k)
{
    cnt++;
    pas[k] = 1;
    pii x = nx(c[k].fi,c[k].se);
    ok[x.fi][x.se] = k;
    ok[c[k].fi][c[k].se] = 0;
    c[k] = x;
    if(pas[nex[k]] == 1) q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
    else ou(nex[k]),q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
}

void cyc()
{
    pas[0] = 1;
    for(int i = 1 ; i <= k ; i ++)
        pas[i] = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        pii q = nx(x,y);
        nex[i] = ok[q.fi][q.se];
    }
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        if(pas[i] == 1) continue ;
        else ou(i);
    }
}

void solve()
{
    cre();

    if(park() == 0) if(2*n == k) {cout<<"-1";exit(0);}
    oupark();
    while(nc)
    {
        cyc();
        oupark();
    }
    cout<<cnt<<endl;
    while(!q.empty())
    {
        pair<int,pii> u = q.front();
        cout<<u.fi<<" "<<u.se.fi<<" "<<u.se.se<<endl;
        q.pop();
    }
    //main code here :))
}

void ouput()
{

    //write
}

int32_t main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //#ifndef ONLINE_JUDGES
    if (ifstream("test.txt"))
    {
        freopen("test.txt", "r", stdin);
        #ifdef CHECKING
            freopen(taskname".out", "w", stdout);
        #else
            //\freopen("test.out", "w", stdout);
        #endif //CHECKING
    }
    else if (ifstream(taskname".inp"))
    {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    //#endif // ONLINE_JUDGES
    int t=1;
   // cin>>t;

    while(t--)
    {
    input();
    solve();
    ouput();
    }
}

//ilove??<3


#include<bits/stdc++.h>
/*---------------------Define--------------------*/
#define int long long
#define next nextt
#define pii pair<int,int>
#define fi first
#define se second
#define rd random
#define ll long long
#define taskname ""
#define pb push_back
#define mp make_pair
#define pb push_back
#define popb pop_back
#define popf pop_front
#define pf push_front
#define res(a,z,n) for(int i = 1 ; i<=n ; i ++) a[i] = z;
/*-------------------necesary---------------------*/
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*------------------limit-------------------------*/
const int base  = 1e9+7;
const int oo = 1e3 + 10;
const int gh = 1e3 + 3 ;
const int inf = 1e15 + 5 ;
/*-----------------globalvari-----------------------*/
int n,m,k;
int a[5][oo];
pii c[oo];
int ok[5][oo];
int p[oo];
int nc;
int nex[oo];
int pas[oo];
int cnt = 0;
queue<pair<int,pii> > q;
/*-----------------code-----------------------*/

void input()
{
   // printf("1\n");
    //read
    cin>>n>>k;

    for(int i = 1 ; i <= 4 ; i ++)
        for(int j = 1 ; j <= n; j ++ )
            {
                cin>>a[i][j];
                if(i == 2 || i == 3) if(a[i][j]) c[a[i][j]] = mp(i,j),ok[i][j] = a[i][j];
            }
    nc = k;
}

void cre()
{

}

bool park()
{
    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) return 1;
        if(x == 3) if(a[4][y] == i) return 1;
    }
    return fl;
}

void oupark()
{
//    int fl = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        if(x == 2) if(a[1][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(1,mp(1,y)));//cout<<i<<" "<<1<<" "<<y<<endl ;
        if(x == 3) if(a[4][y] == i) p[i] = 1 , ok[x][y] = 0 , nc -- , cnt ++ ,q.push(mp(1,mp(4,y)));
    }
    //return fl;
}

pii nx(int x,int y)
{
    if(x == 2) y ++;
    if(x == 3) y --;
    if(y == n+1) y -- , x++;
    if(y == 0) y ++ , x --;
    return mp(x,y);
}

void ou(int k)
{
    cnt++;
    pas[k] = 1;
    pii x = nx(c[k].fi,c[k].se);
    ok[x.fi][x.se] = k;
    ok[c[k].fi][c[k].se] = 0;
    c[k] = x;
    if(pas[nex[k]] == 1) q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
    else ou(nex[k]),q.push(mp(k,x));//cout<<k<<" "<<x.fi<<" "<<x.se<<endl;
}

void cyc()
{
    pas[0] = 1;
    for(int i = 1 ; i <= k ; i ++)
        pas[i] = 0;
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        int x = c[i].fi,y = c[i].se;
        pii q = nx(x,y);
        nex[i] = ok[q.fi][q.se];
    }
    for(int i = 1 ; i <= k ; i ++)
    {
        if(p[i] == 1) continue ;
        if(pas[i] == 1) continue ;
        else ou(i);
    }
}

void solve()
{
    cre();

    if(park() == 0) if(2*n == k) cout<<"-1";
    oupark();
    while(nc)
    {
        cyc();
        oupark();
    }
    cout<<cnt<<endl;
    while(!q.empty())
    {
        pair<int,pii> u = q.front();
        cout<<u.fi<<" "<<u.se.fi<<" "<<u.se.se<<endl;
        q.pop();
    }
    //main code here :))
}

void ouput()
{

    //write
}

int32_t main()
{

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //#ifndef ONLINE_JUDGES
    if (ifstream("test.txt"))
    {
        freopen("test.txt", "r", stdin);
        #ifdef CHECKING
            freopen(taskname".out", "w", stdout);
        #else
            //\freopen("test.out", "w", stdout);
        #endif //CHECKING
    }
    else if (ifstream(taskname".inp"))
    {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    //#endif // ONLINE_JUDGES
    int t=1;
   // cin>>t;

    while(t--)
    {
    input();
    solve();
    ouput();
    }
}

//ilove??<3


#include <iostream>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
int n,k;
int p[5][51];
P C[101];
vector<PP> v;

bool ok(int c){
	P t = C[c];
	return p[t.first-1][t.second]==c || p[t.first+1][t.second]==c;
}

P next(int c){
	if(C[c].first==2){
		if(C[c].second==1) return P(3,1);
		else return P(2,C[c].second-1);
	}else{
		if(C[c].second==n) return P(2,n);
		else return P(3,C[c].second+1);
	}
}

int main(){
	cin >> n >> k;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=n;j++){
			cin >> p[i][j];
			if((i==2 || i==3) && p[i][j]!=0){
				C[p[i][j]] = P(i,j);
			}
		}
	}
	if(2*n==k){
		bool judge = false;
		for(int i=1;i<=k;i++){
			if(ok(k)){
				judge = true;
			}
		}
		if(!judge){
			cout << -1 << endl;
			return 0;
		}
	}
	int count = 0;
	queue<int> Q;
	for(int i=1;i<=k;i++){
		if(ok(i)){
			count++;
			p[C[i].first][C[i].second] = 0;
			if(C[i].first==2){
				v.push_back(PP(i,P(1,C[i].second)));
			}else{
				v.push_back(PP(i,P(4,C[i].second)));
			}
		}else{
			Q.push(i);
		}
	}
	while(count<k){
		int c = Q.front();
		Q.pop();
		P r = next(c);
		if(ok(c)){
			count++;
			p[C[c].first][C[c].second] = 0;
			if(C[c].first==2){
				v.push_back(PP(c,P(1,C[c].second)));
			}else{
				v.push_back(PP(c,P(4,C[c].second)));
			}
		}
		else if(p[r.first][r.second]==0){
			v.push_back(PP(c,P(r.first,r.second)));
			//cout << c << " " << r.first << " " << r.second << endl;
			p[r.first][r.second] = c;
			p[C[c].first][C[c].second] = 0;
			C[c] = r;
			Q.push(c);
		}else Q.push(c);
/*		for(int i=1;i<=4;i++){
			for(int j=1;j<=n;j++){
				if(j!=n) cout << p[i][j] << " ";
				else cout << p[i][j] << endl;
			}
		}
		cout << endl;*/
	}
	cout << v.size() << endl;
	for(int i=0;i<v.size();i++){
		cout << v[i].first << " " << v[i].second.first << " " << v[i].second.second << endl;
	}
}


#include <iostream>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
int n,k;
int p[5][51];
P C[101];
vector<PP> v;

bool ok(int c){
	P t = C[c];
	return p[t.first-1][t.second]==c || p[t.first+1][t.second]==c;
}

P next(int c){
	if(C[c].first==2){
		if(C[c].second==1) return P(3,1);
		else return P(2,C[c].second-1);
	}else{
		if(C[c].second==n) return P(2,n);
		else return P(3,C[c].second+1);
	}
}

int main(){
	cin >> n >> k;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=n;j++){
			cin >> p[i][j];
			if((i==2 || i==3) && p[i][j]!=0){
				C[p[i][j]] = P(i,j);
			}
		}
	}
	if(2*n==k){
		bool judge = false;
		for(int i=1;i<=k;i++){
			if(ok(k)){
				judge = true;
			}
		}
		if(!judge){
			cout << -1 << endl;
			return 0;
		}
	}
	int count = 0;
	queue<int> Q;
	for(int i=1;i<=k;i++){
		if(ok(i)){
			count++;
			p[C[i].first][C[i].second] = 0;
			if(C[i].first==2){
				v.push_back(PP(i,P(1,C[i].second)));
			}else{
				v.push_back(PP(i,P(4,C[i].second)));
			}
		}else{
			Q.push(i);
		}
	}
	while(count<k){
		int c = Q.front();
		Q.pop();
		P r = next(c);
		if(ok(c)){
			count++;
			p[C[c].first][C[c].second] = 0;
			if(C[c].first==2){
				v.push_back(PP(c,P(1,C[c].second)));
			}else{
				v.push_back(PP(c,P(4,C[c].second)));
			}
		}
		else if(p[r.first][r.second]==0){
			v.push_back(PP(c,P(r.first,r.second)));
//			cout << c << " " << r.first << " " << r.second << endl;
			p[r.first][r.second] = c;
			p[C[c].first][C[c].second] = 0;
			C[c] = r;
			Q.push(c);
		}
	}
	cout << v.size() << endl;
	for(int i=0;i<v.size();i++){
		cout << v[i].first << " " << v[i].second.first << " " << v[i].second.second << endl;
	}
}


#include <iostream>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
int n,k;
int p[5][51];
P C[101];
vector<PP> v;

bool ok(int c){
	P t = C[c];
	return p[t.first-1][t.second]==c || p[t.first+1][t.second]==c;
}

P next(int c){
	if(C[c].first==2){
		if(C[c].second==1) return P(3,1);
		else return P(2,C[c].second-1);
	}else{
		if(C[c].second==n) return P(2,n);
		else return P(3,C[c].second+1);
	}
}

int main(){
	cin >> n >> k;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=n;j++){
			cin >> p[i][j];
			if((i==2 || i==3) && p[i][j]!=0){
				C[p[i][j]] = P(i,j);
			}
		}
	}
	if(2*n==k){
		bool judge = false;
		for(int i=1;i<=k;i++){
			if(ok(i)){
				judge = true;
			}
		}
		if(!judge){
			cout << -1 << endl;
			return 0;
		}
	}
	int count = 0;
	queue<int> Q;
	for(int i=1;i<=k;i++){
		if(ok(i)){
			count++;
			p[C[i].first][C[i].second] = 0;
			if(C[i].first==2){
				v.push_back(PP(i,P(1,C[i].second)));
			}else{
				v.push_back(PP(i,P(4,C[i].second)));
			}
		}else{
			Q.push(i);
		}
	}
	while(count<k){
		int c = Q.front();
		Q.pop();
		P r = next(c);
		if(ok(c)){
			count++;
			p[C[c].first][C[c].second] = 0;
			if(C[c].first==2){
				v.push_back(PP(c,P(1,C[c].second)));
			}else{
				v.push_back(PP(c,P(4,C[c].second)));
			}
		}
		else if(p[r.first][r.second]==0){
			v.push_back(PP(c,P(r.first,r.second)));
			p[r.first][r.second] = c;
			p[C[c].first][C[c].second] = 0;
			C[c] = r;
			Q.push(c);
		}else Q.push(c);
	}
	cout << v.size() << endl;
	for(int i=0;i<v.size();i++){
		cout << v[i].first << " " << v[i].second.first << " " << v[i].second.second << endl;
	}
}


#include <iostream>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> PP;
int n,k;
int p[5][51];
P C[101];
vector<PP> v;

bool ok(int c){
	P t = C[c];
	return p[t.first-1][t.second]==c || p[t.first+1][t.second]==c;
}

P next(int c){
	if(C[c].first==2){
		if(C[c].second==1) return P(3,1);
		else return P(2,C[c].second-1);
	}else{
		if(C[c].second==n) return P(2,n);
		else return P(3,C[c].second+1);
	}
}

int main(){
	cin >> n >> k;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=n;j++){
			cin >> p[i][j];
			if((i==2 || i==3) && p[i][j]!=0){
				C[p[i][j]] = P(i,j);
			}
		}
	}
	if(2*n==k){
		bool judge = false;
		for(int i=1;i<=k;i++){
			if(ok(i)){
				judge = true;
			}
		}
		if(!judge){
			cout << -1 << endl;
			return 0;
		}
	}
	int count = 0;
	queue<int> Q;
	for(int i=1;i<=k;i++){
		if(ok(i)){
			count++;
			p[C[i].first][C[i].second] = 0;
			if(C[i].first==2){
				v.push_back(PP(i,P(1,C[i].second)));
			}else{
				v.push_back(PP(i,P(4,C[i].second)));
			}
		}else{
			Q.push(i);
		}
	}
	while(count<k){
		int c = Q.front();
		Q.pop();
		P r = next(c);
		if(ok(c)){
			count++;
			p[C[c].first][C[c].second] = 0;
			if(C[c].first==2){
				v.push_back(PP(c,P(1,C[c].second)));
			}else{
				v.push_back(PP(c,P(4,C[c].second)));
			}
		}
		else if(p[r.first][r.second]==0){
			v.push_back(PP(c,P(r.first,r.second)));
			p[r.first][r.second] = c;
			p[C[c].first][C[c].second] = 0;
			C[c] = r;
			Q.push(c);
		}else Q.push(c);
/*		for(int i=1;i<=4;i++){
			for(int j=1;j<=n;j++){
				if(j!=n) cout << p[i][j] << " ";
				else cout << p[i][j] << endl;
			}
		}
		cout << endl;*/
	}
	cout << v.size() << endl;
	for(int i=0;i<v.size();i++){
		cout << v[i].first << " " << v[i].second.first << " " << v[i].second.second << endl;
	}
}


#include <iostream>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> P;
int n,k;
int p[5][51];
P C[101];

bool ok(int c){
	P t = C[c];
	return p[t.first-1][t.second]==c || p[t.first+1][t.second]==c;
}

P next(int c){
	if(C[c].first==2){
		if(C[c].second==1) return P(3,1);
		else return P(2,C[c].second-1);
	}else{
		if(C[c].second==n) return P(2,n);
		else return P(3,C[c].second+1);
	}
}

int main(){
	cin >> n >> k;
	for(int i=1;i<=4;i++){
		for(int j=1;j<=n;j++){
			cin >> p[i][j];
			if((i==2 || i==3) && p[i][j]!=0){
				C[p[i][j]] = P(i,j);
			}
		}
	}
	if(2*n==k){
		bool judge = false;
		for(int i=1;i<=k;i++){
			if(ok(k)){
				judge = true;
			}
		}
		if(!judge){
			cout << -1 << endl;
			return 0;
		}
	}
	int count = 0;
	queue<int> Q;
	for(int i=1;i<=k;i++){
		if(ok(i)){
			count++;
			p[C[i].first][C[i].second] = 0;
			if(C[i].first==2){
				cout << i << " " << 1 << " " << C[i].second << endl;
			}else{
				cout << i << " " << 4 << " " << C[i].second << endl;
			}
		}else{
			Q.push(i);
		}
	}
	while(count<k){
		int c = Q.front();
		Q.pop();
		P r = next(c);
		if(ok(c)){
			count++;
			p[C[c].first][C[c].second] = 0;
			if(C[c].first==2){
				cout << c << " " << 1 << " " << C[c].second << endl;
			}else{
				cout << c << " " << 4 << " " << C[c].second << endl;
			}
		}
		else if(p[r.first][r.second]==0){
			cout << c << " " << r.first << " " << r.second << endl;
			p[r.first][r.second] = c;
			p[C[c].first][C[c].second] = 0;
			C[c] = r;
			Q.push(c);
		}
	}
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];
int a[3][M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        cin >> a[k][i];
        v[a[k][i]] = {k, i};
    }
}

inline void sol_update(pair<int, int> loc[], int i, int &m) {
    sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
    m ++;
}

inline void update(pair<int, int> loc[], int i, int n, int &m) {
    pair<int, int> j = loc[i];
    if (loc[i].first == 2) {
        if (loc[i].second == n - 1) {
            j = {1, n - 1};
        } else {
            j.second ++;
        }
    } else {
        if (loc[i].second == 0) {
            j = {2, 0};
        } else {
            j.second --;
        }
    }
    if (a[j.first][j.second] == 0) {
        a[j.first][j.second] = 1;
        a[loc[i].first][loc[i].second] = 0;
        loc[i] = j;
        sol_update(loc, i, m);
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k + 1], loc[k + 1];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k, r[k + 1];
    for (int i = 1; i <= k; i ++) {
        r[i] = i;
    }
    while (rest && m < M) {
        int rm = 0;
        for (int j = 1; j <= k; j ++) {
            int i = r[j];
            if (p[i].second == loc[i].second &&
                (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
                loc[i] = p[i];
                rest --;
                sol_update(loc, i, m);
            } else {
                update(loc, i, n, m);
                rm ++;
                r[rm] = i;
            }
        }
        if (rest == k && k == 2 * n)    m = M;
        k = rm;
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        int x;
        cin >> x;
        v[x] = {k, i};
    }
}

void update(pair<int, int> loc[], int i, int n) {
    if (loc[i].first == 2) {
        if (loc[i].second == n) {
            loc[i] = {2, n};
        } else {
            loc[i].second ++;
        }
    } else {
        if (loc[i].second == 0) {
            loc[i] = {1, 0};
        } else {
            loc[i].second --;
        }
    }
}

void find_sol(pair<int, int> p[], pair<int, int> loc[], int k, int n,
        int &m, int &rest) {
    for (int i = 1; i <= k; i ++) {
        if (p[i] == loc[i])     continue;
        if (p[i].second == loc[i].second &&
            (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
            loc[i] = p[i];
            rest --;
        } else {
            update(loc, i, n);
        }
        sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
        m ++;
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k], loc[k];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k;
    while (rest && m < M) {
        find_sol(p, loc, k, n, m, rest);
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        int x;
        cin >> x;
        v[x] = {k, i};
    }
}

void update(pair<int, int> loc[], int i, int n) {
    if (loc[i].first == 1) {
        if (loc[i].second == n) {
            loc[i] = {2, n};
        } else {
            loc[i].second ++;
        }
    } else {
        if (loc[i].second == 0) {
            loc[i] = {1, 0};
        } else {
            loc[i].second --;
        }
    }
}

void find_sol(pair<int, int> p[], pair<int, int> loc[], int k, int n,
        int &m, int &rest) {
    for (int i = 1; i <= k; i ++) {
        if (p[i] == loc[i])     continue;
        if (p[i].second == loc[i].second &&
            (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
            loc[i] = p[i];
            rest --;
        } else {
            update(loc, i, n);
        }
        sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
        m ++;
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k + 1], loc[k + 1];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k;
    while (rest && m < M) {
        find_sol(p, loc, k, n, m, rest);
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];
int a[3][M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        cin >> a[k][i];
        v[a[k][i]] = {k, i};
    }
}

bool matrix_update(int i1, int j1, int i2, int j2) {
    if (a[i2][j2] == 0) {
        a[i2][j2] = a[i1][j1];
        a[i1][j1] = 0;
        return 1;
    }
    return 0;
}

void sol_update(pair<int, int> loc[], int i, int &m) {
    sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
    m ++;
}

void update(pair<int, int> loc[], int i, int n, int &m) {
    pair<int, int> j = loc[i];
    if (loc[i].first == 2) {
        if (loc[i].second == n - 1) {
            j = {1, n - 1};
        } else {
            j.second ++;
        }
    } else {
        if (loc[i].second == 0) {
            j = {2, 0};
        } else {
            j.second --;
        }
    }
    if (matrix_update(loc[i].first, loc[i].second, j.first, j.second)) {
        loc[i] = j;
        sol_update(loc, i, m);
    }
}

void find_sol(pair<int, int> p[], pair<int, int> loc[], int k, int n,
        int &m, int &rest) {
    for (int i = 1; i <= k; i ++) {
        if (p[i] == loc[i])     continue;
        if (p[i].second == loc[i].second &&
            (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
            loc[i] = p[i];
            rest --;
            sol_update(loc, i, m);
        } else {
            update(loc, i, n, m);
        }
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k + 1], loc[k + 1];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k;
    while (rest && m < M) {
        find_sol(p, loc, k, n, m, rest);
        if (rest == k && k == 2 * n)    m = M;
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];
int a[3][M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        cin >> a[k][i];
        v[a[k][i]] = {k, i};
    }
}

inline void sol_update(pair<int, int> loc[], int i, int &m) {
    sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
    m ++;
}

inline void update(pair<int, int> loc[], int i, int n, int &m) {
    pair<int, int> j = loc[i];
    if (loc[i].first == 2) {
        if (loc[i].second == n - 1) {
            j = {1, n - 1};
        } else {
            j.second ++;
        }
    } else {
        if (loc[i].second == 0) {
            j = {2, 0};
        } else {
            j.second --;
        }
    }
    if (a[j.first][j.second] == 0) {
        a[j.first][j.second] = 1;
        a[loc[i].first][loc[i].second] = 0;
        loc[i] = j;
        sol_update(loc, i, m);
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k + 1], loc[k + 1];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k;
    while (rest && m < M) {
        for (int i = 1; i <= k; i ++) {
            if (p[i] == loc[i])     continue;
            if (p[i].second == loc[i].second &&
                (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
                loc[i] = p[i];
                rest --;
                sol_update(loc, i, m);
            } else {
                update(loc, i, n, m);
            }
        }
        if (rest == k && k == 2 * n)    m = M;
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        int x;
        cin >> x;
        v[x] = {k, i};
    }
}

void update(pair<int, int> loc[], int i, int n) {
    if (loc[i].first == 1) {
        if (loc[i].second == n - 1) {
            loc[i] = {2, n - 1};
        } else {
            loc[i].second ++;
        }
    } else {
        if (loc[i].second == 0) {
            loc[i] = {1, 0};
        } else {
            loc[i].second --;
        }
    }
}

void find_sol(pair<int, int> p[], pair<int, int> loc[], int k, int n,
        int &m, int &rest) {
    for (int i = 1; i <= k; i ++) {
        if (p[i] == loc[i])     continue;
        if (p[i].second == loc[i].second &&
            (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
            loc[i] = p[i];
            rest --;
        } else {
            update(loc, i, n);
        }
        sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
        m ++;
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k + 1], loc[k + 1];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k;
    while (rest && m < M) {
        find_sol(p, loc, k, n, m, rest);
        if (rest == k && k == 2 * n)    m = M;
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        int x;
        cin >> x;
        v[x] = {k, i};
    }
}

void update(pair<int, int> loc[], int i, int n) {
    if (loc[i].first == 1) {
        if (loc[i].second == n - 1) {
            loc[i] = {2, n - 1};
        } else {
            loc[i].second ++;
        }
    } else {
        if (loc[i].second == 0) {
            loc[i] = {1, 0};
        } else {
            loc[i].second --;
        }
    }
}

void find_sol(pair<int, int> p[], pair<int, int> loc[], int k, int n,
        int &m, int &rest) {
    for (int i = 1; i <= k; i ++) {
        if (p[i] == loc[i])     continue;
        if (p[i].second == loc[i].second &&
            (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
            loc[i] = p[i];
            rest --;
        } else {
            update(loc, i, n);
        }
        sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
        m ++;
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k + 1], loc[k + 1];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k;
    while (rest && m < M) {
        find_sol(p, loc, k, n, m, rest);
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];

void read(pair<int, int> v[], int k, int n) {
    for (int i = 0; i < n; i ++) {
        int x;
        cin >> x;
        v[x] = {k, i};
    }
}

void update(pair<int, int> loc[], int i, int n) {
    if (loc[i].first == 2) {
        if (loc[i].second == n - 1) {
            loc[i] = {1, n - 1};
        } else {
            loc[i].second ++;
        }
    } else {
        if (loc[i].second == 0) {
            loc[i] = {2, 0};
        } else {
            loc[i].second --;
        }
    }
}

void find_sol(pair<int, int> p[], pair<int, int> loc[], int k, int n,
        int &m, int &rest) {
    for (int i = 1; i <= k; i ++) {
        if (p[i] == loc[i])     continue;
        if (p[i].second == loc[i].second &&
            (p[i].first + 1 == loc[i].first || loc[i].first + 1 == p[i].first)) {
            loc[i] = p[i];
            rest --;
        } else {
            update(loc, i, n);
        }
        sol[m] = {i, {loc[i].first + 1, loc[i].second + 1}};
        m ++;
    }
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    pair<int, int> p[k + 1], loc[k + 1];
    read(p, 0, n);
    read(loc, 1, n);
    read(loc, 2, n);
    read(p, 3, n);
    int rest = k;
    while (rest && m < M) {
        find_sol(p, loc, k, n, m, rest);
        if (rest == k && k == 2 * n)    m = M;
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];

pair<int, int> next(pair<int, int> x, int n) {
    pair<int, int> y = x;
    if (x.first == 1) {
        if (x.second == n - 1) {
            y = {2, n - 1};
        } else {
            y.second ++;
        }
    } else {
        if (x.second == 0) {
            y = {1, 0};
        } else {
            y.second --;
        }
    }
    return y;
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    int a[4][n];
    for (int i = 0; i < 4; i ++) {
        for (int j = 0; j < n; j ++) {
            cin >> a[i][j];
        }
    }
    int rest = k;
    while (rest && m < M) {
        pair<int, int> x = {0, 0}, y, z;
        for (int i = 0; i < n; i ++) {
            if (a[1][i] == a[0][i] && a[1][i] != 0) {
                a[1][i] = 0;
                sol[m] = {a[0][i], {1, i + 1}};
                m ++;
                rest --;
            }
            if (a[2][i] == a[3][i] && a[2][i] != 0) {
                a[2][i] = 0;
                sol[m] = {a[3][i], {4, i + 1}};
                m ++;
                rest --;
            }
            if (a[1][i] == 0) {
                x = {1, i};
            } else if (a[2][i] == 0) {
                x = {2, i};
            }
        }
        if (x.first == 0) {
            m = M;
            break;
        }
        z = x;
        y = next(x, n);
        while (y != z) {
            a[x.first][x.second] = a[y.first][y.second];
            a[y.first][y.second] = 0;
            if (a[x.first][x.second] != 0) {
                sol[m] = {a[x.first][x.second], {x.first + 1, x.second + 1}};
                m ++;
            }
            x = y;
            y = next(x, n);
        }
    }
    print(m);
    return 0;
}


#include <iostream>
using namespace std;
#define M 20001
pair<int, pair<int, int>> sol[M];
int a[4][M];

pair<int, int> next(pair<int, int> x, int n) {
    pair<int, int> y = x;
    if (x.first == 1) {
        if (x.second == n - 1) {
            y = {2, n - 1};
        } else {
            y.second ++;
        }
    } else {
        if (x.second == 0) {
            y = {1, 0};
        } else {
            y.second --;
        }
    }
    return y;
}

void print(int m) {
    if (m >= M) {
        cout << "-1";
        return ;
    }
    cout << m << '\n';
    for (int i = 0; i < m; i ++) {
        cout << sol[i].first << ' ' << sol[i].second.first << ' ';
        cout << sol[i].second.second << '\n';
    }
}

int main() {
    int n, k, m = 0;
    cin >> n >> k;
    for (int i = 0; i < 4; i ++) {
        for (int j = 0; j < n; j ++) {
            cin >> a[i][j];
        }
    }
    int rest = k;
    while (rest && m < M) {
        pair<int, int> x = {0, 0}, y, z;
        for (int i = 0; i < n; i ++) {
            if (a[1][i] == a[0][i] && a[1][i] != 0) {
                a[1][i] = 0;
                sol[m] = {a[0][i], {1, i + 1}};
                m ++;
                rest --;
            }
            if (a[2][i] == a[3][i] && a[2][i] != 0) {
                a[2][i] = 0;
                sol[m] = {a[3][i], {4, i + 1}};
                m ++;
                rest --;
            }
            if (a[1][i] == 0) {
                x = {1, i};
            } else if (a[2][i] == 0) {
                x = {2, i};
            }
        }
        if (x.first == 0) {
            m = M;
            break;
        }
        z = x;
        y = next(x, n);
        while (y != z) {
            a[x.first][x.second] = a[y.first][y.second];
            if (a[x.first][x.second] != 0) {
                sol[m] = {a[x.first][x.second], {x.first + 1, x.second + 1}};
                m ++;
            }
            x = y;
            y = next(x, n);
        }
    }
    print(m);
    return 0;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <cmath>

using namespace std;
typedef long long ll;
const int MAXN = 55;

int N, K;
int board[4][MAXN];

vector <pair <int, pair <int, int> > > v;
pair <int, int> ord[2*MAXN];

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> K;
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < N; j++)
            cin >> board[i][j];

    for (int i = 0; i < N; i++)
    {
        ord[i] = make_pair (1, i);
        ord[i+N] = make_pair (2, N - 1 - i);
    }
    ord[2*N] = make_pair (1, 0);

    for (int run = 0; run < 400; run++)
    {
        bool found = false;
        for (int i = 0; i < 2 * N; i++)
        {
            int x = ord[i].first, y = ord[i].second;
            if (board[x][y] > 0 && board[x][y] == board[x^1][y])
            {
                v.push_back(make_pair (board[x][y], make_pair (x^1, y)));
                board[x][y] = 0;
                found = true;
            }
        }

        int mv = 0;
        for (int i = 2 * N - 1; i >= mv; i--)
        {
            int x = ord[i].first, y = ord[i].second;
            int nx = ord[i+1].first, ny = ord[i+1].second;
            if (board[x][y] > 0 && board[nx][ny] == 0)
            {
                v.push_back(make_pair (board[x][y], make_pair (nx, ny)));
                swap (board[x][y], board[nx][ny]);
                found = true;
                if (i == 2 * N - 1)
                    mv = 1;
            }
        }

        if (!found)
            break;
    }

    bool bad = false;
    for (int i = 1; i <= 2; i++)
        for (int j = 0; j < N; j++)
            if (board[i][j])
                bad = true;

    if (bad)
    {
        cout << "-1\n";
        return 0;
    }
    cout << v.size() << "\n";
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i].first << " " << v[i].second.first + 1 << " " << v[i].second.second + 1 << "\n";
    }
}


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <cmath>

using namespace std;
typedef long long ll;
const int MAXN = 55;

int N, K;
int board[4][MAXN];

vector <pair <int, pair <int, int> > > v;
pair <int, int> ord[2*MAXN];

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> K;
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < N; j++)
            cin >> board[i][j];

    for (int i = 0; i < N; i++)
    {
        ord[i] = make_pair (1, i);
        ord[i+N] = make_pair (2, N - 1 - i);
    }
    ord[2*N] = make_pair (1, 0);

    for (int run = 0; run < 400; run++)
    {
        bool found = false;
        for (int i = 0; i < 2 * N; i++)
        {
            int x = ord[i].first, y = ord[i].second;
            if (board[x][y] > 0 && board[x][y] == board[x^1][y])
            {
                v.push_back(make_pair (board[x][y], make_pair (x^1, y)));
                board[x][y] = 0;
                found = true;
            }
        }

        for (int i = 2 * N - 1; i >= 0; i--)
        {
            int x = ord[i].first, y = ord[i].second;
            int nx = ord[i+1].first, ny = ord[i+1].second;
            if (board[x][y] > 0 && board[nx][ny] == 0)
            {
                v.push_back(make_pair (board[x][y], make_pair (nx, ny)));
                swap (board[x][y], board[nx][ny]);
                found = true;
            }
        }

        if (!found)
            break;
    }

    bool bad = false;
    for (int i = 1; i <= 2; i++)
        for (int j = 0; j < N; j++)
            if (board[i][j])
                bad = true;

    if (bad)
    {
        cout << "-1\n";
        return 0;
    }
    cout << v.size() << "\n";
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i].first << " " << v[i].second.first + 1 << " " << v[i].second.second + 1 << "\n";
    }
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define ll long long int

int n;
vector<vector<int>> g(4);
vector<pair<int,pair<int,int>>> moves;

pair<int,int> find_gap(){
    for(int i=0;i<n;i++){
        if(g[1][i]==0){
            return {1,i};
        }
        if(g[2][i]==0){
            return {2,i};
        }
    }
    return {-1,-1};
}

bool rotate(){
    pair<int,int> gap=find_gap();
    if(gap.first==-1)return false;
    vector<vector<bool>> inc(2,vector<bool> (n));
    bool okay=true;
    debug("R");
    while(okay){
        okay=false;
        for(int i=0;i<n;i++){
            if(i==n-1 && !inc[1][n-1] && g[1][n-1]==0 && g[2][n-1]!=0){
                debug("SU");
                inc[0][n-1]=true;
                g[1][n-1]=g[2][n-1];
                g[2][n-1]=0;
                okay=true;
                moves.push_back({g[1][n-1],{2,n}});
            }
            else if(i!=n-1 && !inc[1][i] && g[2][i]!=0 && g[2][i+1]==0){
                debug("SR");
                okay=true;
                g[2][i+1]=g[2][i];
                g[2][i]=0;
                inc[1][i+1]=true;
                moves.push_back({g[2][i+1],{3,i+2}});
            }
        }
        for(int i=n-1;i>=0;i--){
            if(i==0 && !inc[0][0] && g[1][0]!=0 && g[2][0]==0){
                debug("SD");
                inc[1][0]=true;
                g[2][0]=g[1][0];
                g[1][0]=0;
                moves.push_back({g[2][0],{3,1}});
            }
            else if(i!=0 && !inc[0][i] && g[1][i-1]==0 && g[1][i]!=0){
                debug("SL");
                okay=true;
                g[1][i-1]=g[1][i];
                g[1][i]=0;
                moves.push_back({g[1][i-1],{2,i}});
                inc[0][i-1]=true;
            }
        }
        debug((int)inc[0][0]);
        debug(moves);

    debug(g[0]);
    debug(g[1]);
    debug(g[2]);
    debug(g[3]);
    }
    return true;
}

void push_cars(){
    debug("Pushing");
    for(int i=0;i<n;i++){
        if(g[1][i]!=0 && g[0][i]==g[1][i]){
            g[1][i]=0;
            moves.push_back({g[0][i],{1,i+1}});
        }
        if(g[2][i]!=0 && g[3][i]==g[2][i]){
            g[2][i]=0;
            moves.push_back({g[3][i],{4,i+1}});
        }
    }
    debug(g[0]);debug(g[1]);debug(g[2]);debug(g[3]);
}

bool valid(){
    for(int i=0;i<n;i++){
        if(g[1][i] || g[2][i])return false;
    }
    return true;
}

int main(){
    int k;
    cin>>n>>k;
    for(int i=0;i<4;i++){
        g[i].resize(n);
        for(int j=0;j<n;j++){
            cin>>g[i][j];
        }
    }
    debug(g);
    int cnt=0;
    while(!valid()){
        push_cars();
        if(!rotate()){
            cout<<"-1\n";
            return 0;
        }
        cnt++;
        //if(cnt==5)break;
        //break;
    }
    cout<<moves.size()<<"\n";
    for(auto x:moves){
        cout<<x.first<<" "<<x.second.first<<" "<<x.second.second<<"\n";
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define ll long long int

int n;
vector<vector<int>> g(4);
vector<pair<int,pair<int,int>>> moves;

pair<int,int> find_gap(){
    for(int i=0;i<n;i++){
        if(g[1][i]==0){
            return {1,i};
        }
        if(g[2][i]==0){
            return {2,i};
        }
    }
    return {-1,-1};
}

bool rotate(){
    pair<int,int> gap=find_gap();
    if(gap.first==-1)return false;
    vector<vector<bool>> inc(2,vector<bool> (n));
    bool okay=true;
    debug("R");
    while(okay){
        okay=false;
        for(int i=0;i<n;i++){
            if(i==n-1 && !inc[0][n-1] && g[1][n-1]==0 && g[2][n-1]!=0){
                inc[0][n-1]=true;
                g[1][n-1]=g[2][n-1];
                g[2][n-1]=true;
                okay=true;
                moves.push_back({g[1][n-1],{2,n}});
            }
            else if(!inc[1][i] && g[2][i]!=0 && g[2][i+1]==0){
                okay=true;
                g[2][i+1]=g[2][i];
                g[2][i]=0;
                inc[1][i+1]=true;
                moves.push_back({g[2][i+1],{3,i+2}});
            }
        }
        for(int i=n-1;i>=0;i--){
            if(i==0 && !inc[1][0] && g[1][0]!=0 && g[2][0]==0){
                inc[1][0]=true;
                g[2][0]=g[1][0];
                g[1][0]=0;
                moves.push_back({g[2][0],{3,1}});
            }
            else if(i!=0 && !inc[0][i] && g[1][i-1]==0 && g[1][i]!=0){
                okay=true;
                g[1][i-1]=g[1][i];
                g[1][i]=0;
                moves.push_back({g[1][i-1],{2,i}});
                inc[0][i-1]=true;
            }
        }
        debug(okay);
    }

    debug(g[0]);
    debug(g[1]);
    debug(g[2]);
    debug(g[3]);
    return true;
}

void push_cars(){
    for(int i=0;i<n;i++){
        if(g[1][i]!=0 && g[0][i]==g[1][i]){
            g[1][i]=0;
            moves.push_back({g[0][i],{1,i+1}});
        }
        if(g[2][i]!=0 && g[3][i]==g[2][i]){
            g[2][i]=0;
            moves.push_back({g[3][i],{4,i+1}});
        }
    }
    debug(g[0]);debug(g[1]);debug(g[2]);debug(g[3]);
}

bool valid(){
    for(int i=0;i<n;i++){
        if(g[1][i] || g[2][i])return false;
    }
    return true;
}

int main(){
    int k;
    cin>>n>>k;
    for(int i=0;i<4;i++){
        g[i].resize(n);
        for(int j=0;j<n;j++){
            cin>>g[i][j];
        }
        debug(g);
    }
    debug(g);
    int cnt=0;
    while(!valid()){
        push_cars();
        if(!rotate()){
            cout<<"-1\n";
            return 0;
        }
        cnt++;
    }
    cout<<moves.size()<<"\n";
    for(auto x:moves){
        cout<<x.first<<" "<<x.second.first<<" "<<x.second.second<<"\n";
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <numeric>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <queue>
#include <numeric>
#include <iomanip>
#define ll long long
using namespace std;
int arr[10][150];
int cnt = 0,n,k;
struct info{
    int a,b,c;
};
vector<info> vc;
pair<int,int> empty(){
    for(int i = 2; i <= 3; i++){
        for(int j = 1; j <= n; j++){
            if(arr[i][j] == 0) return make_pair(i,j);
        }
    }
    return {0,0};
}
void go(){
    for(int i = 2; i <= 3; i++){
        for(int j = 1; j <= n; j++){
            if(arr[i][j] == 0) continue;
            if(i == 2 && arr[i][j]  == arr[i-1][j]){
//                cout << arr[i][j] <<' ' << i-1<<' ' << j <<"\n";
                vc.push_back({arr[i][j],i-1,j});
                arr[i][j] = 0;
                cnt++;
                return;
            }
            if(i == 3 && arr[i][j]  == arr[i+1][j]){
//                cout << arr[i][j] <<' ' << i+1<<' ' << j <<"\n";
                vc.push_back({arr[i][j],i+1,j});
                arr[i][j] = 0;
                cnt++;
                return;
            }
        }
    }
    cout << -1;
    exit(0);
}
int dy[5][1005];
int dx[5][1005];
pair<int,int> nextPos(int y,int x){
    if(y == 2 && x < n){
        return {y,x+1};
    }
    if(y == 2 && x == n){
        return {y+1,x};
    }
    if(y == 3 && x > 1){
        return {y,x-1};
    }
    if(y == 3 && x == 1){
        return {y-1,x};
    }
    return make_pair(-1,-1);
}
void print(){
    for(int i = 1; i <= 4; i++){
        for(int j = 1; j <= n; j++){
            cout << arr[i][j];
        }
        cout << endl;
    }
    cout << endl;
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;

    for(int j = 1; j <= 4; j++){
        for(int i = 1; i <= n; i++){
            cin >> arr[j][i];
        }
    }
    if(k == 2 * n){
        go();
    }
    while(cnt < k){
//        print();
        for(int i = 1; i <= 4; i++){
            for(int j = 1; j <= n; j++){
                if(arr[i][j] == 0) continue;
                if(i == 2 && arr[i][j]  == arr[i-1][j]){
//                    cout << arr[i][j] <<' ' << i-1<<' ' << j <<"\n";
                    vc.push_back({arr[i][j],i-1,j});
                    arr[i][j] = 0;
                    cnt++;
                    continue;
                }
                if(i == 3 && arr[i][j]  == arr[i+1][j]){
//                    cout << arr[i][j] <<' ' << i+1<<' ' << j <<"\n";
                    vc.push_back({arr[i][j],i+1,j});
                    arr[i][j] = 0;
                    cnt++;
                    continue;
                }
                pair<int,int> loc = nextPos(i,j);
                if(arr[loc.first][loc.second] == 0){
                    vc.push_back({arr[i][j],loc.first,loc.second});
//                    cout << arr[i][j] <<' '<< loc.first<<' ' <<loc.second << "\n";
                    arr[loc.first][loc.second] = arr[i][j];
                    arr[i][j] = 0;
                }
            }
        }
    }
    cout << vc.size()<<"\n";
    for(int i = 0; i < vc.size(); i++){
        cout << vc[i].a<<' '<<vc[i].b <<' ' <<vc[i].c << "\n";
    }
    return 0;
}

/*
 4 5
 1 2 0 4
 1 2 0 4
 5 0 0 3
 0 5 0 3
 1 1 1
 2 1 2
 4 1 4
 5 2 1
 3 4 4
 5 2 2
 5 2 3
 5 2 4
 5 3 4
 5 3 3
 5 3 2
 5 4 2
 5 3 2
 */


#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <numeric>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <queue>
#include <numeric>
#include <iomanip>
#define ll long long
using namespace std;
int arr[10][150];
int cnt = 0,n,k;
struct info{
    int a,b,c;
};
vector<info> vc;
pair<int,int> empty(){
    for(int i = 2; i <= 3; i++){
        for(int j = 1; j <= n; j++){
            if(arr[i][j] == 0) return make_pair(i,j);
        }
    }
    return {0,0};
}
void go(){
    for(int i = 2; i <= 3; i++){
        for(int j = 1; j <= n; j++){
            if(arr[i][j] == 0) continue;
            if(i == 2 && arr[i][j]  == arr[i-1][j]){
//                cout << arr[i][j] <<' ' << i-1<<' ' << j <<"\n";
                vc.push_back({arr[i][j],i-1,j});
                arr[i][j] = 0;
                cnt++;
                return;
            }
            if(i == 3 && arr[i][j]  == arr[i+1][j]){
//                cout << arr[i][j] <<' ' << i+1<<' ' << j <<"\n";
                vc.push_back({arr[i][j],i+1,j});
                arr[i][j] = 0;
                cnt++;
                return;
            }
        }
    }
    cout << -1;
    exit(0);
}
int dy[5][1005];
int dx[5][1005];
pair<int,int> nextPos(int y,int x){
    if(y == 2 && x < n){
        return {y,x+1};
    }
    if(y == 2 && x == n){
        return {y+1,x};
    }
    if(y == 3 && x > 1){
        return {y,x-1};
    }
    if(y == 3 && x == 1){
        return {y-1,x};
    }
    return make_pair(-1,-1);
}
void print(){
    for(int i = 1; i <= 4; i++){
        for(int j = 1; j <= n; j++){
            cout << arr[i][j];
        }
        cout << endl;
    }
    cout << endl;
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;

    for(int j = 1; j <= 4; j++){
        for(int i = 1; i <= n; i++){
            cin >> arr[j][i];
        }
    }
    if(k == 2 * n){
        go();
    }
    while(cnt < k){
//        print();
        for(int i = 1; i <= 4; i++){
            for(int j = 1; j <= n; j++){
                if(arr[i][j] == 0) continue;
                if(i == 2 && arr[i][j]  == arr[i-1][j]){
//                    cout << arr[i][j] <<' ' << i-1<<' ' << j <<"\n";
                    vc.push_back({arr[i][j],i-1,j});
                    arr[i][j] = 0;
                    cnt++;
                    continue;
                }
                if(i == 3 && arr[i][j]  == arr[i+1][j]){
//                    cout << arr[i][j] <<' ' << i+1<<' ' << j <<"\n";
                    vc.push_back({arr[i][j],i+1,j});
                    arr[i][j] = 0;
                    cnt++;
                    continue;
                }
                pair<int,int> loc = nextPos(i,j);
                if(arr[loc.first][loc.second] == 0){
                    vc.push_back({arr[i][j],loc.first,loc.second});
//                    cout << arr[i][j] <<' '<< loc.first<<' ' <<loc.second << "\n";
                    arr[loc.first][loc.second] = arr[i][j];
                }
            }
        }
    }
    cout << vc.size()<<"\n";
    for(int i = 0; i < vc.size(); i++){
        cout << vc[i].a<<' '<<vc[i].b <<' ' <<vc[i].c << "\n";
    }
    return 0;
}

/*
 4 5
 1 2 0 4
 1 2 0 4
 5 0 0 3
 0 5 0 3
 1 1 1
 2 1 2
 4 1 4
 5 2 1
 3 4 4
 5 2 2
 5 2 3
 5 2 4
 5 3 4
 5 3 3
 5 3 2
 5 4 2
 5 3 2
 */


#include <iostream>
#include <cassert>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <set>
#include <numeric>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <queue>
#include <numeric>
#include <iomanip>
#define ll long long
using namespace std;
int arr[10][150];
int cnt = 0,n,k;
struct info{
    int a,b,c;
};
vector<info> vc;
void go(){
    for(int i = 2; i <= 3; i++){
        for(int j = 1; j <= n; j++){
            if(arr[i][j] == 0) continue;
            if(i == 2 && arr[i][j]  == arr[i-1][j]){
                vc.push_back({arr[i][j],i-1,j});
                arr[i][j] = 0;
                cnt++;
                return;
            }
            if(i == 3 && arr[i][j]  == arr[i+1][j]){
                vc.push_back({arr[i][j],i+1,j});
                arr[i][j] = 0;
                cnt++;
                return;
            }
        }
    }
    cout << -1;
    exit(0);
}
pair<int,int> nextPos(int y,int x){
    if(y == 2 && x < n){
        return {y,x+1};
    }
    if(y == 2 && x == n){
        return {y+1,x};
    }
    if(y == 3 && x > 1){
        return {y,x-1};
    }
    if(y == 3 && x == 1){
        return {y-1,x};
    }
    assert(false);
    return make_pair(-1,-1);
}
void print(){
    for(int i = 1; i <= 4; i++){
        for(int j = 1; j <= n; j++){
            cout << arr[i][j];
        }
        cout << endl;
    }
    cout << endl;
}
int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;

    for(int j = 1; j <= 4; j++){
        for(int i = 1; i <= n; i++){
            cin >> arr[j][i];
        }
    }
    if(k == 2 * n){
        go();
    }
    while(cnt < k && vc.size() <= 20000){
//        print();
        for(int i = 2; i <= 3; i++){
            for(int j = 1; j <= n; j++){
                if(arr[i][j] == 0) continue;
                if(i == 2 && arr[i][j]  == arr[i-1][j]){
//                    cout << arr[i][j] <<' ' << i-1<<' ' << j <<"\n";
                    vc.push_back({arr[i][j],i-1,j});
                    arr[i][j] = 0;
                    cnt++;
                    continue;
                }
                if(i == 3 && arr[i][j]  == arr[i+1][j]){
//                    cout << arr[i][j] <<' ' << i+1<<' ' << j <<"\n";
                    vc.push_back({arr[i][j],i+1,j});
                    arr[i][j] = 0;
                    cnt++;
                    continue;
                }
                pair<int,int> loc = nextPos(i,j);
                if(arr[loc.first][loc.second] == 0){
                    vc.push_back({arr[i][j],loc.first,loc.second});
//                    cout << arr[i][j] <<' '<< loc.first<<' ' <<loc.second << "\n";
                    arr[loc.first][loc.second] = arr[i][j];
                    arr[i][j] = 0;
                }
            }
        }
    }
    cout << vc.size()<<"\n";
    for(int i = 0; i < vc.size(); i++){
        cout << vc[i].a<<' '<<vc[i].b <<' ' <<vc[i].c << "\n";
    }
    return 0;
}

/*
 4 5
 1 2 0 4
 1 2 0 4
 5 0 0 3
 0 5 0 3
 1 1 1
 2 1 2
 4 1 4
 5 2 1
 3 4 4
 5 2 2
 5 2 3
 5 2 4
 5 3 4
 5 3 3
 5 3 2
 5 4 2
 5 3 2
 */