// Independent verifier of the strict literal-F clock. Input: number of symbols, period, runes.
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
using namespace std;
struct Engine{
 int p,pm,alpha; vector<int>x,order,k; array<int,29> reject{}; struct S {int lo,hi;};vector<S>windows;
 vector<vector<array<int,29>>> forbidden;
 vector<long long> nodes,scans,full;vector<vector<int>>answers;
 Engine(vector<int> a,int m,int q):p(m),pm((1<<p)-1),alpha(q),x(move(a)),k(p,-1),nodes(p+1),scans(p+1),full(p+1){
   // Order of slots maximizes their circular spread: e.g. p8 0,4,2,6,1,3,5,7.
   order.push_back(0);
   while((int)order.size()<p){int best=-1,bj=-1;for(int j=0;j<p;j++){
      int v=p;for(int t:order)v=min(v,min(abs(t-j),p-abs(t-j)));
      if(v>best){best=v;bj=j;}
   }order.push_back(bj);}
   // An unbroken non-F substring is illegal if every possible starting phase
   // is ruled out. These local certificates frequently eliminate a branch
   // before the logically independent complete glyph-by-glyph search.
   vector<S> strips;for(int i=0;i<(int)x.size();){if(x[i]==0){++i;continue;}int a=i;
      while(i<(int)x.size()&&x[i]!=0)++i;strips.push_back({a,i});}
   stable_sort(strips.begin(),strips.end(),[](S a,S b){return a.hi-a.lo>b.hi-b.lo;});
   if(strips.size()>32)strips.resize(32);
   for(S s:strips){vector<array<int,29>> f(p,array<int,29>{});
      for(int i=s.lo;i<s.hi;i++)for(int st=0;st<p;st++)f[(st+i-s.lo)%p][x[i]]|=1<<st;
      forbidden.push_back(move(f));
      // Also look at a real 480-rune window centered around that long stretch to kill phase-inconsistent branches.
      int lo=max(0,(s.lo+s.hi-480)/2),hi=min((int)x.size(),lo+480);windows.push_back({lo,hi});
   }
 }
 inline int walk(int b,int e)const{int state=pm;for(int i=b;i<e&&state;i++){
     int a=state&~reject[x[i]];int v=((a<<1)&pm)|(a>>(p-1));state=x[i]?v:(state|v);
   }return state;}
 void dfs(int lev,const array<unsigned short,32>&open){
   int j=order[lev];
   // A free starting phase entails rotation invariance: every possible key is represented
   // by a rotation with the greatest numeric shift in slot 0. Include all ties and deduplicate at the leaves.
   for(int v=0;v<=(lev?k[0]:alpha-1);v++){
     nodes[lev+1]++;k[j]=v;array<unsigned short,32> next;bool yes=true;
     for(unsigned w=0;w<forbidden.size();w++){next[w]=open[w]&~forbidden[w][j][v];if(!next[w]){yes=false;break;}}
     if(!yes)continue;reject[v]|=1<<j;scans[lev+1]++;
     for(int h=0;h<min((int)windows.size(),5)&&yes;++h)yes=walk(windows[h].lo,windows[h].hi)!=0;
     if(yes){full[lev+1]++;yes=walk(0,x.size())!=0;}
     if(yes){if(lev+1==p){vector<int> t=k,r=k;for(int i=1;i<p;i++){rotate(t.begin(),t.begin()+1,t.end());r=min(r,t);}if(find(answers.begin(),answers.end(),r)==answers.end())answers.push_back(r);}
          else dfs(lev+1,next);}
     reject[v]&=~(1<<j);
   }
   k[j]=-1;
 }
 void solve(){array<unsigned short,32> open;open.fill(pm);dfs(0,open);}
};
int main(int argc,char**argv){ios::sync_with_stdio(false);int n,p;cin>>n>>p;vector<int>x(n);for(int&i:x)cin>>i;Engine e(x,p,argc>1?stoi(argv[1]):29);e.solve();
 for(auto&v:{e.nodes,e.scans,e.full}){for(auto c:v)cout<<c<<" ";cout<<"\n";}
 for(auto&v:e.answers){cout<<"K";for(int c:v)cout<<" "<<c;cout<<"\n";}
}
