#pragma region competitive_programming #ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #pragma GCC optimize("Ofast") #include template struct Binary_Indexed_Tree { public: Binary_Indexed_Tree() :_n(0) {} Binary_Indexed_Tree(int n) : _n(n) { for (int i = 0; i < 2; i++)data[i].assign(n, 0); } void add(int l, int r, T x) { // [l, r)にxを加算 assert(0 <= l && l < r && r <= _n); add_sub(0, l, -x * (l - 1)); add_sub(0, r, x * (r - 1)); add_sub(1, l, x); add_sub(1, r, -x); } void add(int p, T x) { // pにxを加算 assert(0 <= p && p < _n); add(p, p + 1, x); } T sum(int r) { // [0, r) return sum_sub(0, r) + sum_sub(1, r) * (r - 1); } T sum(int l, int r) { // [l,r) assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } T operator[](int r) { return sum(r + 1) - sum(r); } int lower_bound(T w) { // \sum_{i=0}^{x}a_x >= wとなるような最小のx なければn, ただしa_xは全て非負であることが条件 if (w <= 0)return 0; int x = 0, r = 1; while (r < _n)r = r << 1; for (int len = r; len > 0; len = len >> 1) { if (x + len < _n && sum(x, x + len) < w) { w -= sum(x, x + len); x += len; } } if (w > 0)x++; return x; } private: int _n; std::vector data[2]; void add_sub(int i, int p, T x) { // 0-indexed assert(0 <= p && p <= _n); p++; // 内部では1-indexed while (p <= _n) { data[i][p - 1] += x; p += p & -p; } } T sum_sub(int i, int p) { T s = 0; for (int idx = p; idx > 0; idx -= (idx & -idx)) { s += data[i][idx - 1]; } return s; } }; namespace tomo0608 { typedef long long ll; typedef long double ld; template using V = std::vector; template using VV = V>; template using VVV = V>; typedef std::pair pii; typedef std::pair pll; templatevoid input(T&... a) { (std::cin >> ... >> a); }; #define INT(...) int __VA_ARGS__; IN(__VA_ARGS__) #define LL(...) long long __VA_ARGS__; IN(__VA_ARGS__) #define STR(...) string __VA_ARGS__; IN(__VA_ARGS__) #define DBL(...) double __VA_ARGS__; IN(__VA_ARGS__) #define VEC(type, name, size) std::vector name(size);IN(name) #define VECVEC(type, name, h, w) std::vector> name(h, std::vector(w));IN(name) template std::istream& operator>>(std::istream& is, std::pair& p) { is >> p.first >> p.second; return is; } template std::ostream& operator<<(std::ostream& os, const std::pair& p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template std::istream& operator>>(std::istream& is, std::vector& v) { for (auto& e : v) is >> e; return is; } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { for (auto& e : v) os << e << ' '; return os; } template std::ostream& operator << (std::ostream& os, std::set& set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr;++itr;if (itr != set_var.end()) os << ", ";itr--; }os << "}";return os; } template std::ostream& operator<<(std::ostream& os, std::map& map_var) { os << "{";for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--; }os << "}";return os; } void IN() {} template void IN(Head& head, Tail &...tail) { std::cin >> head; IN(tail...); } void print() { std::cout << '\n'; } templatevoid print(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n'; } void drop() { std::cout << '\n';exit(0); } templatevoid drop(const T& a, const Ts&... b) { std::cout << a; (std::cout << ... << (std::cout << ' ', b)); std::cout << '\n';exit(0); } #ifdef __LOCAL void debug_out() { std::cerr << std::endl; } template < class Head, class... Tail> void debug_out(Head H, Tail... T) { std::cerr << ' ' << H; debug_out(T...); } #define debug(...) std::cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) std::cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << std::endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif #define rep1(a) for(long long i = 0; i < a; i++) #define rep2(i, a) for(long long i = 0; i < a; i++) #define rep3(i, a, b) for(long long i = a; i < b; i++) #define rep4(i, a, b, c) for(long long i = a; i < b; i += c) #define drep1(a) for(long long i = a-1; i >= 0; i--) #define drep2(i, a) for(long long i = a-1; i >= 0; i--) #define drep3(i, a, b) for(long long i = a-1; i >= b; i--) #define drep4(i, a, b, c) for(long long i = a-1; i >= b; i -= c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define drep(...) overload4(__VA_ARGS__, drep4, drep3, drep2, drep1)(__VA_ARGS__) #define endl '\n' } // namespace tomo0608 namespace tomo0608 { #define ALL(x) x.begin(),x.end() template T SUM(const S& v) { return accumulate(ALL(v), T(0)); } #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define RSORT(v) sort(ALL(v)); reverse(ALL(v)) #define UNIQUE(x) SORT(x), x.erase(unique(ALL(x)), x.end()) #define lb(c, x) distance((c).begin(), lower_bound(ALL(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(ALL(c), (x))) template void zip(std::vector& x) { std::vector y = x;UNIQUE(y);for (int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template using priority_queue_rev = std::priority_queue, std::greater>; template inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return 1; } return 0; } template inline int count_between(std::vector& a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r) #define bittest(n, k) ((n >> k) & 1) int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); } #define perm(v) for(bool permrepflag = true; (permrepflag ? exchange(permrepflag, false) : next_permutation(ALL(v)));) template T ceil(T x, S y) { assert(y); return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y)); } template T floor(T x, S y) { assert(y); return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1))); } } //using namespace atcoder; using namespace std; using namespace tomo0608; int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 }; int dy[8] = { 0, 1, 0, -1, 1, -1, -1, 1 }; // インタラクティブ問題のときは出力するたびにcout.flush();を忘れない!!!!! void solve(); int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); std::cout << std::setprecision(20); int codeforces = 1; //cin >> codeforces; while (codeforces--) { solve(); } return 0; } #pragma endregion void solve() { LL(n, m); VEC(ll, p, n); Binary_Indexed_Tree BIT(n); ll x = 0; rep(i, n) { x += BIT.sum(p[i] - 1, n); BIT.add(p[i] - 1, 1); } if (x == 0) { drop(0); } if (x % 2 == 1) { if (m % 2 == 0)drop(-1); // mの奇数倍でx以上となればよい if (m >= x)drop(m); ll s = (x + m - 1) / m; if (s % 2 == 0)s++; drop(s * m); } else { if (m >= x) { if (m % 2)m *= 2; drop(m); } ll s = (x + m - 1) / m; if (m % 2 == 1 && s % 2 == 1)s++; drop(s * m); } }