#include #include using namespace std; using ll = long long; #define REP(i,n) for(int i=0;i void chmin(T& a, T b) { a = min(a, b); } template void chmax(T& a, T b) { a = max(a, b); } #define PR(x) cerr << #x << "=" << x << endl using i128 = __int128_t; template struct BIT { int n; // 配列の要素数(数列の要素数+1) vector bit; // データの格納先(1-index) BIT(int n_) : n(n_ + 1), bit(n, 0) {} void add(int idx, T x) { for (int i = idx; i < n; i += (i & -i)) { bit[i] += x; } } T sum(int idx) { T s(0); for (int i = idx; i > 0; i -= (i & -i)) { s += bit[i]; } return s; } }; int main() { ll n, m; cin >> n >> m; vector p(n); REP(i, n) cin >> p[i]; BIT bit(n); ll cnt = 0; REP(i, n) { bit.add(p[i], 1); cnt += bit.sum(n) - bit.sum(p[i]); } if(cnt == 0) { cout << 0; return 0; } ll nm = (cnt + m - 1) / m * m; if(nm % 2 == cnt % 2) { cout << nm; return 0; } nm += m; if(nm % 2 == cnt % 2) { cout << nm; } else { cout << -1; } return 0; }