#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

int n;
vvint vv;
stack<pair<pii,int> > ans;
int dx1[]={0,-1,0,1,0};
int dy1[]={0,1,1,1,2};
int dx2[]={0,1,2,0,1,2,0,1,2};
int dy2[]={0,0,0,1,1,1,2,2,2};
int dx3[]={0,-1,0,1,-2,-1,0,1,2,-1,0,1,0};
int dy3[]={0,1,1,1,2,2,2,2,2,3,3,3,4};
bool foo(int x,int y,int c,int cnt);

bool check(int x,int y,int c,int d,int cnt){
	// cout<<"check "<<x<<" "<<y<<" "<<c<<" "<<d<<endl;
	if(c==0){
		rep(i,5){
			int yy=y+dy1[i],xx=x+dx1[i];
			if(0<=yy&&yy<10&&0<=xx&&x<10&&vv[yy][xx]>=1){
			}
			else return false;
		}
		rep(i,5){
			int yy=y+dy1[i],xx=x+dx1[i];
			vv[yy][xx]--;
		}
		ans.push(mkp(mkp(y+1,x),c));
		if(foo(x,y,d-1,cnt-5)) return true;
		rep(i,5){
			int yy=y+dy1[i],xx=x+dx1[i];
			vv[yy][xx]++;
		}
		ans.pop();
		return false;
	}
	if(c==1){
		rep(i,9){
			int yy=y+dy2[i],xx=x+dx2[i];
			if(0<=yy&&yy<10&&0<=xx&&x<10&&vv[yy][xx]>=1){
			}
			else return false;
		}
		rep(i,9){
			int yy=y+dy2[i],xx=x+dx2[i];
			vv[yy][xx]--;
		}
		ans.push(mkp(mkp(y+1,x+1),c));
		if(foo(x,y,d-1,cnt-9)) return true;
		rep(i,9){
			int yy=y+dy2[i],xx=x+dx2[i];
			vv[yy][xx]++;
		}
		ans.pop();
		return false;
	}
	if(c==2){
		rep(i,13){
			int yy=y+dy3[i],xx=x+dx3[i];
			if(0<=yy&&yy<10&&0<=xx&&x<10&&vv[yy][xx]>=1){
			}
			else return false;
		}
		rep(i,13){
			int yy=y+dy3[i],xx=x+dx3[i];
			vv[yy][xx]--;
		}
		ans.push(mkp(mkp(y+2,x),c));
		if(foo(x,y,d-1,cnt-13)) return true;
		rep(i,13){
			int yy=y+dy3[i],xx=x+dx3[i];
			vv[yy][xx]++;
		}
		ans.pop();
		return false;
	}
	return false;
}
bool foo(int x,int y,int c,int cnt){
	if(cnt<0) return false;
	// cout<<x<<" "<<y<<" "<<c<<endl;
	// rep(i,10){
		// rep(j,10){
			// cout<<vv[i][j]<<" ";
		// }
		// cout<<endl;
	// }
	// cout<<endl;
	int a;
	cin>>a;
	if(c==0&&cnt==0) return true;
	if(c==0) return false;
	rep(i,10){
		rep(j,10){
			if(vv[i][j]>=1){
				rep(k,3){
					if(check(j,i,2-k,c,cnt)) return true;
				}
			}
		}
	}
	return false;
}
void mainmain(){
	cin>>n;
	initvv(vv,10,10);
	int cnt=0;
	rep(i,10){
		rep(j,10){
			cin>>vv[i][j];
			cnt+=vv[i][j];
		}
	}
	foo(0,0,n,cnt);
	while(!ans.empty()){
		cout<<ans.top().F.S<<" "<<ans.top().F.F<<" "<<ans.top().S+1<<endl;
		ans.pop();
	}
}



}
main() try{
    mainmain();
}
catch(...){
}