#include<bits/stdc++.h>
using namespace std;
int n,a[10][10],b[10][10];
bool p;
struct node{
int x,y,g;
bool operator <(const node &t) const{
if(x!=t.x)
return x<t.x;
if(y!=t.y)
return y<t.y;
return g>t.g;
}
};
vector<node> v1,v2;
inline bool check(){
for(int i=0;i<5;++i)
for(int j=0;j<7;++j)
if(a[i][j])
return 0;
return 1;
}
inline bool ch(int x,int y){
return x>=0&&x<5&&y>=0&&y<7;
}
inline bool work(){
bool p=0;
for(int i=0;i<5;++i)
for(int j=0;j<7;++j){
if(!a[i][j])
continue;
bool x=0;
if(ch(i-2,j)&&a[i][j]==a[i-2][j]&&ch(i-1,j)&&a[i][j]==a[i-1][j]&&ch(i+1,j)&&a[i][j]==a[i+1][j]&&ch(i+2,j)&&a[i][j]==a[i+2][j]){
x=1;
a[i-2][j]=a[i-1][j]=a[i+1][j]=a[i+2][j]=0;
} else if(ch(i-1,j)&&a[i][j]==a[i-1][j]&&ch(i+1,j)&&a[i][j]==a[i+1][j]&&ch(i+2,j)&&a[i][j]==a[i+2][j]){
x=1;
a[i-1][j]=a[i+1][j]=a[i+2][j]=0;
} else if(ch(i-2,j)&&a[i][j]==a[i-2][j]&&ch(i-1,j)&&a[i][j]==a[i-1][j]&&ch(i+1,j)&&a[i][j]==a[i+1][j]){
x=1;
a[i-2][j]=a[i-1][j]=a[i+1][j]=0;
} else if(ch(i+1,j)&&a[i][j]==a[i+1][j]&&ch(i+2,j)&&a[i][j]==a[i+2][j]){
x=1;
a[i+1][j]=a[i+2][j]=0;
} else if(ch(i-1,j)&&a[i][j]==a[i-1][j]&&ch(i+1,j)&&a[i][j]==a[i+1][j]){
x=1;
a[i-1][j]=a[i+1][j]=0;
} else if(ch(i-2,j)&&a[i][j]==a[i-2][j]&&ch(i-1,j)&&a[i][j]==a[i-1][j]){
x=1;
a[i-2][j]=a[i-1][j]=0;
}
if(ch(i,j-2)&&a[i][j]==a[i][j-2]&&ch(i,j-1)&&a[i][j]==a[i][j-1]&&ch(i,j+1)&&a[i][j]==a[i][j+1]&&ch(i,j+2)&&a[i][j]==a[i][j+2]){
x=1;
a[i][j-2]=a[i][j-1]=a[i][j+1]=a[i][j+2]=0;
} else if(ch(i,j-1)&&a[i][j]==a[i][j-1]&&ch(i,j+1)&&a[i][j]==a[i][j+1]&&ch(i,j+2)&&a[i][j]==a[i][j+2]){
x=1;
a[i][j-1]=a[i][j+1]=a[i][j+2]=0;
} else if(ch(i,j-2)&&a[i][j]==a[i][j-2]&&ch(i,j-1)&&a[i][j]==a[i][j-1]&&ch(i,j+1)&&a[i][j]==a[i][j+1]){
x=1;
a[i][j-2]=a[i][j-1]=a[i][j+1]=0;
} else if(ch(i,j+1)&&a[i][j]==a[i][j+1]&&ch(i,j+2)&&a[i][j]==a[i][j+2]){
x=1;
a[i][j+1]=a[i][j+2]=0;
} else if(ch(i,j-1)&&a[i][j]==a[i][j-1]&&ch(i,j+1)&&a[i][j]==a[i][j+1]){
x=1;
a[i][j-1]=a[i][j+1]=0;
} else if(ch(i,j-2)&&a[i][j]==a[i][j-2]&&ch(i,j-1)&&a[i][j]==a[i][j-1]){
x=1;
a[i][j-2]=a[i][j-1]=0;
}
if(x){
p=1;
a[i][j]=0;
}
}
return p;
}
inline void down(){
for(int i=0;i<5;++i)
for(int j=0;j<6;++j){
if(!a[i][j]){
int qwq=-1;
for(int k=j+1;k<7;++k){
if(a[i][k]){
qwq=k;
break;
}
}
if(qwq==-1)
break;
a[i][j]=a[i][qwq];
a[i][qwq]=0;
}
}
return;
}
inline void dfs(int x){
if(x>n)
return;
if(!v1.empty()&&v2>v1)
return;
if(check()){
if(x!=n)
return;
v1=v2;
return;
}
for(int i=0;i<5;++i)
for(int j=0;j<6;++j){
for(int o=0;o<5;++o)
for(int p=0;p<7;++p)
b[o][p]=a[o][p];
swap(a[i][j],a[i+1][j]);
bool qwq=0;
while(work()){
down();
qwq=1;
}
if(qwq){
v2.push_back(node({i,j,1}));
dfs(x+1);
v2.pop_back();
}
for(int o=0;o<5;++o)
for(int p=0;p<7;++p)
a[o][p]=b[o][p];
if(!a[i-1][j]){
swap(a[i][j],a[i+1][j]);
bool qwq=0;
while(work()){
down();
qwq=1;
}
if(qwq){
v2.push_back(node({i,j,-1}));
dfs(x+1);
v2.pop_back();
}
for(int o=0;o<5;++o)
for(int p=0;p<7;++p)
a[o][p]=b[o][p];
}
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<5;++i){
int k,tot=0;
while(~scanf("%d",&k)&&k)
a[i][tot++]=k;
}
dfs(0);
if(v1.empty())
puts("-1");
else
for(int i=0;i<n;++i)
printf("%d %d %d\n",v1[i].x,v1[i].y,v1[i].g);
return 0;
}