#include using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define vv(type, c, m, n, i) vector> c(m, vector(n, i)) #define rep(i,a,n) for(int i=(a), i##_len=(n); ii##_len; --i) #define each(x,y) for(auto &(x):(y)) #define var(type, ...)type __VA_ARGS__;Scan(__VA_ARGS__); template void Scan(T& t) { std::cin >> t; } templatevoid Scan(First& first, Rest&...rest) { cin >> first; Scan(rest...); } #define vec(type, c, n) vector c(n);for(auto& i:c) cin>>i; #define len(x) ((int)(x).size()) #define mp make_pair #define eb emplace_back #define em emplace #define pb pop_back #define fi first #define se second #define get(a, i) get(a) typedef long long ll; typedef long double ld; typedef vector vi; typedef vector vd; typedef vector vll; typedef vector vs; typedef vector vb; typedef pair pii; #define mini min_element #define maxi max_element #define sum(a, b) accumulate(a, b) #define chmax(a,b) if(ab)a=b #define ce(x, y) (x + y - 1) / y template inline void print(T t){cout << t << "\n";} template inline void print(H h, T... t){cout << h << " ";print(t...);} int main() { var(int, n, k); vec(int, a, n); vi v(all(a)); sort(all(v)); int ans = 0; rep(i, 0, n - k + 1) { int m = n; while (m > 0) { for (int j = i ; j < m - k; j += k) { if (a[j] > a[j + k]) { swap(a[j], a[j + k]); ++ans; } } m -= k; } } print(v == a ? ans : -1); return 0; }