#include <stdio.h>
#include <random>
#include <time.h>

const int dropsize[3] = { 13, 9, 5 };

int countstain(int drops[3])
{
	return drops[0] * dropsize[0] + drops[1] * dropsize[1] + drops[2] * dropsize[2];
}

int findnextcombi(int n,int drops[3])
{
	do
	{
		if(drops[1] == 0)
		{
			if(drops[0] == 0) return false;
			
			drops[0]--;
			drops[1]+=(1+drops[2]);
			drops[2]=0;
		}
		else
		{
			drops[1]--;
			drops[2]++;
		}
	}
	while(n != countstain(drops));
	return true;
}

int testdrop(int c[10][10],int d,int x,int y)
{
	if(c[x][y]==0 || c[x-1][y]==0 || c[x+1][y]==0 || c[x][y-1]==0 || c[x][y+1]==0) return false;
	if(d == 2) return true;
	if(c[x-1][y-1]==0 || c[x+1][y-1]==0 || c[x-1][y+1]==0 || c[x+1][y+1]==0) return false;
	if(d == 1) return true;
	if(c[x-2][y]==0 || c[x+2][y]==0 || c[x][y-2]==0 || c[x][y+2]==0) return false;
	return true;
}

void setdrop(int c[10][10],int d,int x,int y)
{
	c[x][y]--;	c[x][y-1]--;	c[x][y+1]--;	c[x-1][y]--;	c[x+1][y]--;
	if(d == 2) return;
	c[x-1][y-1]--;	c[x+1][y-1]--;	c[x-1][y+1]--;	c[x+1][y+1]--;
	if(d == 1) return;
	c[x-2][y]--;	c[x+2][y]--;	c[x][y-2]--;	c[x][y+2]--;
}

void resetdrop(int c[10][10],int d,int x,int y)
{
	c[x][y]++;	c[x][y-1]++;	c[x][y+1]++;	c[x-1][y]++;	c[x+1][y]++;
	if(d == 2) return;
	c[x-1][y-1]++;	c[x+1][y-1]++;	c[x-1][y+1]++;	c[x+1][y+1]++;
	if(d == 1) return;
	c[x-2][y]++;	c[x+2][y]++;	c[x][y-2]++;	c[x][y+2]++;
}

int seek(int c[10][10],int p,int d,int drops[3])
{
	int x,y;
	
	while(d < 3 && drops[d]==0) d++;
	if(d == 3) return true;
	
	switch(d)
	{
	case 0:
		while(p<36)
		{
			x = 2 + (p%6);
			y = 2 + (p/6);
			if(testdrop(c,0,x,y))
			{
				setdrop(c,0,x,y);
				drops[0]--;
				if(drops[0] == 0)
				{
					if(seek(c,0,1,drops))
					{
						printf("%d %d %d\n",x,y,3);
						return true;
					}
					else
					{
						drops[0]++;
						resetdrop(c,0,x,y);
					}
				}
				else
				{
					if(seek(c,p,0,drops))
					{
						printf("%d %d %d\n",x,y,3);
						return true;
					}
					else
					{
						drops[0]++;
						resetdrop(c,0,x,y);
					}
				}
			}
			p++;
		}
		break;
	case 1:
	case 2:
		while(p<64)
		{
			x = 1 + (p%8);
			y = 1 + (p/8);
			if(testdrop(c,d,x,y))
			{
				setdrop(c,d,x,y);
				drops[d]--;
				if(drops[d]==0)
				{
					if(d == 2)
					{
						printf("%d %d %d\n",x,y,1);
						return true;
					}
					else
					{
						if(seek(c,0,2,drops))
						{
							printf("%d %d %d\n",x,y,3-d);
							return true;
						}
						else
						{
							drops[d]++;
							resetdrop(c,d,x,y);
						}
					}
				}
				else
				{
					if(seek(c,p,d,drops))
					{
						printf("%d %d %d\n",x,y,3-d);
						return true;
					}
					else
					{
						drops[d]++;
						resetdrop(c,d,x,y);
					}
				}
			}
			p++;
		}
		break;
	}
	return false;
}

int main(void)
{
	int dropnum, cloth[10][10];
	
	//int idrops[3];
	//scanf("%d %d %d",&idrops[0],&idrops[1],&idrops[2]);
	scanf("%d",&dropnum);
	//dropnum = idrops[0] + idrops[1] + idrops[2];
	
	/*for(int i=0; i<100; i++)
	{
		cloth[i%10][i/10] = 0;
	}
	
	std::mt19937 engine((unsigned)time(NULL));
	std::uniform_int_distribution<int> dist0(2,7),dist12(1,8);
	
	for(int i=0; i<3; i++)
	{
		for(int j=0; j<idrops[i]; j++)
		{
			int x,y;
			switch(i)
			{
			case 0:
				x = dist0(engine);
				y = dist0(engine);
				resetdrop(cloth,0,x,y);
				printf("%d %d %d\n",x,y,3);
				break;
			case 1:
			case 2:
				x = dist12(engine);
				y = dist12(engine);
				resetdrop(cloth,i,x,y);
				printf("%d %d %d\n",x,y,3-i);
				break;
			}
		}
	}
	
	printf("****************************\n");*/
	
	int stainsum = 0;
	
	for(int i=0; i<100; i++)
	{
		scanf("%d",&(cloth[i%10][i/10]));
		stainsum += cloth[i%10][i/10];
	}
	
	/*for(int iy=0; iy<10; iy++)
	{
		for(int ix=0; ix<10; ix++)
		{
			printf("%02d ",cloth[ix][iy]);
		}
		printf("\n");
	}*/
	
	int drops[3] = {dropnum, 0, 0};
	if(countstain(drops) != stainsum)
	{
		if(!findnextcombi(stainsum,drops)) return 0;
	}
	do
	{
		int cdrops[3] = {drops[0], drops[1], drops[2]};
		seek(cloth,0,0,cdrops);
	}
	while(findnextcombi(stainsum,drops));
	
	/*for(int iy=0; iy<10; iy++)
	{
		for(int ix=0; ix<10; ix++)
		{
			printf("%02d ",cloth[ix][iy]);
		}
		printf("\n");
	}*/
	
	return 0;
}