// #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #define _USE_MATH_DEFINES #include using namespace std; using lg = long long; using pii = pair; using pll = pair; #define TEST cerr << "TEST" << endl #define IINF 2147483647 #define LLINF 9223372036854775807LL #define AMARI 998244353 // #define AMARI 1000000007 #define TEMOTO ((sizeof(long double) == 16) ? false : true) #define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000) #define el '\n' #define El '\n' //抽象再帰セグ木?(抽象セグ木の定義分からんな)(遅延ではない) //ococo_segtree rsq(n,0);みたいに宣言する template class ococo_segtree { T func(T a, T b) { //ここに演算を入れる return (a + b); } public: int n; T tmax; //range minimum query vector rmq; //(データの大きさ,単位元) ococo_segtree(int N = 0, T t = IINF) { syokica(N, t); } //配列の初期化 O(N) void syokica(int a, T t) { n = 1; tmax = t; while (n < a)n *= 2; rmq.resize(2 * n - 1); for (int i = 0; i < 2 * n - 1; i++) { rmq[i] = tmax; } } //a[i]をxにする O(logN) void update_num(int i, T x) { i += (n - 1); rmq[i] = x; while (i) { i = (i - 1) / 2; rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]); } } //a[i]にxを加える O(logN) void add_num(int i, T x) { i += (n - 1); rmq[i] = func(rmq[i], x); while (i) { i = (i - 1) / 2; rmq[i] = func(rmq[i * 2 + 1], rmq[i * 2 + 2]); } } T get_minimum2(int a, int b, int k, int l, int r) { if (a <= l && r <= b)return rmq[k]; else if (r <= a || b <= l)return tmax; else return func(get_minimum2(a, b, k * 2 + 1, l, (l + r) / 2), get_minimum2(a, b, k * 2 + 2, (l + r) / 2, r)); } //[l,r)の区間演算の取得 O(logN) T get_val(int l, int r) { return get_minimum2(l, r, 0, 0, n); } }; #define MULTI_TEST_CASE false void solve(void) { int n; lg m; cin >> n >> m; vector p(n); for(int i = 0; i < n; i++)cin >> p[i]; lg tentousuu = 0; ococo_segtree os(n + 2,0); for(int i = 0; i < n; i++){ tentousuu += os.get_val(p[i],n + 2); os.add_num(p[i],1); } if(m % 2 == 0 && tentousuu % 2 == 1){ cout << -1 << el; return; } if(tentousuu % m == 0){ cout << tentousuu << el; return; } lg ans = tentousuu; ans += (m - (ans % m)); if(ans % 2 == tentousuu % 2){ cout << ans << el; return; } ans += m; if(ans % 2 == tentousuu % 2){ cout << ans << el; return; } cout << -1 << el; return; } void calc(void) { return; } int main(void) { cin.tie(nullptr); ios::sync_with_stdio(false); calc(); int t = 1; if (MULTI_TEST_CASE) cin >> t; while (t--) { solve(); } return 0; }