#include using namespace std; using ll = long long; #define MOD (998244353) class BIT { int n; vector a; public: BIT(int n_) : n(n_) { a.resize(n + 1, 0); } void add(int i, int x) { while (i <= n) { a[i] += x; i += i & (-i); } } int sum(int i) { int ret = 0; while (i > 0) { ret += a[i]; i -= i & (-i); } return ret; } int sum(int l, int r) { return sum(r) - sum(l - 1); } }; ll modpow(ll a, ll x) { ll ret = 1; a %= MOD; while (x > 0) { if (x % 2 == 1) { ret = ret * a % MOD; } a = a * a % MOD; x >>= 1; } return ret; } int main() { int N; ll M; cin >> N >> M; vector P(N); BIT tree(N); ll sum = 0; for (int i = 0; i < N; i++) { cin >> P[i]; sum += (ll)tree.sum(P[i] + 1, N); tree.add(P[i], 1); } if (sum == 0) { cout << 0 << endl; return 0; } if (sum % 2 == 0) { ll ans = ((sum - 1) / M + 1) * M; if (ans % 2 == 1) { ans += M; } cout << ans << endl; } else { if (M % 2 == 0) { cout << -1 << endl; } else { ll ans = ((sum - 1) / M + 1) * M; if (ans % 2 == 0) { ans += M; } cout << ans << endl; } } }