#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; struct before_main{before_main(){cin.tie(0); ios::sync_with_stdio(false);}} before_main; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) template inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); } template inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); } typedef long long ll; int const inf = 1<<29; int main() { int N, K; cin >> N >> K; vector v(N); rep(i, N) { cin >> v[i]; } queue>> q; q.emplace(0, v); auto u = v; sort(all(u)); set> st = {v}; while(!q.empty()) { int cost; vector p; tie(cost, p) = q.front(); q.pop(); if(u == p) { cout << cost << endl; return 0; } rep(i, N-K) { if(p[i] < p[i+K]) { continue; } swap(p[i], p[i+K]); if(st.count(p)) { continue; } q.emplace(cost+1, p); swap(p[i], p[i+K]); } } cout << -1 << endl; return 0; }