#include using namespace std; using ll = long long; template struct BIT { vector bit; int N; BIT (int n){ N = n; bit.resize(N); } void add(int loc, T val){ loc++; while(loc <= N){ bit[loc-1] += val; loc += loc & -loc; } } T sum(int l, int r){ T res = _sum(r) - _sum(l-1); return res; } T _sum(int r){ r++; T res = 0; while(r > 0){ res += bit[r-1]; r -= r & -r; } return res; } }; ll inversion_number(vector &A){ ll d=0, mx=0; for (int i=0; i tree(mx+1); for (int i=0; i> N >> M; vector A(N); for (int i=0; i> A[i]; I = inversion_number(A); K = (I+M-1)/M; for (ll i=K; i<=K+10; i++){ if ((M*K-I) % 2 == 0){ cout << M*K << endl; return 0; } } cout << -1 << endl; return 0; }