#include #define _overload(_1,_2,_3,name,...) name #define _rep(i,n) _range(i,0,n) #define _range(i,a,b) for(int i=int(a);i=int(b);--i) #define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__) #define _all(arg) begin(arg),end(arg) #define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg)) #define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary) #define clr(a,b) memset((a),(b),sizeof(a)) #define bit(n) (1LL<<(n)) #define popcount(n) (__builtin_popcountll(n)) templatebool chmax(T &a, const T &b) { return (abool chmin(T &a, const T &b) { return (b> n >> k; rep(i,n) cin >> a[i],sa[i]=a[i]; sort(sa,sa+n); int ans=0; rep(offset,k){ int start=offset; while(1){ if(start>=n) break; int cmax=1<<30,idx=-1; for(int i=start;i=start) swap(a[idx],a[idx-k]),idx-=k,ans++; start+=k; } } bool ok=true; rep(i,n) if(a[i]!=sa[i]) ok=false; if(ok==false) cout << -1 << endl; else cout << ans << endl; return 0; }