#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <ctime>
using namespace std;

int d[13] = {
	-14, -1, 0, 1, 14,
	-15, -13, 13, 15,
	-28, -2, 2, 28,
};
int to[3] = {5, 9, 13};

int c[196];
int nmb[196];
bool visit[196];
vector<vector<int> > lumps;
vector<int> sum;
vector<int> ans;

int draw(int x, int t, int dif){
	for(int i = 0; i < t; ++i){
		c[x + d[i]] += dif;
	}
}

int next(int t, int u){
	if(u == lumps[t].size()){ u = 0; }
	while(c[lumps[t][u]] == 0){
		++u;
		if(u == lumps[t].size()){ u = 0; }
	}
	return u;
}

bool check(int t){
	const vector<int> &v = lumps[t];
	memset(visit, 0, sizeof visit);
	int k = 0;
	for(int i = 0; i < v.size(); ++i){
		if(c[v[i]] == 0 || visit[v[i]]){ continue; }
		if(k > 0){ return false; }

		queue<int> q;
		q.push(v[i]);
		visit[v[i]] = true;
		while(!q.empty()){
			for(int j = 0; j < 5; ++j){
				int x = q.front() + d[j];
				if(c[x] > 0 && !visit[x]){
					q.push(x);
					visit[x] = true;
				}
			}
			q.pop();
		}
		++k;
	}
	return true;
}


void dfs(int n, int t, int u){
	if(t == lumps.size()){
		if(n == 0){
			for(int i = 0; i < ans.size(); ++i){
				int y = (ans[i] >> 2) / 14 - 2;
				int x = (ans[i] >> 2) % 14 - 2;
				int z = (ans[i] & 3) + 1;
				printf("%d %d %d\n", x, y, z);
			}
			exit(0);
		}
		return;
	}

//	if(!check(t)){ return; }

	int prev = u;
	do{
		int x = lumps[t][u];
		int m;
		for(m = 0; m < 13 && c[x + d[m]] > 0; ++m);
		for(int i = 2; i >= 0; --i){
			if(m >= to[i]){
				draw(x, to[i], -1);
				sum[t] -= to[i];
				ans.push_back(x << 2 | i);
				if(sum[t] == 0){
					dfs(n - 1, t + 1, 0);
				}
				else{
					dfs(n - 1, t, next(t, u));
				}
				ans.pop_back();
				sum[t] += to[i];
				draw(x, to[i], 1);
			}
		}

		u = next(t, u + 1);
	}while(u != prev);
}


int main(){
	srand(time(0));
	memset(nmb, -1, sizeof nmb);

	int n;
	scanf("%d", &n);
	for(int i = 0; i < 10; ++i){
		for(int j = 0; j < 10; ++j){
			scanf("%d", &c[(i + 2) * 14 + (j + 2)]);
		}
	}

	for(int i = 0; i < 196; ++i){
		if(c[i] != 0 && nmb[i] == -1){
			int s = 0;
			vector<int> lm;
			queue<int> q;
			q.push(i);
			nmb[i] = lumps.size();
			while(!q.empty()){
				lm.push_back(q.front());
				s += c[q.front()];
				for(int j = 0; j < 5; ++j){
					int x = q.front() + d[j];
					if(c[x] != 0 && nmb[x] == -1){
						q.push(x);
						nmb[x] = lumps.size();
					}
				}
				q.pop();
			}
			random_shuffle(lm.begin(), lm.end());
			lumps.push_back(lm);
			sum.push_back(s);
		}
	}

	dfs(n, 0, 0);
	return 1;
}