#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string>

using namespace std;


string L_cheak(string cloth, int now, bool* ret){
    if(
       cloth[now+(14*0)+0]!='0'&&
       cloth[now+(14*1)-1]!='0'&&cloth[now+(14*1)+0]!='0'&&cloth[now+(14*1)+1]!='0'&&
       cloth[now+(14*2)-2]!='0'&&cloth[now+(14*2)-1]!='0'&&cloth[now+(14*2)+0]!='0'&&cloth[now+(14*2)+1]!='0'&&cloth[now+(14*2)+2]!='0'&&
       cloth[now+(14*3)-1]!='0'&&cloth[now+(14*3)+0]!='0'&&cloth[now+(14*3)+1]!='0'&&
       cloth[now+(14*4)+0]!='0'
       )
    {
        cloth[now+(14*0)+0]--;
        cloth[now+(14*1)-1]--;cloth[now+(14*1)+0]--;cloth[now+(14*1)+1]--;
        cloth[now+(14*2)-2]--;cloth[now+(14*2)-1]--;cloth[now+(14*2)+0]--;cloth[now+(14*2)+1]--;cloth[now+(14*2)+2]--;
        cloth[now+(14*3)-1]--;cloth[now+(14*3)+0]--;cloth[now+(14*3)+1]--;
        cloth[now+(14*4)+0]--;
        *ret = true;
        return cloth;
    }else{
        return cloth;
    }
}


string M_cheak(string cloth, int now, bool *ret){
    if(
       cloth[now+(14*0)-0]!='0'&&cloth[now+(14*0)+1]!='0'&&cloth[now+(14*0)+2]!='0'&&
       cloth[now+(14*1)-0]!='0'&&cloth[now+(14*1)+1]!='0'&&cloth[now+(14*1)+2]!='0'&&
       cloth[now+(14*2)-0]!='0'&&cloth[now+(14*2)+1]!='0'&&cloth[now+(14*2)+2]!='0'
       )
    {
        cloth[now+(14*0)-0]--;cloth[now+(14*0)+1]--;cloth[now+(14*0)+2]--;
        cloth[now+(14*1)-0]--;cloth[now+(14*1)+1]--;cloth[now+(14*1)+2]--;
        cloth[now+(14*2)-0]--;cloth[now+(14*2)+1]--;cloth[now+(14*2)+2]--;
        *ret = true;
        return cloth;
    }else{
        return cloth;
    }
}
string S_cheak(string cloth, int now, bool *ret){
    if(
       cloth[now+(14*0)-0]!='0'&&
       cloth[now+(14*1)-1]!='0'&&cloth[now+(14*1)+0]!='0'&&cloth[now+(14*1)+1]!='0'&&
       cloth[now+(14*2)-0]!='0'
       )
    {
        cloth[now+(14*0)-0]--;
        cloth[now+(14*1)-1]--;cloth[now+(14*1)+0]--;cloth[now+(14*1)+1]--;
        cloth[now+(14*2)-0]--;
        *ret = true;
        return cloth;
    }else{
        return cloth;
    }
}


bool Blur(int l, int m, int s, string cloth, int now){
    if(now>=cloth.length()){
        for(int i=0; i<cloth.length(); i++){
            if(cloth[i]!='0')return false;
        }
        return true;
    }
    
    if(cloth[now]!='0'){
        string temp=cloth;
        bool ret1=false, ret2=false, ret3=false;
        temp=L_cheak(temp, now, &ret1);
        if(l>0&&ret1){
            ret1=Blur(l-1,m,s,temp,now);
            if(ret1){
                
                cout<<now%14-2<<" "<<now/14+2<<" "<<3<<endl;
                return true;
            }
            
        }
        temp=cloth;
        temp=M_cheak(temp, now,&ret2);
        if(m>0&&ret2){
            ret2=Blur(l,m-1,s,temp,now);
            if(ret2){
                cout<<now%14-1<<" "<<now/14+1<<" "<<2<<endl;
                return true;
            }
            
        }
        temp=cloth;
        temp=S_cheak(temp, now, &ret3);
        if(s>0&&ret3){
            ret3=Blur(l,m,s-1,temp,now);
            if(ret3){
                cout<<now%14-2<<" "<<now/14+1<<" "<<1<<endl;
                return true;
            }
            
        }
        return false;
        
    }else{
        return Blur(l,m,s,cloth,now+1);
    }
    
    
}

int main(){
    string temp;
    int in;
    string cloth;
    int n;
    while(cin>>n){
        for(int i=0; i<10; i++){
            cloth+="00";
            for(int j=0; j<10; j++){
                cin>>in;
                cloth+=in+'0';
            }
            cloth+="00";
        }
        cloth+="00000000000000";
        cloth+="00000000000000";
        cloth+="00000000000000";
        cloth+="00000000000000";
        cloth+="00000000000000";
        
        int sum=0;
        for(int i=0; i<cloth.length(); i++){
            sum+=cloth[i]-'0';
        }
        for(int i=0; i<=n; i++){
            for(int j=0; j<=n; j++){
                for(int k=0; k<=n; k++){
                    if(13*i+9*j+5*k==sum&&i+j+k==n){
                        int l,m,s;
                        l=i;
                        m=j;
                        s=k;
                        temp=cloth;
                        Blur(l, m, s, temp, 0);
                    }
                }
                
            }
        }
    }
}

Measured memory_bytes: 1332