#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 = 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
 */


#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;

ll n;

pair<ll, ll> spot(ll x) {
	if (x<n) return {1, x+1};
	return {4, n-(x-n)};
}

pair<ll, ll> place(ll x) {
	if (x<n) return {2, x+1};
	return {3, n-(x-n)};
}

#define NN 300
ll a[NN], b[NN], c[NN], d[NN];

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

	ll k;
	cin>>n>>k;
	for (ll i=0; i<n; ++i) cin>>a[i];
	for (ll i=0; i<n; ++i) cin>>b[i];
	for (ll i=0; i<n; ++i) cin>>c[i];
	for (ll i=0; i<n; ++i) cin>>d[i];
	reverse(c, c+n);
	reverse(d, d+n);
	for (ll i=0; i<n; ++i) {
		a[i+n] = d[i];
		b[i+n] = c[i];
	}
	vector<pair<ll, pair<ll, ll>>> moves;
	ll emp = -1;
	for (ll i=0; i<2*n; ++i) {
		if (a[i] && a[i]==b[i]) {
			moves.push_back({a[i], spot(i)});
			a[i] = b[i] = 0;
		}
		if (b[i]==0) emp=i;
	}

	if (emp==-1) {cout<<-1<<endl; return 0;}
	for (ll r=0; r<2*n; ++r) {
		ll cur = emp;
		for (ll j=0; j<2*n; ++j) {
			ll prev = (cur-1+2*n)%(2*n);
			if (b[prev]) {
				b[cur] = b[prev];
				b[prev] = 0;
				moves.push_back({b[cur], place(cur)});
			}
			cur = prev;
		}
		for (ll i=0; i<2*n; ++i) {
			if (a[i] && a[i]==b[i]) {
				moves.push_back({a[i], spot(i)});
				a[i] = b[i] = 0;
			}
		}
		emp = (emp+1)%(2*n);
	}
	cout<<moves.size()<<endl;
	for (auto m:moves) cout<<m.first<<" "<<m.second.first<<" "<<m.second.second<<endl;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;

ll n;

pair<ll, ll> spot(ll x) {
	if (x<n) return {1, x+1};
	return {4, n-(x-n)};
}

pair<ll, ll> place(ll x) {
	if (x<n) return {2, x+1};
	return {3, n-(x-n)};
}

#define NN 300
ll a[NN], b[NN], c[NN], d[NN];

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

	ll k;
	cin>>n>>k;
	for (ll i=0; i<n; ++i) cin>>a[i];
	for (ll i=0; i<n; ++i) cin>>b[i];
	for (ll i=0; i<n; ++i) cin>>c[i];
	for (ll i=0; i<n; ++i) cin>>d[i];
	reverse(c, c+n);
	reverse(d, d+n);
	for (ll i=0; i<n; ++i) {
		a[i+n] = d[i];
		b[i+n] = c[i];
	}
	vector<pair<ll, pair<ll, ll>>> moves;
	ll emp = -1;
	for (ll i=0; i<2*n; ++i) {
		if (a[i] && a[i]==b[i]) {
			moves.push_back({a[i], spot(i)});
			a[i] = b[i] = 0;
		}
		if (b[i]==0) emp=i;
	}

	if (emp==-1) {cout<<-1<<endl; return 0;}
	for (ll r=0; r<2*n; ++r) {
		ll cur = emp;
		for (ll j=0; j<2*n; ++j) {
			ll prev = (cur-1+2*n)%(2*n);
			if (b[prev]) {
				b[cur] = b[prev];
				b[(prev] = 0;
				moves.push_back({b[cur], place(cur)});
			}
			cur = prev;
		}
		for (ll i=0; i<2*n; ++i) {
			if (a[i] && a[i]==b[i]) {
				moves.push_back({a[i], spot(i)});
				a[i] = b[i] = 0;
			}
		}
		emp = (emp+1)%(2*n);
	}
	cout<<moves.size()<<endl;
	for (auto m:moves) cout<<m.first<<" "<<m.second.first<<" "<<m.second.second<<endl;
}


#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;

ll n;

pair<ll, ll> spot(ll x) {
	if (x<n) return {1, x+1};
	return {4, n-(x-n)};
}

pair<ll, ll> place(ll x) {
	if (x<n) return {2, x+1};
	return {3, n-(x-n)};
}

#define NN 300
ll a[NN], b[NN], c[NN], d[NN];

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

	ll k;
	cin>>n>>k;
	for (ll i=0; i<n; ++i) cin>>a[i];
	for (ll i=0; i<n; ++i) cin>>b[i];
	for (ll i=0; i<n; ++i) cin>>c[i];
	for (ll i=0; i<n; ++i) cin>>d[i];
	reverse(c, c+n);
	reverse(d, d+n);
	for (ll i=0; i<n; ++i) {
		a[i+n] = d[i];
		b[i+n] = c[i];
	}
	vector<pair<ll, pair<ll, ll>>> moves;
	ll emp = -1;
	for (ll i=0; i<2*n; ++i) {
		if (a[i] && a[i]==b[i]) {
			moves.push_back({a[i], spot(i)});
			a[i] = b[i] = 0;
		}
		if (b[i]==0) emp=i;
	}

	if (emp==-1) {cout<<-1<<endl; return 0;}
	for (ll r=0; r<2*n; ++r) {
		ll cur = emp;
		for (ll j=0; j<2*n; ++j) {
			if (b[(cur-1+2*n)%(2*n)]) {
				b[cur] = b[(cur-1 +2*n)%(2*n)];
				b[(cur-1 +2*n)%(2*n)] = 0;
				moves.push_back({b[cur], place(cur)});
			}
			cur = (cur-1+2*n)%(2*n);
		}
		for (ll i=0; i<2*n; ++i) {
			if (a[i] && a[i]==b[i]) {
				moves.push_back({a[i], spot(i)});
				a[i] = b[i] = 0;
			}
		}
		emp = (emp+1)%2*n;
	}
	cout<<moves.size()<<endl;
	for (auto m:moves) cout<<m.first<<" "<<m.second.first<<" "<<m.second.second<<endl;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (leftCars.size() > 0 && ans.size() < 20000) {
		vector<int> foundCars;

		vector<pair<int, int> > minDists;
		map<int, vector<vector<int> > > prevs;
		map<int, pair<int, int> > dests;
		map<int, int> badCars;

		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1 && ((i == 1 || i == 2) && field[i][j] == 0 || i == destX && j == destY)) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				prevs[car] = prev;
				dests[car] = { destX, destY };
				minDists.push_back({ dist[destX][destY], car });
			}

		}

		sort(minDists.begin(), minDists.end());
		if (minDists.size() > 0) {
			foundCars.push_back(minDists[0].second);

			int car = minDists[0].second;
			int destX = dests[car].first;
			int destY = dests[car].second;
			auto prev = prevs[car];

			vector<int> path;
			int curX = destX;
			int curY = destY;
			while (prev[curX][curY] != -1) {
				int prevMove = prev[curX][curY];
				path.push_back(prevMove);
				curX -= dx[prevMove];
				curY -= dy[prevMove];
			}

			field[curX][curY] = 0;
			field[destX][destY] = car;

			for (int i = path.size() - 1; i >= 0; i--) {
				ans.push_back({ car,{ curX + dx[path[i]], curY + dy[path[i]] } });
				curX += dx[path[i]];
				curY += dy[path[i]];
			}

			foundCars.push_back(car);
		}

		if (foundCars.size() == 0) {
			int emptyX = -1;
			int emptyY = -1;
			for (int i = 2; i >= 1; i--) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == 0) {
						emptyX = i;
						emptyY = j;
					}
				}
			}
			if (emptyX == -1) {
				break;
			}
			else {
				if (emptyX == 2) {
					if (field[1][emptyY] != 0) {
						ans.push_back({ field[1][emptyY], {2, emptyY} });
						field[2][emptyY] = field[1][emptyY];
						field[1][emptyY] = 0;
						emptyX = 1;
					}
				}
				if (emptyX == 1) {
					for (int j = emptyY - 1; j >= 0; j--) {
						if (field[emptyX][j] != 0) {
							ans.push_back({ field[emptyX][j], {emptyX, j + 1} });
							field[emptyX][j + 1] = field[emptyX][j];
							field[emptyX][j] = 0;
						}
					}
					if (field[2][0] != 0) {
						ans.push_back({ field[2][0], {1, 0} });
						field[1][0] = field[2][0];
						field[2][0] = 0;
					}
					for (int j = 1; j < n; j++) {
						if (field[2][j] != 0) {
							ans.push_back({ field[2][j], {2, j - 1} });
							field[2][j - 1] = field[2][j];
							field[2][j] = 0;
						}
					}
					if (field[1][n - 1] != 0) {
						ans.push_back({ field[1][n - 1], {2, n - 1} });
						field[2][n - 1] = field[1][n - 1];
						field[1][n - 1] = 0;
					}
					for (int j = n - 2; j > emptyY; j--) {
						if (field[1][j] != 0) {
							ans.push_back({ field[1][j], {1, j + 1} });
							field[1][j + 1] = field[1][j];
							field[1][j] = 0;
						}
					}
				}
			}
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0 && ans.size() <= 20000) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	ifstream cin("input.txt");
	ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (true) {
		vector<int> foundCars;
		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				vector<int> path;
				int curX = destX;
				int curY = destY;
				while (prev[curX][curY] != -1) {
					int prevMove = prev[curX][curY];
					path.push_back(prevMove);
					curX -= dx[prevMove];
					curY -= dy[prevMove];
				}
				for (int i = path.size() - 1; i >= 0; i--) {
					ans.push_back({ car, {curX + dx[path[i]], curY + dy[path[i]]} });
					curX += dx[path[i]];
					curY += dy[path[i]];
				}

				foundCars.push_back(car);
			}
		}

		if (foundCars.size() == 0) {
			break;
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (true) {
		vector<int> foundCars;
		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1 && (i == 1 || i == 2 || i == destX && j == destY)) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				vector<int> path;
				int curX = destX;
				int curY = destY;
				while (prev[curX][curY] != -1) {
					int prevMove = prev[curX][curY];
					path.push_back(prevMove);
					curX -= dx[prevMove];
					curY -= dy[prevMove];
				}
				for (int i = path.size() - 1; i >= 0; i--) {
					ans.push_back({ car, {curX + dx[path[i]], curY + dy[path[i]]} });
					curX += dx[path[i]];
					curY += dy[path[i]];
				}

				foundCars.push_back(car);
			}
		}

		if (foundCars.size() == 0) {
			break;
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (true) {
		vector<int> foundCars;
		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1 && ((i == 1 || i == 2) && field[i][j] == 0 || i == destX && j == destY)) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				vector<int> path;
				int curX = destX;
				int curY = destY;
				while (prev[curX][curY] != -1) {
					int prevMove = prev[curX][curY];
					path.push_back(prevMove);
					curX -= dx[prevMove];
					curY -= dy[prevMove];
				}

				field[curX][curY] = 0;
				field[destX][destY] = car;

				for (int i = path.size() - 1; i >= 0; i--) {
					ans.push_back({ car, {curX + dx[path[i]], curY + dy[path[i]]} });
					curX += dx[path[i]];
					curY += dy[path[i]];
				}

				foundCars.push_back(car);
			}
		}

		if (foundCars.size() == 0) {
			break;
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (true) {
		vector<int> foundCars;
		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				vector<int> path;
				int curX = destX;
				int curY = destY;
				while (prev[curX][curY] != -1) {
					int prevMove = prev[curX][curY];
					path.push_back(prevMove);
					curX -= dx[prevMove];
					curY -= dy[prevMove];
				}
				for (int i = path.size() - 1; i >= 0; i--) {
					ans.push_back({ car, {curX + dx[path[i]], curY + dy[path[i]]} });
					curX += dx[path[i]];
					curY += dy[path[i]];
				}

				foundCars.push_back(car);
			}
		}

		if (foundCars.size() == 0) {
			break;
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (leftCars.size() > 0 && ans.size() < 20000) {
		vector<int> foundCars;

		vector<pair<int, int> > minDists;
		map<int, vector<vector<int> > > prevs;
		map<int, pair<int, int> > dests;
		map<int, int> badCars;

		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1 && ((i == 1 || i == 2) && field[i][j] == 0 || i == destX && j == destY)) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				prevs[car] = prev;
				dests[car] = { destX, destY };
				minDists.push_back({ dist[destX][destY], car });
			}

		}

		sort(minDists.begin(), minDists.end());
		if (minDists.size() > 0) {
			foundCars.push_back(minDists[0].second);

			int car = minDists[0].second;
			int destX = dests[car].first;
			int destY = dests[car].second;
			auto prev = prevs[car];

			vector<int> path;
			int curX = destX;
			int curY = destY;
			while (prev[curX][curY] != -1) {
				int prevMove = prev[curX][curY];
				path.push_back(prevMove);
				curX -= dx[prevMove];
				curY -= dy[prevMove];
			}

			field[curX][curY] = 0;
			field[destX][destY] = car;

			for (int i = path.size() - 1; i >= 0; i--) {
				ans.push_back({ car,{ curX + dx[path[i]], curY + dy[path[i]] } });
				curX += dx[path[i]];
				curY += dy[path[i]];
			}

			foundCars.push_back(car);
		}

		if (foundCars.size() == 0) {
			int emptyX = -1;
			int emptyY = -1;
			for (int i = 2; i >= 1; i--) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == 0) {
						emptyX = i;
						emptyY = j;
					}
				}
			}
			if (emptyX == -1) {
				break;
			}
			else {
				if (emptyX == 2) {
					if (field[1][emptyY] != 0) {
						ans.push_back({ field[1][emptyY], {2, emptyY} });
						field[2][emptyY] = field[1][emptyY];
						field[1][emptyY] = 0;
						emptyX = 1;
					}
				}
				if (emptyX == 1) {
					for (int j = emptyY - 1; j >= 0; j--) {
						if (field[emptyX][j] != 0) {
							ans.push_back({ field[emptyX][j], {emptyX, j + 1} });
							field[emptyX][j + 1] = field[emptyX][j];
							field[emptyX][j] = 0;
						}
					}
					if (field[2][0] != 0) {
						ans.push_back({ field[2][0], {1, 0} });
						field[1][0] = field[2][0];
						field[2][0] = 0;
					}
					for (int j = 1; j < n; j++) {
						if (field[2][j] != 0) {
							ans.push_back({ field[2][j], {2, j - 1} });
							field[2][j - 1] = field[2][j];
							field[2][j] = 0;
						}
					}
					if (field[1][n - 1] != 0) {
						ans.push_back({ field[1][n - 1], {2, n - 1} });
						field[2][n - 1] = field[1][n - 1];
						field[1][n - 1] = 0;
					}
					for (int j = n - 2; j > emptyY; j++) {
						if (field[1][j] != 0) {
							ans.push_back({ field[1][j], {1, j + 1} });
							field[1][j + 1] = field[1][j];
							field[1][j] = 0;
						}
					}
				}
			}
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0 && ans.size() <= 20000) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (true) {
		vector<int> foundCars;

		vector<pair<int, int> > minDists;
		map<int, vector<vector<int> > > prevs;
		map<int, pair<int, int> > dests;

		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1 && ((i == 1 || i == 2) && field[i][j] == 0 || i == destX && j == destY)) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				prevs[car] = prev;
				dests[car] = { destX, destY };
				minDists.push_back({ dist[destX][destY], car });
			}

		}

		sort(minDists.begin(), minDists.end());
		if (minDists.size() > 0) {
			foundCars.push_back(minDists[0].second);

			int car = minDists[0].second;
			int destX = dests[car].first;
			int destY = dests[car].second;
			auto prev = prevs[car];

			vector<int> path;
			int curX = destX;
			int curY = destY;
			while (prev[curX][curY] != -1) {
				int prevMove = prev[curX][curY];
				path.push_back(prevMove);
				curX -= dx[prevMove];
				curY -= dy[prevMove];
			}

			field[curX][curY] = 0;
			field[destX][destY] = car;

			for (int i = path.size() - 1; i >= 0; i--) {
				ans.push_back({ car,{ curX + dx[path[i]], curY + dy[path[i]] } });
				curX += dx[path[i]];
				curY += dy[path[i]];
			}

			foundCars.push_back(car);
		}

		if (foundCars.size() == 0) {
			break;
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0 && ans.size() <= 20000) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <functional>
#include <stdlib.h>
#include <bitset>
#include <random>
#include <sstream>
#include <numeric>

using namespace std;

const int PARKING = 10000;

int field[10][100];

int dx[] = { -1, 1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };

int main() {
	ios_base::sync_with_stdio(false);
	ifstream cin("input.txt");
	//ofstream cout("output.txt");

	int n, k;
	cin >> n >> k;

	set<int> leftCars;
	for (int i = 0; i < k; i++) {
		leftCars.insert(i + 1);
	}

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < n; j++) {
			cin >> field[i][j];
			if (i == 0 || i == 3 && field[i][j] != 0) {
				field[i][j] += PARKING;
			}
		}
	}

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

	while (leftCars.size() > 0 && ans.size() < 20000) {
		vector<int> foundCars;

		vector<pair<int, int> > minDists;
		map<int, vector<vector<int> > > prevs;
		map<int, pair<int, int> > dests;
		map<int, int> badCars;

		for (int car : leftCars) {
			vector<vector<int> > dist(4, vector<int>(n, -1));
			vector<vector<int> > prev(4, vector<int>(n, -1));

			int destX, destY;

			queue <pair<int, pair<int, int> > > q;
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == car) {
						q.push({ 0, {i, j} });
						dist[i][j] = 0;
					}
					else if (field[i][j] == car + PARKING) {
						destX = i;
						destY = j;
					}
				}
			}

			while (!q.empty()) {
				int l = q.front().first;
				int x = q.front().second.first;
				int y = q.front().second.second;
				q.pop();

				for (int we = 0; we < 4; we++) {
					int i = x + dx[we];
					int j = y + dy[we];
					if (i >= 0 && i < 4 && j >= 0 && j < n && dist[i][j] == -1 && ((i == 1 || i == 2) && field[i][j] == 0 || i == destX && j == destY)) {
						dist[i][j] = l + 1;
						prev[i][j] = we;
						q.push({ l + 1, {i, j} });
					}
				}
			}

			if (dist[destX][destY] != -1) {
				prevs[car] = prev;
				dests[car] = { destX, destY };
				minDists.push_back({ dist[destX][destY], car });
			}

		}

		sort(minDists.begin(), minDists.end());
		if (minDists.size() > 0) {
			foundCars.push_back(minDists[0].second);

			int car = minDists[0].second;
			int destX = dests[car].first;
			int destY = dests[car].second;
			auto prev = prevs[car];

			vector<int> path;
			int curX = destX;
			int curY = destY;
			while (prev[curX][curY] != -1) {
				int prevMove = prev[curX][curY];
				path.push_back(prevMove);
				curX -= dx[prevMove];
				curY -= dy[prevMove];
			}

			field[curX][curY] = 0;
			field[destX][destY] = car;

			for (int i = path.size() - 1; i >= 0; i--) {
				ans.push_back({ car,{ curX + dx[path[i]], curY + dy[path[i]] } });
				curX += dx[path[i]];
				curY += dy[path[i]];
			}

			foundCars.push_back(car);
		}

		if (foundCars.size() == 0) {
			int emptyX = -1;
			int emptyY = -1;
			for (int i = 2; i >= 1; i--) {
				for (int j = 0; j < n; j++) {
					if (field[i][j] == 0) {
						emptyX = i;
						emptyY = j;
					}
				}
			}
			if (emptyX == -1) {
				break;
			}
			else {
				if (emptyX == 2) {
					if (field[1][emptyY] != 0) {
						ans.push_back({ field[1][emptyY], {2, emptyY} });
						field[2][emptyY] = field[1][emptyY];
						field[1][emptyY] = 0;
						emptyX = 1;
					}
				}
				if (emptyX == 1) {
					for (int j = emptyY - 1; j >= 0; j--) {
						if (field[emptyX][j] != 0) {
							ans.push_back({ field[emptyX][j], {emptyX, j + 1} });
							field[emptyX][j + 1] = field[emptyX][j];
							field[emptyX][j] = 0;
						}
					}
					if (field[2][0] != 0) {
						ans.push_back({ field[2][0], {1, 0} });
						field[1][0] = field[2][0];
						field[2][0] = 0;
					}
					for (int j = 1; j < n; j++) {
						if (field[2][j] != 0) {
							ans.push_back({ field[2][j], {2, j - 1} });
							field[2][j - 1] = field[2][j];
							field[2][j] = 0;
						}
					}
					if (field[1][n - 1] != 0) {
						ans.push_back({ field[1][n - 1], {2, n - 1} });
						field[2][n - 1] = field[1][n - 1];
						field[1][n - 1] = 0;
					}
					for (int j = n - 2; j > emptyY; j++) {
						if (field[1][j] != 0) {
							ans.push_back({ field[1][j], {1, j + 1} });
							field[1][j + 1] = field[1][j];
							field[1][j] = 0;
						}
					}
				}
			}
		}
		else {
			for (int i = 0; i < foundCars.size(); i++) {
				leftCars.erase(foundCars[i]);
			}
		}
	}

	if (leftCars.size() == 0 && ans.size() <= 20000) {
		cout << ans.size() << endl;
		for (int i = 0; i < ans.size(); i++) {
			cout << ans[i].first << " " << ans[i].second.first + 1 << " " << ans[i].second.second + 1 << endl;
		}
	}
	else {
		cout << -1;
	}

	//system("pause");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 2e4 + 7;

int n;
pair <int, pair <int, int> > an[N];
vector <vector <int> > a, b;
map <vector <vector <int> >, bool> p;

void upd(int i, int j, int x, int y, int v) {
	an[v] = {a[i][j], {i, j}};
	swap(a[i][j], a[x][y]);
}

void rec(int v) {
	if (p.count(a))
		return;
	p[a] = 1;
	if (a == b) {
		printf("%d\n", v);
		for (int i = v - 1; i >= 0; --i)
			printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
		exit(0);
	}
	if (v == 1000)
		return;
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j])
				continue;
			if (i < 3 && !a[i + 1][j]) {
				upd(i, j, i + 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i + 1][j]);
			}
			if (i > 2 && !a[i - 1][j]) {
				upd(i, j, i - 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i - 1][j]);
			}
			if (j < n && !a[i][j + 1]) {
				upd(i, j, i, j + 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j + 1]);
			}
			if (j > 1 && !a[i][j - 1]) {
				upd(i, j, i, j - 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j - 1]);
			}
		}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int k;
	scanf("%d %d", &n, &k);
	vector <int> w;
	for (int j = 0; j <= 54; ++j)
		w.pb(0);
	for (int i = 0; i <= 6; ++i) {
		a.pb(w);
		b.pb(w);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[1][i]);
	for (int i = 2; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &b[i][j]);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[4][i]);
	rec(0);
	printf("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 2e4 + 7;

int n;
pair <int, pair <int, int> > an[N];
vector <vector <int> > a, b;
map <vector <vector <int> >, bool> p;

void upd(int i, int j, int x, int y, int v) {
	an[v] = {a[i][j], {i, j}};
	swap(a[i][j], a[x][y]);
}

void rec(int v) {
	if (p.count(a))
		return;
	p[a] = 1;
	if (a == b) {
		printf("%d\n", v);
		for (int i = v - 1; i >= 0; --i)
			printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
		exit(0);
	}
	else if (v == 200)
		return;
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j])
				continue;
			if (i < 3 && !a[i + 1][j]) {
				upd(i, j, i + 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i + 1][j]);
			}
			if (i > 2 && !a[i - 1][j]) {
				upd(i, j, i - 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i - 1][j]);
			}
			if (j < n && !a[i][j + 1]) {
				upd(i, j, i, j + 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j + 1]);
			}
			if (j > 1 && !a[i][j - 1]) {
				upd(i, j, i, j - 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j - 1]);
			}
		}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int k;
	scanf("%d %d", &n, &k);
	vector <int> w;
	for (int j = 0; j <= 50; ++j)
		w.pb(0);
	for (int i = 0; i <= 4; ++i) {
		a.pb(w);
		b.pb(w);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[1][i]);
	for (int i = 2; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &b[i][j]);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[4][i]);
	rec(0);
	printf("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 2e4 + 7;

int n;
pair <int, pair <int, int> > an[N];
vector <vector <int> > a, b;
map <vector <vector <int> >, bool> p;

void upd(int i, int j, int x, int y, int v) {
	an[v] = {a[i][j], {i, j}};
	swap(a[i][j], a[x][y]);
}

void rec(int v) {
	if (p.count(a))
		return;
	p[a] = 1;
	if (a == b) {
		printf("%d\n", v);
		for (int i = v - 1; i >= 0; --i)
			printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
		exit(0);
	}
	if (v == 20000)
		return;
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j])
				continue;
			if (i < 3 && !a[i + 1][j]) {
				upd(i, j, i + 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i + 1][j]);
			}
			if (i > 2 && !a[i - 1][j]) {
				upd(i, j, i - 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i - 1][j]);
			}
			if (j < n && !a[i][j + 1]) {
				upd(i, j, i, j + 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j + 1]);
			}
			if (j > 1 && !a[i][j - 1]) {
				upd(i, j, i, j - 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j - 1]);
			}
		}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int k;
	scanf("%d %d", &n, &k);
	vector <int> w;
	for (int j = 0; j <= 54; ++j)
		w.pb(0);
	for (int i = 0; i <= 6; ++i) {
		a.pb(w);
		b.pb(w);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[1][i]);
	for (int i = 2; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &b[i][j]);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[4][i]);
	rec(0);
	printf("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 2e4 + 7;

int n;
pair <int, pair <int, int> > an[N];
vector <vector <int> > a, b;
map <vector <vector <int> >, bool> p;

void upd(int i, int j, int x, int y, int v) {
	an[v] = {a[i][j], {i, j}};
	swap(a[i][j], a[x][y]);
}

void rec(int v) {
	if (p.count(a))
		return;
	p[a] = 1;
	if (a == b) {
		printf("%d\n", v);
		for (int i = v - 1; i >= 0; --i)
			printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
		exit(0);
	}
	if (v == 500)
		return;
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j])
				continue;
			if (i < 3 && !a[i + 1][j]) {
				upd(i, j, i + 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i + 1][j]);
			}
			if (i > 2 && !a[i - 1][j]) {
				upd(i, j, i - 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i - 1][j]);
			}
			if (j < n && !a[i][j + 1]) {
				upd(i, j, i, j + 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j + 1]);
			}
			if (j > 1 && !a[i][j - 1]) {
				upd(i, j, i, j - 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j - 1]);
			}
		}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int k;
	scanf("%d %d", &n, &k);
	vector <int> w;
	for (int j = 0; j <= 54; ++j)
		w.pb(0);
	for (int i = 0; i <= 6; ++i) {
		a.pb(w);
		b.pb(w);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[1][i]);
	for (int i = 2; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &b[i][j]);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[4][i]);
	rec(0);
	printf("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 2e4 + 7;

int n;
pair <int, pair <int, int> > an[N];
vector <vector <int> > a, b;
map <vector <vector <int> >, bool> p;

void upd(int i, int j, int x, int y, int v) {
	an[v] = {a[i][j], {i, j}};
	swap(a[i][j], a[x][y]);
}

void rec(int v) {
	if (p.count(a))
		return;
	p[a] = 1;
	if (a == b) {
		printf("%d\n", v);
		for (int i = v - 1; i >= 0; --i)
			printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
		exit(0);
	}
	if (v == 100)
		return;
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j])
				continue;
			if (i < 3 && !a[i + 1][j]) {
				upd(i, j, i + 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i + 1][j]);
			}
			if (i > 2 && !a[i - 1][j]) {
				upd(i, j, i - 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i - 1][j]);
			}
			if (j < n && !a[i][j + 1]) {
				upd(i, j, i, j + 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j + 1]);
			}
			if (j > 1 && !a[i][j - 1]) {
				upd(i, j, i, j - 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j - 1]);
			}
		}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int k;
	scanf("%d %d", &n, &k);
	vector <int> w;
	for (int j = 0; j <= 50; ++j)
		w.pb(0);
	for (int i = 0; i <= 4; ++i) {
		a.pb(w);
		b.pb(w);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[1][i]);
	for (int i = 2; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &b[i][j]);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[4][i]);
	rec(0);
	printf("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 2e4 + 7;

int n;
pair <int, pair <int, int> > an[N];
vector <vector <int> > a, b;
map <vector <vector <int> >, bool> p;

void upd(int i, int j, int x, int y, int v) {
	an[v] = {a[i][j], {i, j}};
	swap(a[i][j], a[x][y]);
}

void rec(int v) {
	if (p.count(a))
		return;
	p[a] = 1;
	if (a == b) {
		printf("%d\n", v);
		for (int i = v - 1; i >= 0; --i)
			printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
		exit(0);
	}
	if (v == 200)
		return;
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j])
				continue;
			if (i < 3 && !a[i + 1][j]) {
				upd(i, j, i + 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i + 1][j]);
			}
			if (i > 2 && !a[i - 1][j]) {
				upd(i, j, i - 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i - 1][j]);
			}
			if (j < n && !a[i][j + 1]) {
				upd(i, j, i, j + 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j + 1]);
			}
			if (j > 1 && !a[i][j - 1]) {
				upd(i, j, i, j - 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j - 1]);
			}
		}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int k;
	scanf("%d %d", &n, &k);
	vector <int> w;
	for (int j = 0; j <= 50; ++j)
		w.pb(0);
	for (int i = 0; i <= 4; ++i) {
		a.pb(w);
		b.pb(w);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[1][i]);
	for (int i = 2; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &b[i][j]);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[4][i]);
	rec(0);
	printf("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 55;
const int M = 2e4 + 7;

int k, a[5][N];
int ans;
pair <int, pair <int, int> > an[M];

void upd(int i, int j, int x, int y) {
	int t = (x == 2 ? 1 : 4);
	swap(a[i][j], a[x][y]);
	an[++ans] = {a[x][y], {x, y}};
	if (a[x][y] == a[t][y]) {
		--k;
		an[++ans] = {a[x][y], {t, y}};
		a[x][y] = 0;
	}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int n;
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &a[i][j]);
	for (int j = 1; j <= n; ++j) {
		if (a[2][j] == a[1][j] && a[2][j]) {
			--k;
			an[++ans] = {a[2][j], {1, j}};
			a[2][j] = 0;
		}
		if (a[3][j] == a[4][j] && a[3][j]) {
			--k;
			an[++ans] = {a[3][j], {4, j}};
			a[3][j] = 0;
		}
	}
	if (k == (n << 1)) {
		puts("-1");
		return 0;
	}
	while (k) {
		for (int i = 1; i < n; ++i)
			if (a[2][i] && !a[2][i + 1])
				upd(2, i, 2, i + 1);
		if (a[2][n] && !a[3][n])
			upd(2, n, 3, n);
		for (int i = n; i > 1; --i)
			if (a[3][i] && !a[3][i - 1])
				upd(3, i, 3, i - 1);
		if (a[3][1] && !a[2][1])
			upd(3, 1, 2, 1);
	}
	printf("%d\n", ans);
	for (int i = 1; i <= ans; ++i)
		printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

const int N = 2e4 + 7;

int n;
pair <int, pair <int, int> > an[N];
vector <vector <int> > a, b;
map <vector <vector <int> >, bool> p;

void upd(int i, int j, int x, int y, int v) {
	an[v] = {a[i][j], {i, j}};
	swap(a[i][j], a[x][y]);
}

void rec(int v) {
	if (p.count(a))
		return;
	p[a] = 1;
	if (a == b) {
		printf("%d\n", v);
		for (int i = v - 1; i >= 0; --i)
			printf("%d %d %d\n", an[i].f, an[i].s.f, an[i].s.s);
		exit(0);
	}
	else if (v == 100)
		return;
	for (int i = 1; i <= 4; ++i)
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j])
				continue;
			if (i < 3 && !a[i + 1][j]) {
				upd(i, j, i + 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i + 1][j]);
			}
			if (i > 2 && !a[i - 1][j]) {
				upd(i, j, i - 1, j, v);
				rec(v + 1);
				swap(a[i][j], a[i - 1][j]);
			}
			if (j < n && !a[i][j + 1]) {
				upd(i, j, i, j + 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j + 1]);
			}
			if (j > 1 && !a[i][j - 1]) {
				upd(i, j, i, j - 1, v);
				rec(v + 1);
				swap(a[i][j], a[i][j - 1]);
			}
		}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	int k;
	scanf("%d %d", &n, &k);
	vector <int> w;
	for (int j = 0; j <= 50; ++j)
		w.pb(0);
	for (int i = 0; i <= 4; ++i) {
		a.pb(w);
		b.pb(w);
	}
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[1][i]);
	for (int i = 2; i <= 3; ++i)
		for (int j = 1; j <= n; ++j)
			scanf("%d", &b[i][j]);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[4][i]);
	rec(0);
	printf("-1");
	return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define Str(x) to_string(x)
#define len(s) size(s)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

struct Move
{
	int id, i, j;
};

int main()
{
	ios::sync_with_stdio(0);
	int n, k;
	cin >> n >> k;
	int **park = new int*[4];
	for (int i = 0; i < 4; ++i)
	{
		park[i] = new int[n];
		for (int j = 0; j < n; ++j)
			cin >> park[i][j];
	}
	vec<Move> ans;
	int empty = 0;
	for (int i = 0; i < n; ++i)
	{
		if (park[1][i] == park[0][i] && park[1][i])
		{
			--k;
			ans.pb({park[1][i], 1, i + 1});
			park[1][i] = 0;
		}
		if (park[2][i] == park[3][i] && park[2][i])
		{
			--k;
			ans.pb({park[2][i], 4, i + 1});
			park[2][i] = 0;
		}
		empty += !park[1][i] + !park[2][i];
	}
	if (!empty)
	{
		cout << -1;
		return 0;
	}
	while (k)
	{
		for (int i = 1; i < n; ++i)
		{
			if (!park[1][i] && park[1][i - 1])
			{
				swap(park[1][i - 1], park[1][i]);
				ans.pb({park[1][i], 2, i + 1});
				if (park[1][i] == park[0][i])
				{
					ans.pb({park[1][i], 1, i + 1});
					park[1][i] = 0;
					--k;
				}
			}
		}
		for (int i = n - 1; i > 0; --i)
		{
			if (!park[2][i - 1] && park[2][i])
			{
				swap(park[2][i - 1], park[2][i]);
				ans.pb({park[2][i - 1], 3, i});
				if (park[2][i - 1] == park[3][i - 1])
				{
					ans.pb({park[2][i - 1], 4, i});
					park[2][i - 1] = 0;
					--k;
				}
			}
		}
		if (park[1][n - 1] && !park[2][n - 1])
		{
			ans.pb({park[1][n - 1], 3, n});
			swap(park[1][n - 1], park[2][n - 1]);
		}
		if (park[2][n - 1] == park[3][n - 1] && park[2][n - 1])
		{
			ans.pb({ park[2][n - 1], 4, n });
			park[2][n - 1] = 0;
			--k;
		}
		if (park[2][0] && !park[1][0])
		{
			ans.pb({park[2][0], 2, 1});
			swap(park[2][0], park[1][0]);
		}
		if (park[1][0] == park[0][0] && park[0][0])
		{
			ans.pb({park[1][0], 1, 1});
			park[1][0] = 0;
			--k;
		}
	}
	cout << size(ans) << endl;
	for (auto &it : ans)
		cout << it.id << ' ' << it.i << ' ' << it.j << endl;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define Str(x) to_string(x)
#define len(s) size(s)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

struct Move
{
	int id, i, j;
};

int main()
{
	ios::sync_with_stdio(0);
	int n, k;
	cin >> n >> k;
	int **park = new int*[4];
	for (int i = 0; i < 4; ++i)
	{
		park[i] = new int[n];
		for (int j = 0; j < n; ++j)
			cin >> park[i][j];
	}
	vec<Move> ans;
	int empty = 0;
	for (int i = 0; i < n; ++i)
	{
		if (park[1][i] == park[0][i] && park[1][i])
		{
			--k;
			ans.pb({park[1][i], 1, i + 1});
			park[1][i] = 0;
		}
		if (park[2][i] == park[3][i] && park[2][i])
		{
			--k;
			ans.pb({park[2][i], 4, i + 1});
			park[2][i] = 0;
		}
		empty += !park[1][i] + !park[2][i];
	}
	if (!empty)
	{
		cout << -1;
		return 0;
	}
	while (k)
	{
		for (int i = 1; i < n; ++i)
		{
			if (!park[1][i] && park[1][i - 1])
			{
				swap(park[1][i - 1], park[1][i]);
				ans.pb({park[1][i], 2, i + 1});
				if (park[1][i] == park[0][i])
				{
					ans.pb({park[1][i], 1, i + 1});
					park[1][i] = 0;
					--k;
				}
			}
		}
		for (int i = n - 1; i > 0; --i)
		{
			if (!park[2][i - 1] && park[2][i])
			{
				swap(park[2][i - 1], park[2][i]);
				ans.pb({park[2][i - 1], 3, i});
				if (park[2][i - 1] == park[3][i - 1])
				{
					ans.pb({park[2][i - 1], 4, i});
					park[2][i - 1] = 0;
					--k;
				}
			}
		}
		if (park[1][n - 1] && !park[2][n - 1])
		{
			ans.pb({park[1][n - 1], 3, n});
			swap(park[1][n - 1], park[2][n - 1]);
		}
		if (park[2][0] && !park[1][0])
		{
			ans.pb({park[2][0], 2, 1});
			swap(park[2][0], park[1][0]);
		}
	}
	cout << size(ans) << endl;
	for (auto &it : ans)
		cout << it.id << ' ' << it.i << ' ' << it.j << endl;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define Str(x) to_string(x)
#define len(s) size(s)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

struct Move
{
	int id, i, j;
};

int main()
{
	ios::sync_with_stdio(0);
	int n, k;
	cin >> n >> k;
	int **park = new int*[4];
	for (int i = 0; i < 4; ++i)
	{
		park[i] = new int[n];
		for (int j = 0; j < n; ++j)
			cin >> park[i][j];
	}
	vec<Move> ans;
	int empty = 0;
	for (int i = 0; i < n; ++i)
	{
		if (park[1][i] == park[0][i] && park[1][i])
		{
			--k;
			ans.pb({park[1][i], 1, i + 1});
			park[1][i] = 0;
		}
		if (park[2][i] == park[3][i] && park[2][i])
		{
			--k;
			ans.pb({park[2][i], 4, i + 1});
			park[2][i] = 0;
		}
		empty += !park[1][i] + !park[2][i];
	}
	if (!empty)
	{
		cout << -1;
		return 0;
	}
	while (k)
	{
		for (int i = 1; i < n; ++i)
		{
			if (!park[1][i] && park[1][i - 1])
			{
				swap(park[1][i - 1], park[1][i]);
				ans.pb({park[1][i], 2, i + 1});
				if (park[1][i] == park[0][i])
				{
					ans.pb({park[1][i], 1, i + 1});
					park[1][i] = 0;
					--k;
				}
			}
		}
		for (int i = n - 1; i > 0; --i)
		{
			if (!park[2][i - 1] && park[2][i])
			{
				swap(park[2][i - 1], park[2][i]);
				ans.pb({park[2][i - 1], 3, i});
				if (park[2][i - 1] == park[3][i - 1])
				{
					ans.pb({park[2][i - 1], 4, i});
					park[2][i - 1] = 0;
					--k;
				}
			}
		}
		if (park[1][n - 1] && !park[2][n - 1])
		{
			ans.pb({park[1][n - 1], 3, n});
			swap(park[1][n - 1], park[2][n - 1]);
		}
		if (park[2][0] && !park[1][0])
		{
			ans.pb({park[2][0], 2, 1});
			swap(park[2][0], park[1][0]);
		}
		if (park[2][n - 1] == park[3][n - 1] && park[2][n - 1])
		{
			ans.pb({park[2][n - 1], 4, n});
			park[2][n - 1] = 0;
			--k;
		}
		if (park[1][0] == park[0][0] && park[0][0])
		{
			ans.pb({park[1][0], 1, 1});
			park[1][0] = 0;
			--k;
		}
	}
	cout << size(ans) << endl;
	for (auto &it : ans)
		cout << it.id << ' ' << it.i << ' ' << it.j << endl;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define vec vector
#define pb push_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define Str(x) to_string(x)
#define len(s) size(s)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

struct Move
{
	char id;
	int i, j;
};

int main()
{
	ios::sync_with_stdio(0);
	int n, k;
	cin >> n >> k;
	str up, down, mid1, mid2, mid;
	for (int i = 0; i < n; ++i)
	{
		char t;
		cin >> t;
		up += t;
	}
	for (int i = 0; i < n; ++i)
	{
		char t;
		cin >> t;
		mid1 += t;
	}
	for (int i = 0; i < n; ++i)
	{
		char t;
		cin >> t;
		mid2 += t;
	}
	for (int i = 0; i < n; ++i)
	{
		char t;
		cin >> t;
		down += t;
	}
	vec<Move> ans;
	int empty = 0;
	for (int i = 0; i < n; ++i)
	{
		if (mid1[i] == up[i] && up[i] != '0')
		{
			--k;
			mid1[i] = '0';
			ans.pb({up[i], 1, i + 1});
		}
		if (mid2[i] == down[i] && mid2[i] != '0')
		{
			mid2[i] = '0';
			--k;
			ans.pb({down[i], 4, i + 1});
		}
		empty += (mid1[i] == '0') + (mid2[i] == '0');
	}
	if (!empty)
	{
		cout << -1;
		return 0;
	}
	mid = mid1 + mid2;
	while (k)
	{
		for (int i = 0; i < 2 * n; ++i)
		{
			if (mid[i] != '0' && mid[(i + 1) % (2 * n)] == '0')
			{
				int j = (i + 1) % (2 * n);
				while (mid[j] == '0')
				{
					j %= 2 * n;
					if (j >= n && mid[i] == down[j % n])
					{
						for (int id = i + 1; id <= j; ++id)
							ans.pb({ mid[i], id >= n ? 3 : 2, id % n + 1 });
						ans.pb({mid[i], 4, (i + 1) % n + 1});
						mid[i] = '0';
						--k;
						break;
					}
					else if (j < n && mid[i] == up[j % n])
					{
						for (int id = i + 1; id <= j; ++id)
							ans.pb({ mid[i], id >= n ? 3 : 2, id % n + 1 });
						ans.pb({ mid[i], 1, (i + 1) % n + 1});
						mid[i] = '0';
						--k;
						break;
					}
				}
				swap(mid[i], mid[(j - 1) < 0? 2 * n - 1 : j - 1]);
				break;
			}
		}
	}
	cout << size(ans) << endl;
	for (auto &it : ans)
		cout << it.id << ' ' << it.i << ' ' << it.j << endl;
}


// Problem : A. Tesla
// Contest : Codeforces - Codeforces Round #492 (Div. 1) [Thanks, uDebug!]
// URL : https://codeforces.com/contest/995/problem/A
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*
 A Submission by $%U%$
 at time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define fef(i,a,b) for(ll i=a;i<=b;i++)
#define rer(i,a,b) for(ll i=b;i>=a;i--)
#define wew while(true)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define ord(a,b,c) ((a>=b)and(b>=c))
#define MOD (ll(1000000007))
#define MAX 300001
#define mag 320
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define mk_piii(a,b,c) {a,{b,c}}
#define pow2(x) (ll(1)<<x)
#define pii pair<int,int>
#define piii pair<int,pii>
#define For(i,__,___) for(int i=__;i<=___;i++)
#define Rep(i,__,___) for(int i=__;i>=___;i--)
#define ordered_set tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update>
#define endl "\n"
#define bi BigInt
typedef long long ll;
//---------END-------//
int n,m,k,c[501],pos[100001],t,t1,i,j,cnt;
int mp[5][101];
vector<piii> moves;
void check (int sa, int sb, int ta, int tb)
{

	if ((mp[sa][sb]!=0)and(mp[ta][tb]==0))
	{
	moves.push_back(mk_piii(mp[sa][sb],ta,tb));
	mp[ta][tb]=mp[sa][sb];
	mp[sa][sb]=0;
	}
}
void pus()
{
	for (int i=1;i<=n;i++)
	{
		if ((mp[2][i]==mp[1][i])and(mp[1][i]))
		{
			moves.push_back(mk_piii(mp[2][i],1,i));
			mp[2][i]=0;
			cnt++;
		}
		if ((mp[3][i]==mp[4][i])and(mp[4][i]))
		{
			moves.push_back(mk_piii(mp[3][i],4,i));
			mp[3][i]=0;
			cnt++;
		}
	}
}
int main()
{
//  ofile;
    fio;
    cin>>n>>k;
    for (i=1;i<=4;i++)
    {
    	for (j=1;j<=n;j++) cin>>mp[i][j];
    }
    for (t=1;t<=3*n;t++)
    {
    	pus();
 	  	for (i=1;i<=n;i++) if (i==1) check(2,i,3,i); else check(2,i,2,i-1);
  		for (i=n;i>=1;i--) if (i==n) check(3,i,2,i); else check(3,i,3,i+1);
   		pus();
    }
    if (cnt<k) cout<<-1;
    else
    {
    	cout<<moves.size()<<endl;
    	for (piii pp : moves) cout<<pp.p1<<' '<<pp.p2<<' '<<pp.p3<<endl;
    }
}


// Problem : A. Tesla
// Contest : Codeforces - Codeforces Round #492 (Div. 1) [Thanks, uDebug!]
// URL : https://codeforces.com/contest/995/problem/A
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*
 A Submission by $%U%$
 at time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define fef(i,a,b) for(ll i=a;i<=b;i++)
#define rer(i,a,b) for(ll i=b;i>=a;i--)
#define wew while(true)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define ord(a,b,c) ((a>=b)and(b>=c))
#define MOD (ll(1000000007))
#define MAX 300001
#define mag 320
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define mk_piii(a,b,c) {a,{b,c}}
#define pow2(x) (ll(1)<<x)
#define pii pair<int,int>
#define piii pair<int,pii>
#define For(i,__,___) for(int i=__;i<=___;i++)
#define Rep(i,__,___) for(int i=__;i>=___;i--)
#define ordered_set tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update>
#define endl "\n"
#define bi BigInt
typedef long long ll;
//---------END-------//
int n,m,k,c[501],pos[100001],t,t1,i,j,cnt;
int mp[5][101];
vector<piii> moves;
void check (int sa, int sb, int ta, int tb)
{

	if ((mp[sa][sb]!=0)and(mp[ta][tb]==0))
	{
	moves.push_back(mk_piii(mp[sa][sb],ta,tb));
	mp[ta][tb]=mp[sa][sb];
	mp[sa][sb]=0;
	}
}
void pus()
{
	for (int i=1;i<=n;i++)
	{
		if ((mp[2][i]==mp[1][i])and(mp[1][i]))
		{
			moves.push_back(mk_piii(mp[2][i],1,i));
			mp[2][i]=0;
			cnt++;
		}
		if ((mp[3][i]==mp[4][i])and(mp[4][i]))
		{
			moves.push_back(mk_piii(mp[3][i],4,i));
			mp[3][i]=0;
			cnt++;
		}
	}
}
int main()
{
//  ofile;
    fio;
    cin>>n>>k;
    for (i=1;i<=4;i++)
    {
    	for (j=1;j<=n;j++) cin>>mp[i][j];
    }
    for (t=1;t<=3*n;t++)
    {
    	pus();
 	  	for (i=1;i<=n;i++) if (i==1) check(2,i,3,i); else check(2,i,2,i-1);
   		pus();
  		for (i=n;i>=1;i--) if (i==n) check(3,i,2,i); else check(3,i,3,i+1);
   		pus();
    }
    if (cnt<k) cout<<-1;
    else
    {
    	cout<<moves.size()<<endl;
    	for (piii pp : moves) cout<<pp.p1<<' '<<pp.p2<<' '<<pp.p3<<endl;
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ul unsigned long
#define ld long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

typedef tree
<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update
> or_st;

int N=998244353 ,M=5e5;
int i,j;
int x,y,z,n,m,k,l,r,t,ma=0,x2,y2,z2,x3,y3,x4,y4,f,a[410000],b[410000],dp[510000],d[510000],p[510000];
int ans,ans2;
string s,s2,s3;
char c[510000];
vector<int>v3;

struct pos
{
    int x,y,z;
};

vector<pos>v,v2;

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>n;
    for(i=1;i<=n;i++)dp[i]=1;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(x<0)dp[i]*=-1,x*=-1,y*=-1;
        if(y>0)v.push_back(pos{x,y,i});
        else v2.push_back(pos{x,y,i});
    }

}

/*
5 10
1 2 3 4 5
1 2 3 4 5
6 7 8 9 10
6 7 8 9 10
*/


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ul unsigned long
#define ld long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

typedef tree
<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update
> or_st;

int N=998244353 ,M=5e5;
int i,j;
int x,y,z,n,m,k,l,r,t,ma=0,x2,y2,z2,x3,y3,x4,y4,f,a[3][110000],b[3][110000],dp[510000],d[510000],p[510000];
int ans,ans2;
string s,s2,s3;
char c[510000];
vector<int>v2[510000],v3;

struct pos
{
    int x,y,z;
};
vector<pos>v;

void sl()
{
    if(x==1)
    {
        if(y==1)x=2,y=1;
        else y--;
    }
    else
    {
        if(y==n)x=1,y=n;
        else y++;
    }
}

void pr()
{
    if(l==1)
    {
        if(r==n)l=2,r=n;
        else r++;
    }
    else
    {
        if(r==1)l=1,r=1;
        else r--;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>n>>m;
    for(j=1;j<=n;j++)
    {
        cin>>b[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[2][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>b[2][j];
    }
    if(m==n*2)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)if(b[i][j]!=a[i][j])f=1;
        }
        if(f)cout<<-1;
        else
        {
            cout<<m<<endl;
            for(i=1;i<=2;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(i==1)cout<<a[i][j]<<' '<<1<<' '<<j<<endl;
                    else cout<<a[i][j]<<' '<<4<<' '<<j<<endl;
                }
            }
        }
        return 0;
    }
    for(i=1;i<=n;i++)
    {
        if(!a[1][i])l=1,r=i;
        if(!a[2][i])l=2,r=i;
    }
    while(m)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(a[i][j]&&a[i][j]==b[i][j])
                {
                    if(i==1)v.push_back(pos{a[i][j],1,j});
                    else v.push_back(pos{a[i][j],4,j});
                    m--;
                    a[i][j]=0;
                }
            }
        }
        if(!m)break;
        x=l;y=r;
        for(i=1;i<2*n;i++)
        {
            x2=x;y2=y;
            sl();
            if(!a[x][y])continue;
            v.push_back(pos{a[x][y],x2+1,y2});
            swap(a[x][y],a[x2][y2]);
        }
        pr();
       // if((int)v.size()>1e5){cout<<-1;return 0;}
        //cout<<(int)v.size()<<' '<<l<<' '<<r<<' '<<m<<endl;
      //  for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;
    }
    cout<<(int)v.size()<<endl;
    for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;

}
/*
2 3
1 2
2 0
1 3
3 0
*/


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ul unsigned long
#define ld long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

typedef tree
<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update
> or_st;

int N=998244353 ,M=5e5;
int i,j;
int x,y,z,n,m,k,l,r,t,ma=0,x2,y2,z2,x3,y3,x4,y4,f,a[3][110000],b[3][110000],dp[510000],d[510000],p[510000];
int ans,ans2;
string s,s2,s3;
char c[510000];
vector<int>v2[510000],v3;

struct pos
{
    int x,y,z;
};
vector<pos>v;

void sl()
{
    if(x==1)
    {
        if(y==1)x=2,y=1;
        else y--;
    }
    else
    {
        if(y==n)x=1,y=n;
        else y++;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>n>>m;
    for(j=1;j<=n;j++)
    {
        cin>>b[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[2][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>b[2][j];
    }
    if(m==n*2)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)if(b[i][j]!=a[i][j])f=1;
        }
        if(f)cout<<-1;
        else
        {
            cout<<m<<endl;
            for(i=1;i<=2;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(i==1)cout<<a[i][j]<<' '<<1<<' '<<j<<endl;
                    else cout<<a[i][j]<<' '<<4<<' '<<j<<endl;
                }
            }
        }
        return 0;
    }
    for(i=1;i<=n;i++)
    {
        if(!a[1][i])l=1,r=i;
        if(!a[2][i])l=2,r=i;
    }
    f=m;
    while(f)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(a[i][j]&&a[i][j]==b[i][j])
                {
                    if(i==1)v.push_back(pos{a[i][j],1,j});
                    else v.push_back(pos{a[i][j],4,j});
                    f--;
                    a[i][j]=0;
                }
            }
        }
        if(!f)break;
        x=l;y=r;
        for(i=1;i<=2*n;i++)
        {
            x2=x;y2=y;
            sl();
            if(!a[x][y])continue;
            v.push_back(pos{a[x][y],x2+1,y2});
            swap(a[x][y],a[x2][y2]);
        }
        if((int)v.size()>1e5){cout<<-1;return 0;}
        //cout<<(int)v.size()<<endl;
       // for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;
    }
    cout<<(int)v.size()<<endl;
    for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ul unsigned long
#define ld long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

typedef tree
<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update
> or_st;

ll N=998244353 ,M=5e5;
ll i,j;
ll x,y,z,n,m,k,l,r,t,ma=0,x2,y2,z2,x3,y3,x4,y4,f,a[3][110000],b[3][110000],dp[510000],d[510000],p[510000];
ll ans,ans2;
string s,s2,s3;
char c[510000];
vector<ll>v2[510000],v3;

struct pos
{
    ll x,y,z;
};
vector<pos>v;

void sl()
{
    if(x==1)
    {
        if(y==1)x=2,y=1;
        else y--;
    }
    else
    {
        if(y==n)x=1,y=n;
        else y++;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>n>>m;
    for(j=1;j<=n;j++)
    {
        cin>>b[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[2][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>b[2][j];
    }
    if(m==n*2)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)if(b[i][j]!=a[i][j])f=1;
        }
        if(f)cout<<-1;
        else
        {
            cout<<m<<endl;
            for(i=1;i<=2;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(i==1)cout<<a[i][j]<<' '<<1<<' '<<j<<endl;
                    else cout<<a[i][j]<<' '<<4<<' '<<j<<endl;
                }
            }
        }
        return 0;
    }
    for(i=1;i<=n;i++)
    {
        if(!a[1][i])l=1,r=i;
        if(!a[2][i])l=2,r=i;
    }
    f=m;
    while(f)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(a[i][j]&&a[i][j]==b[i][j])
                {
                    if(i==1)v.push_back(pos{a[i][j],1,j});
                    else v.push_back(pos{a[i][j],4,j});
                    f--;
                    a[i][j]=0;
                }
            }
        }
        if(!f)break;
        x=l;y=r;
        for(i=1;i<=2*n;i++)
        {
            x2=x;y2=y;
            sl();
            if(!a[x][y])continue;
            v.push_back(pos{a[x][y],x2+1,y2});
            swap(a[x][y],a[x2][y2]);
        }
        //cout<<(int)v.size()<<endl;
       // for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;
    }
    cout<<(int)v.size()<<endl;
    for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;

}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ul unsigned long
#define ld long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

typedef tree
<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update
> or_st;

int N=998244353 ,M=5e5;
int i,j;
int x,y,z,n,m,k,l,r,t,ma=0,x2,y2,z2,x3,y3,x4,y4,f,a[3][110000],b[3][110000],dp[510000],d[510000],p[510000];
int ans,ans2;
string s,s2,s3;
char c[510000];
vector<int>v2[510000],v3;

struct pos
{
    int x,y,z;
};
vector<pos>v;

void sl()
{
    if(x==1)
    {
        if(y==1)x=2,y=1;
        else y--;
    }
    else
    {
        if(y==n)x=1,y=n;
        else y++;
    }
}

void pr()
{
    if(l==1)
    {
        if(r==n)l=2,r=n;
        else r++;
    }
    else
    {
        if(r==1)l=1,r=1;
        else r--;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>n>>m;
    for(j=1;j<=n;j++)
    {
        cin>>b[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[1][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>a[2][j];
    }
    for(j=1;j<=n;j++)
    {
        cin>>b[2][j];
    }
    for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(a[i][j]&&a[i][j]==b[i][j])
                {
                    if(i==1)v.push_back(pos{a[i][j],1,j});
                    else v.push_back(pos{a[i][j],4,j});
                    m--;
                    a[i][j]=0;
                }
            }
        }
    if(m==n*2)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)if(b[i][j]!=a[i][j])f=1;
        }
        if(f)cout<<-1;
        else
        {
            cout<<m<<endl;
            for(i=1;i<=2;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(i==1)cout<<a[i][j]<<' '<<1<<' '<<j<<endl;
                    else cout<<a[i][j]<<' '<<4<<' '<<j<<endl;
                }
            }
        }
        return 0;
    }
    for(i=1;i<=n;i++)
    {
        if(!a[1][i])l=1,r=i;
        if(!a[2][i])l=2,r=i;
    }
    while(m)
    {
        for(i=1;i<=2;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(a[i][j]&&a[i][j]==b[i][j])
                {
                    if(i==1)v.push_back(pos{a[i][j],1,j});
                    else v.push_back(pos{a[i][j],4,j});
                    m--;
                    a[i][j]=0;
                }
            }
        }
        if(!m)break;
        x=l;y=r;
        for(i=1;i<2*n;i++)
        {
            x2=x;y2=y;
            sl();
            if(!a[x][y])continue;
            v.push_back(pos{a[x][y],x2+1,y2});
            swap(a[x][y],a[x2][y2]);
        }
        pr();
       // if((int)v.size()>1e5){cout<<-1;return 0;}
        //cout<<(int)v.size()<<' '<<l<<' '<<r<<' '<<m<<endl;
      //  for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;
    }
    cout<<(int)v.size()<<endl;
    for(i=0;i<v.size();i++)cout<<v[i].x<<' '<<v[i].y<<' '<<v[i].z<<endl;

}
/*
2 3
1 2
2 0
1 3
3 0
*/


#include <bits/stdc++.h>
using namespace std;
const int N=55;
int n, k, a[5][N];
vector<tuple<int,int,int>> ans;
bool done(){
  return accumulate(a[2], a[2]+n+1, 0)+
         accumulate(a[3], a[3]+n+1, 0)==0;
}
int to[]={0,0,1,4};
void push_in(){
  for(int i=2; i<=3; i++)
    for(int j=1; j<=n; j++)
      if(a[i][j] and a[i][j]==a[to[i]][j]){
        ans.push_back(make_tuple(a[i][j], to[i], j));
        a[i][j]=a[to[i]][j]=0;
      }
}
vector<pair<int,int>> pos;
bool move(){
  int si=-1, sj=-1;
  for(int i=2; i<=3; i++)
    for(int j=1; j<=n; j++)
      if(a[i][j] == 0)
        si=i, sj=j;
  if(si == -1) return false;
  size_t at=0;
  while(pos[at] != make_pair(si, sj)) at++;
  for(size_t i=1; i<pos.size(); i++){
    int curi, curj;
    tie(curi, curj)=pos[(at+i)%pos.size()];
    int toi, toj;
    tie(toi, toj)=pos[(at+i-1)%pos.size()];
    if(a[curi][curj]){
      ans.push_back(make_tuple(a[curi][curj], toi, toj));
      a[toi][toj]=a[curi][curj];
      a[curi][curj]=0;
    }
  }
  return true;
}
int main(){
  cin>>n>>k;
  for(int i=1; i<=4; i++)
    for(int j=1; j<=n; j++)
      cin>>a[i][j];
  for(int i=1; i<=n; i++)
    pos.push_back({2,i});
  for(int i=n; i>=1; i--)
    pos.push_back({3,i});
  while(not done()){
    push_in();
    if(not move()){
      puts("-1");
      exit(0);
    }
  }
  printf("%d\n", (int)ans.size());
  for(auto i: ans)
    printf("%d %d %d\n",
           get<0>(i),
           get<1>(i),
           get<2>(i));
}


#include <bits/stdc++.h>
using namespace std;
const int N=55;
int n, k, a[5][N];
vector<tuple<int,int,int>> ans;
bool done(){
  return accumulate(a[2], a[2]+n+1, 0)+
         accumulate(a[3], a[3]+n+1, 0)==0;
}
int to[]={0,0,1,4};
void push_in(){
  for(int i=2; i<=3; i++)
    for(int j=1; j<=n; j++)
      if(a[i][j] and a[i][j]==a[to[i]][j]){
        ans.push_back(make_tuple(a[i][j], to[i], j));
        a[i][j]=a[to[i]][j]=0;
      }
}
vector<pair<int,int>> pos;
bool move(){
  int si=-1, sj=-1;
  for(int i=2; i<=3; i++)
    for(int j=1; j<=n; j++)
      if(a[i][j] == 0)
        si=i, sj=j;
  if(si == -1) return false;
  size_t at=0;
  while(pos[at] != make_pair(si, sj)) at++;
  for(size_t i=1; i<pos.size(); i++){
    int curi, curj;
    tie(curi, curj)=pos[i];
    int toi, toj;
    tie(toi, toj)=pos[i-1];
    if(a[curi][curj]){
      ans.push_back(make_tuple(a[curi][curj], toi, toj));
      a[toi][toj]=a[curi][curj];
      a[curi][curj]=0;
    }
  }
  return true;
}
int main(){
  cin>>n>>k;
  for(int i=1; i<=4; i++)
    for(int j=1; j<=n; j++)
      cin>>a[i][j];
  for(int i=1; i<=n; i++)
    pos.push_back({2,i});
  for(int i=n; i>=1; i--)
    pos.push_back({3,i});
  while(not done()){
    push_in();
    if(not move()){
      puts("-1");
      exit(0);
    }
  }
  printf("%d\n", (int)ans.size());
  for(auto i: ans)
    printf("%d %d %d\n",
           get<0>(i),
           get<1>(i),
           get<2>(i));
}


#include <bits/stdc++.h>
using namespace std;
const int N=55;
int n, k, a[5][N];
vector<tuple<int,int,int>> ans;
bool done(){
  return accumulate(a[2], a[2]+n+1, 0)+
         accumulate(a[3], a[3]+n+1, 0)==0;
}
int to[]={0,0,1,4};
void push_in(){
  for(int i=2; i<=3; i++)
    for(int j=1; j<=n; j++)
      if(a[i][j]==a[to[i]][j]){
        ans.push_back(make_tuple(a[i][j], to[i], j));
        a[i][j]=a[to[i]][j]=0;
      }
}
vector<pair<int,int>> pos;
bool move(){
  int si=-1, sj=-1;
  for(int i=2; i<=3; i++)
    for(int j=1; j<=n; j++)
      if(a[i][j] == 0)
        si=i, sj=j;
  if(si == -1) return false;
  size_t at=0;
  while(pos[at] != make_pair(si, sj)) at++;
  for(size_t i=1; i<pos.size(); i++){
    int curi, curj;
    tie(curi, curj)=pos[i];
    int toi, toj;
    tie(toi, toj)=pos[i-1];
    if(a[curi][curj]){
      ans.push_back(make_tuple(a[curi][curj], toi, toj));
      a[toi][toj]=a[curi][curj];
      a[curi][curj]=0;
    }
  }
  return true;
}
int main(){
  cin>>n>>k;
  for(int i=1; i<=4; i++)
    for(int j=1; j<=n; j++)
      cin>>a[i][j];
  for(int i=1; i<=n; i++)
    pos.push_back({2,i});
  for(int i=n; i>=1; i--)
    pos.push_back({3,i});
  while(not done()){
    push_in();
    if(not move()){
      puts("-1");
      exit(0);
    }
  }
  printf("%d\n", (int)ans.size());
  for(auto i: ans)
    printf("%d %d %d\n",
           get<0>(i),
           get<1>(i),
           get<2>(i));
}


#pragma GCC optimize("O3")
#define first x
#define second y
#include <bits/stdc++.h>

using namespace std;

enum {MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009};
typedef unsigned short ushort;typedef unsigned int uint;typedef long long ll;typedef unsigned long long ull;
#ifndef CrafterKolyan
#define endl '\n'
#endif

int a[4][50];
vector<pair<int, pair<int, int>>> ans;

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

int main() {
#ifndef CrafterKolyan
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    srand(time(0));
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }
    bool good = false;
    while (k) {
        for (int i = 0; i < n; ++i) {
            if (a[1][i]) {
                if (a[0][i] == a[1][i]) {
                    ans.push_back({a[1][i], pair<int, int>{1, i + 1}});
                    good = true;
                    --k;
                    a[0][i] = a[1][i] = 0;
                }
            }
            if (a[2][i]) {
                if (a[2][i] == a[3][i]) {
                    ans.push_back({a[2][i], pair<int, int>{4, i + 1}});
                    good = true;
                    --k;
                    a[2][i] = a[3][i] = 0;
                }
            }
        }
        if (!good) {
            cout << -1 << endl;
            return 0;
        }
        pair<int, int> cur;
        for (int i = 0; i < n; ++i) {
            if (!a[1][i]) {
                cur.x = 1;
                cur.y = i;
                break;
            } else if (!a[2][i]) {
                cur.x = 2;
                cur.y = i;
                break;
            }
        }
        for (int i = 0; i < 2 * n; ++i) {
            pair<int, int> next = nxt(cur, n);
            if (a[next.x][next.y]) {
                ans.push_back({a[next.x][next.y], pair<int, int>{cur.x + 1, cur.y + 1}});
                a[cur.x][cur.y] = a[next.x][next.y];
                a[next.x][next.y] = 0;
            }
            cur = next;
        }
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i].x << " " << ans[i].y.x << " " << ans[i].y.y << endl;
    }
    return 0;
}


#pragma GCC optimize("O3")
#define first x
#define second y
#include <bits/stdc++.h>

using namespace std;

enum {MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009};
typedef unsigned short ushort;typedef unsigned int uint;typedef long long ll;typedef unsigned long long ull;
#ifndef CrafterKolyan
#define endl '\n'
#endif

int a[4][50];
int b[4][50];

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

vector<pair<int, pair<int, int>>> ans;
int cc = 0;

void try_put(int x, int y, int n) {
    memset(b, 0, sizeof(b));
    b[x][y] = 1;
    queue<pair<int, int>> q;
    q.push({x, y});
    pair<int, int> cur;
    while (!q.empty()) {
        cur = q.front();
        q.pop();
        if (a[cur.x][cur.y] == a[x][y] && cur.x != x) {
            break;
        }
        for (int i = 0; i < 4; ++i) {
            pair<int, int> newc = {cur.x + dx[i], cur.y + dy[i]};
            if (newc.x >= 1 && newc.x <= 2 && newc.y >= 0 && newc.y <= n - 1) {
                if (!b[newc.x][newc.y] && (!a[newc.x][newc.y] || a[newc.x][newc.y] == a[x][y])) {
                    b[newc.x][newc.y] = b[cur.x][cur.y] + 1;
                    q.push(newc);
                }
            }
        }
    }
    if (a[cur.x][cur.y] != a[x][y] || cur.x == x) {
        return;
    }
    a[cur.x][cur.y] = 0;
    while (cur.x != x || cur.y != y) {
        for (int i = 0; i < 4; ++i) {
            pair<int, int> newc = {cur.x + dx[i], cur.y + dy[i]};
            if (newc.x >= 0 && newc.x <= 3 && newc.y >= 0 && newc.y <= n - 1) {
                if (b[newc.x][newc.y] + 1 == b[cur.x][cur.y]) {
                    ans.push_back({a[x][y], pair<int, int>{newc.x + 1, newc.y + 1}});
                    cur = newc;
                    break;
                }
            }
        }
    }
    a[x][y] = 0;
    ++cc;
}

bool try_move(int x, int y, int n) {
    for (int i = 0; i < 4; ++i) {
        pair<int, int> newc = {x + dx[i], y + dy[i]};
        if (newc.x >= 1 && newc.x <= 2 && newc.y >= 0 && newc.y <= n - 1) {
            if (!a[newc.x][newc.y]) {
                ans.push_back({a[x][y], pair<int, int>{newc.x + 1, newc.y + 1}});
                a[newc.x][newc.y] = a[x][y];
                a[x][y] = 0;
                return true;
            }
        }
    }
    return false;
}

int main() {
#ifndef CrafterKolyan
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    srand(time(0));
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }
    int prev = cc;
    while (cc < k) {
        prev = cc;
        for (int i = 0; i < n; ++i) {
            if (a[0][i]) {
                try_put(0, i, n);
            } else if (a[3][i]) {
                try_put(3, i, n);
            }
        }
        if (cc == prev) {
            bool bad = true;
            for (int i = 0; i < n; ++i) {
                int rnd = rand() % n;
                if (a[1][rnd]) {
                    if(try_move(1, rnd, n)){
                        bad = false;
                        break;
                    }
                }
                if (a[2][rnd]) {
                    if(try_move(2, rnd, n)) {
                        bad = false;
                        break;
                    }
                }
            }
            if (bad) {
                for (int i = 0; i < n; ++i) {
                    if (a[1][i]) {
                        if(try_move(1, i, n)){
                            bad = false;
                            break;
                        }
                    }
                    if (a[2][i]) {
                        if(try_move(2, i, n)) {
                            bad = false;
                            break;
                        }
                    }
                }
            }
            if (bad) {
                ans.resize(20000);
                break;
            }
        }
        if (ans.size() >= 20000) {
            break;
        }
    }
    if (ans.size() >= 20000) {
        cout << -1 << endl;
        return 0;
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i].x << " " << ans[i].y.x << " " << ans[i].y.y << endl;
    }
    return 0;
}


#pragma GCC optimize("O3")
#define first x
#define second y
#include <bits/stdc++.h>

using namespace std;

enum {MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009};
typedef unsigned short ushort;typedef unsigned int uint;typedef long long ll;typedef unsigned long long ull;
#ifndef CrafterKolyan
#define endl '\n'
#endif

int a[4][50];
int b[4][50];

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

vector<pair<int, pair<int, int>>> ans;
int cc = 0;

void try_put(int x, int y, int n) {
    memset(b, 0, sizeof(b));
    b[x][y] = 1;
    queue<pair<int, int>> q;
    q.push({x, y});
    pair<int, int> cur;
    while (!q.empty()) {
        cur = q.front();
        q.pop();
        if (a[cur.x][cur.y] == a[x][y] && cur.x != x) {
            break;
        }
        for (int i = 0; i < 4; ++i) {
            pair<int, int> newc = {cur.x + dx[i], cur.y + dy[i]};
            if (newc.x >= 1 && newc.x <= 2 && newc.y >= 0 && newc.y <= n - 1) {
                if (!b[newc.x][newc.y] && (!a[newc.x][newc.y] || a[newc.x][newc.y] == a[x][y])) {
                    b[newc.x][newc.y] = b[cur.x][cur.y] + 1;
                    q.push(newc);
                }
            }
        }
    }
    if (a[cur.x][cur.y] != a[x][y] || cur.x == x) {
        return;
    }
    a[cur.x][cur.y] = 0;
    while (cur.x != x || cur.y != y) {
        for (int i = 0; i < 4; ++i) {
            pair<int, int> newc = {cur.x + dx[i], cur.y + dy[i]};
            if (newc.x >= 0 && newc.x <= 3 && newc.y >= 0 && newc.y <= n - 1) {
                if (b[newc.x][newc.y] + 1 == b[cur.x][cur.y]) {
                    ans.push_back({a[x][y], pair<int, int>{newc.x + 1, newc.y + 1}});
                    cur = newc;
                    break;
                }
            }
        }
    }
    a[x][y] = 0;
    ++cc;
}

bool try_move(int x, int y, int n) {
    for (int i = 0; i < 4; ++i) {
        pair<int, int> newc = {x + dx[i], y + dy[i]};
        if (newc.x >= 1 && newc.x <= 2 && newc.y >= 0 && newc.y <= n - 1) {
            if (!a[newc.x][newc.y]) {
                ans.push_back({a[x][y], pair<int, int>{newc.x + 1, newc.y + 1}});
                a[newc.x][newc.y] = a[x][y];
                a[x][y] = 0;
                return true;
            }
        }
    }
    return false;
}

int main() {
#ifndef CrafterKolyan
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    srand(time(0));
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }
    int prev = cc;
    while (cc < k) {
        prev = cc;
        for (int i = 0; i < n; ++i) {
            if (a[0][i]) {
                try_put(0, i, n);
            } else if (a[3][i]) {
                try_put(3, i, n);
            }
        }
        if (cc == prev) {
            bool bad = true;
            for (int i = 0; i < n; ++i) {
                int rnd = rand() % n;
                if (a[1][rnd]) {
                    if(try_move(1, rnd, n)){
                        bad = false;
                        break;
                    }
                }
                if (a[2][i]) {
                    if(try_move(2, rnd, n)) {
                        bad = false;
                        break;
                    }
                }
            }
            for (int i = 0; i < n; ++i) {
                if (a[1][i]) {
                    if(try_move(1, i, n)){
                        bad = false;
                        break;
                    }
                }
                if (a[2][i]) {
                    if(try_move(2, i, n)) {
                        bad = false;
                        break;
                    }
                }
            }
            if (bad) {
                ans.resize(20000);
                break;
            }
        }
        if (ans.size() >= 20000) {
            break;
        }
    }
    if (ans.size() >= 20000) {
        cout << -1 << endl;
        return 0;
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i].x << " " << ans[i].y.x << " " << ans[i].y.y << endl;
    }
    return 0;
}


#pragma GCC optimize("O3")
#define first x
#define second y
#include <bits/stdc++.h>

using namespace std;

enum {MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009};
typedef unsigned short ushort;typedef unsigned int uint;typedef long long ll;typedef unsigned long long ull;
#ifndef CrafterKolyan
#define endl '\n'
#endif

int a[4][50];
vector<pair<int, pair<int, int>>> ans;

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

int main() {
#ifndef CrafterKolyan
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    srand(time(0));
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }
    while (k) {
        for (int i = 0; i < n; ++i) {
            if (a[1][i]) {
                if (a[0][i] == a[1][i]) {
                    ans.push_back({a[1][i], pair<int, int>{1, i + 1}});
                    --k;
                    a[0][i] = a[1][i] = 0;
                }
            }
            if (a[2][i]) {
                if (a[2][i] == a[3][i]) {
                    ans.push_back({a[2][i], pair<int, int>{4, i + 1}});
                    --k;
                    a[2][i] = a[3][i] = 0;
                }
            }
        }
        if (k == 2 * n) {
            cout << -1 << endl;
            return 0;
        }
        pair<int, int> cur;
        for (int i = 0; i < n; ++i) {
            if (!a[1][i]) {
                cur.x = 1;
                cur.y = i;
                break;
            } else if (!a[2][i]) {
                cur.x = 2;
                cur.y = i;
                break;
            }
        }
        for (int i = 1; i < 2 * n; ++i) {
            pair<int, int> next = nxt(cur, n);
            if (a[next.x][next.y]) {
                ans.push_back({a[next.x][next.y], pair<int, int>{cur.x + 1, cur.y + 1}});
                a[cur.x][cur.y] = a[next.x][next.y];
                a[next.x][next.y] = 0;
            }
            cur = next;
        }
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i].x << " " << ans[i].y.x << " " << ans[i].y.y << endl;
    }
    return 0;
}


/*input
1 2
1
1
2
2
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i,j,k)     for(int i = j ; i < k ; ++i)
#define RREP(i,j,k)    for(int i = j ; i >=k ; --i)
#define A    first
#define B    second
#define mp   make_pair
#define pb   emplace_back
#define PII pair<int , int>
#define MEM(i,j)   memset(i , j , sizeof i)
#define ALL(i)     i.begin() , i.end()
#define DBGG(i,j)     cout << i << " " << j << endl
#define DB4(i,j,k,l)  cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie(0) , cout.sync_with_stdio(0)
// #define endl "\n"
///------------------------------------------------------------
#define MAX 200
#define INF 0x3f3f3f3f

struct N{
    int id , x , y;
};
vector<N> ans;
int n , k , in , a[MAX] , aa[MAX] , b[MAX] , bb[MAX] , u[MAX];
PII x[MAX] , wt[MAX];
int can(int now){
    if(abs(wt[now].A - x[now].A) == 1 && wt[now].B == x[now].B){
    //     DBGG("now = " , now);
    // DBGG(wt[now].A , wt[now].B);
    // DBGG(x[now].A , x[now].B);
    // exit(0);
        ans.pb(N{now , wt[now].A , wt[now].B});
        u[now] = 1;
        return 1;
    }
    else return 0;
}
int put(int idx , PII now){
    // DB4("why = " , now.A , now.B , "");
    REP(i , 1 , k + 1) if(u[i] == 0 && x[i] == now) return 0;
    x[idx] = now;
    ans.pb(N{idx , now.A , now.B});

    // cout << "fack" << endl;
    // for(auto to : ans) cout << to.id << " " << to.x << " " << to.y << endl;
    return 1;
}
int solve(int now){
    // cout << "here" << endl;
    // cout << x[now].A << " " << x[now].B << endl;
    if(x[now].A == 2){
        if(x[now].B == 1){
            if(put(now , mp(3 , 1))) return 1;
        }
        else if(put(now , mp(2 , x[now].B - 1))) return 1;
    }
    if(x[now].A == 3){
        if(x[now].B == n){
            if(put(now , mp(2 , n))) return 1;
        }
        else if(put(now , mp(3 , x[now].B + 1))) return 1;
    }
    return 0;
}
int32_t main(){
    IOS;
    cin >> n >> k;
    REP(i , 1 , n + 1) cin >> a[i] , wt[a[i]] = mp(1 , i);
    REP(i , 1 , n + 1) {
        cin >> in , aa[i] = in;
        if(in != 0) x[in] = mp(2 , i);
    }
    REP(i , 1 , n + 1){
        cin >> in , bb[i] = in;
        if(in != 0) x[in] = mp(3 , i);
    }
    REP(i , 1 , n + 1) cin >> b[i] , wt[b[i]] = mp(4 , i);

    vector<int> sol;
    REP(i , 1 , n + 1) if(aa[i] != 0) sol.pb(aa[i]);
    RREP(i , n , 1)    if(bb[i] != 0) sol.pb(bb[i]);

    int ok = 0;
    // REP(i , 1 , k + 1) DBGG(x[i].A , x[i].B);
    // return 0;
    while(ok != k){
        REP(i , 1 , k + 1) if(u[i] == 0 && can(i)) u[i] = 1 , ok ++;
        int move = 0 , pos = 0;
        while(move != k - ok){
            if(u[sol[pos]] == 1) pos = (pos + 1) % sol.size();
            else {
                if(solve(sol[pos])) move ++;
                pos = (pos + 1) % sol.size();
            }
    // for(auto to : ans) cout << to.id << " " << to.x << " " << to.y << endl;
    //     return 0;
    //      if(pos == 0 && move == 0){
    //          return cout << -1 << endl , 0;
    //      }
            if(pos == 0 && move == 0) return cout << -1 << endl , 0;
        }
    }
    cout << ans.size() << endl;
    for(auto to : ans) cout << to.id << " " << to.x << " " << to.y << endl;
    return 0;
}