#include #include #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector rawVec(n); for (auto& i : rawVec) { cin >> i; } if ( is_sorted(rawVec.begin(), rawVec.end())) { cout << "0" << endl; return 0; } else { if (n <= k) { cout << "-1" << endl; return 0; } else { vector sortedVec = rawVec; sort(sortedVec.begin(), sortedVec.end()); for (size_t i = 0; i < sortedVec.size(); ++i) { auto itr = find(rawVec.begin(), rawVec.end(), sortedVec.at(i)); size_t rawDist = distance(rawVec.begin(), itr); if (((int)rawDist - (int)i) % k != 0) { cout << "-1" << endl; return 0; } } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j + k < n; ++j) { if (rawVec.at(j) > rawVec.at(j + k)) { swap(rawVec.at(j), rawVec.at(j + k)); ans++; } } } cout << ans << endl; return 0; } } cout << "nandeyanen" << endl; return 0; }