#include using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int n, k, t; vector a, b; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t; a.push_back(t); } b = a; sort(b.begin(), b.end()); int res = 0; for (int i = 0; i < n; i++) { int p = -1; for (int j = i; j < n; j++) { if (b[i] == a[j]) { p = j; break; } } if ((p - i) % k != 0) { cout << -1 << '\n'; return 0; } res += ((p - i) / k); for (int j = p; j > i; j -= k) { int tt = a[j]; a[j] = a[j - k]; a[j - k] = tt; } } cout << res << '\n'; return 0; }