#define _CRT_SECURE_NO_WARNINGS #include using namespace std; #define len(n) (int)n.length() #define pb push_back #define ll long long #define make_pair mkp #define REP(i, n) for(int i = 0;i < n;i++) #define REPR(i, n) for(int i = n-1;i >= 0;i--) #define ALL(v) (v).begin(), (v).end() #ifdef DBGPRT #define dbg(n) cerr << "//" << #n << ":" << n < LLARR; #define INF 999999999 const double PI = 3.1415926535897932384626433832795; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { ios::sync_with_stdio(false); ll n,k; cin >> n >> k; vector v; REP(i,n) { ll t;cin >> t; v.pb(t); } ll ans = 0; REP(i, n-1) { dbg(i); for(int j = i; j < n; j+=k) { dbg(j); if (v[i] > v[j]) { ll tmp = v[i]; v[i] = v[j]; v[j] = tmp; ans++; } } } dbg(""); for(auto i:v) dbg(i); bool f = true; REP(i, n-1) { if (v[i] > v[i+1]) f = false; } cout << (f ? ans : -1) << endl; return 0; }