#include #include #include #define Add(x, y) (x + y >= mod) ? (x + y - mod) : (x + y) #define lowbit(x) x & (-x) #define pi pair #define pii pair> #define iip pair, ll> #define ppii pair, pair> #define ls(k) k << 1 #define rs(k) k << 1 | 1 #define fi first #define se second #define full(l, r, x) for(auto it = l; it != r; ++it) (*it) = x #define Full(a) memset(a, 0, sizeof(a)) #define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout); #define For(i, l, r) for(register int i = l; i <= r; ++i) #define _For(i, l, r) for(register int i = r; i >= l; --i) using namespace std; using namespace __gnu_pbds; typedef double db; typedef unsigned long long ull; typedef long long ll; bool Begin; const int N = 1e3 + 10; inline ll read(){ ll x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9'){ if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9'){ x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x * f; } inline void write(ll x){ if(x < 0){ putchar('-'); x = -x; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); } int n, k, ans; int a[N]; bool End; int main(){ n = read(), k = read(); for(int i = 1; i <= n; ++i) a[i] = read(); for(int i = 1; i <= n; ++i){ for(int j = 1; j + k <= n; ++j){ if(a[j] > a[j + k]){ swap(a[j], a[j + k]); ++ans; } } } for(int i = 1; i <= n; ++i){ if(a[i] < a[i - 1]){ puts("-1"); exit(0); } } write(ans); cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB"; return 0; }