#include using namespace std; #define REP(i,n) for(ll i=0;i<(ll)n;i++) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using pdd = pair; template using V = vector; template using P = pair; template vector make_vec(size_t n, T a) { return vector(n, a); } template auto make_vec(size_t n, Ts... ts) { return vector(n, make_vec(ts...)); } template ostream& operator << (ostream& os, const pair v){os << "(" << v.first << ", " << v.second << ")"; return os;} template ostream& operator<<(ostream &os, const vector &v) { for (auto &e : v) os << e << ' '; return os; } template ostream& operator<<(ostream& os, const vector> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template void UNIQUE(vector &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());} template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template void debug(vector>&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){if(n!=0)cerr< struct BIT { vector< T > data; BIT(int sz) { data.assign(++sz, 0); } // 閉区間 [0, k] の和を求める // k は(指定したsz)-1 までしか値を取れない T sum(int k) { T ret = 0; for(++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } // 閉区間 [k, l] の和を求める T segment_sum(int k, int l){ T ret = 0; ret += sum(l); ret -= sum(k-1); return (ret); } // 要素 k に x を加える void add(int k, T x) { for(++k; k < (int)data.size(); k += k & -k) data[k] += x; } // 要素 k を x の変更 void update(int k, T x){ T tmp = segment_sum(k, k); add(k, x-tmp); } // 二分探索 [0, k] の和が w 以上になるような最小の k を求める // 値が全て0以上じゃないと無理 // 存在しなければ sz-1 を返す // w<=0 のときは -1 を返す int lower_bound(T w){ if(w<=0) return -1; int sz = data.size(); int x = 0, r = 1; while(r < sz-1) r = (r << 1); for(int len = r; len > 0; len = (len >> 1)){ if(x + len <= sz-1 and data[x + len] < w){ w -= data[x + len]; x += len; } } return x; } // 全ての要素をprint void print(){ cout << "Line" << __LINE__ << ": BIT = "; for(int i=0;i<(int)data.size()-1;i++) cout << segment_sum(i, i) << " "; cout << "\n"; } }; template ll inversion_count(vector &x, bool compress = false){ // x の転倒数を計算 // 座標圧縮が必要な場合,compress = true とする // 同じ値が含まれる場合は使えない,inversion_count_2 を使う int sz = x.size(); if(compress){ vector y = x; UNIQUE(y); for(int i=0;i bit(MA); ll res = 0; for(int i=0;i> N >> M; vector P(N); REP(i, N) cin >> P[i]; auto inv = inversion_count(P); ll res = -1; if(inv == 0){ res = 0; }else{ if(M % 2 == 0){ if(inv % 2 == 0){ res = ((inv + M - 1) / M) * M; } }else{ res = ((inv + M - 1) / M) * M; if((res - inv) % 2 != 0){ res += M; } } } cout << res << endl; return 0; }