// Exhaust the 29^4 ordered keys and ANY restart locations through maxb=12. Four phase bits / budget.
#include <cstdint>
#include <iostream>
#include <array>
#include <map>
#include <vector>
using namespace std;using U=uint64_t;static constexpr int B=12;
static constexpr U ALL=(U(1)<<(4*(B+1)))-1, ONE=0x1111111111111111ULL&ALL, SEVEN=7*ONE;
U rot(U v){return ((v&SEVEN)<<1)|((v>>3)&ONE);}
int main(int argc,char**argv){int q=argc>1?atoi(argv[1]):29;vector<int>x;int c;while(cin>>c){if(c<0||c>=q)return 2;x.push_back(c);}
 using ID=array<int,3>;map<ID,int> count;map<ID,vector<int>> examples; 
 for(int e=0;e<q*q*q*q;e++){
 int k[4],v=e,non=0;U ban[29]{};for(int s=0;s<4;s++){k[s]=v%q;v/=q;ban[k[s]]|=(ONE<<s);non+=!!k[s];}
 for(int mode=0;mode<2;mode++){
    U st=ONE;bool first=true;for(int c:x){
        U before=st;if(!first){U reset=((st|(st>>1)|(st>>2)|(st>>3))&ONE)<<4;if(mode)reset*=15;before|=reset&ALL;}first=false;
        st=rot(before&~ban[c]) | (c==0?before:0);
        if(!st)break;
    }
    int n=0;while(n<=B&&!(st&(U(15)<<(4*n))))n++;
    ID id={mode,non,n};count[id]++;if(examples[id].size()<24)examples[id].push_back(e);
 }}
 for(auto a:count){auto id=a.first;cout<<id[0]<<' '<<id[1]<<' '<<id[2]<<' '<<a.second;for(int e:examples[id])cout<<' '<<e;cout<<'\n';}
}
