#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
int pw(int a, int b, int mod){
int ret = 1;
while(b){
if(b%2){
ret = ret*a%mod;
}
a = a*a%mod;
b>>=1;
}
return ret;
}
signed main() {
cin>>n>>k;
// cout<<pw(2,0,100);
vector<int> nxt(n),p(n),pos(n);
for(auto &i: p){
cin>>i;
i--;
}
for(int i = 0;i<n;i++){
nxt[i] = p[i];
pos[p[i]] = i;
}
vector<int> ans(n);
vector<bool> used(n,false);
for(int i = 0;i<n;i++){
vector<int> tmp;
int node = i;
while(!used[node]){
used[node] = true;
tmp.push_back(node);
node = nxt[node];
}
if(tmp.size() == 0) continue;
for(auto m: tmp) cout<<m<<" ";
cout<<endl;
int sz = tmp.size();
int shift = pw(2LL,k,sz);
for(int j = 0;j<tmp.size();j++){
int position = pos[tmp[(j+shift)%(int)tmp.size()]];
ans[position] = tmp[j];
}
}
for(int i = 0;i<n;i++){
cout<<i+1<<": "<<ans[i]+1<<" "<<endl;
}
// for(auto i: ans) cout<<i+1<<" ";
// your code goes here
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW50IGxvbmcgbG9uZwppbnQgbixrOwppbnQgcHcoaW50IGEsIGludCBiLCBpbnQgbW9kKXsKCWludCByZXQgPSAxOwoJd2hpbGUoYil7CgkJaWYoYiUyKXsKCQkJcmV0ID0gcmV0KmElbW9kOwoJCX0KCQlhID0gYSphJW1vZDsKCQliPj49MTsKCX0KCXJldHVybiByZXQ7Cn0Kc2lnbmVkIG1haW4oKSB7CgljaW4+Pm4+Pms7CgkvLyBjb3V0PDxwdygyLDAsMTAwKTsKCXZlY3RvcjxpbnQ+IG54dChuKSxwKG4pLHBvcyhuKTsKCWZvcihhdXRvICZpOiBwKXsKCQkgY2luPj5pOwoJCSBpLS07Cgl9CgkKCWZvcihpbnQgaSA9IDA7aTxuO2krKyl7CgkJbnh0W2ldID0gcFtpXTsKC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