#include #include using ll = long long; #define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i) #define rreps(i, n) for (int i = ((int)(n)); i > 0; --i) #define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i) #define repc2(i, s, n) for (int i = (s); i <= (int)(n); ++i) constexpr int inf = 2000'000'000; constexpr ll linf = 4000000000000000000ll; constexpr ll M7 = 1000000007ll; constexpr ll M09 = 1000000009ll; constexpr ll M9 = 998244353ll; #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) using namespace std; using namespace atcoder; template inline ostream& operator<<(ostream& os, vector& v) { for (auto& e : v) os << e << " "; return os; } template std::ostream& operator<<(std::ostream& os, const std::pair& p) noexcept { return os << "(" << p.first << ", " << p.second << ")"; } #ifdef ONLINE_JUDGE #define debug(...) #else #define debug(...) cerr << "<" << #__VA_ARGS__ << ">: ", debug_out(__VA_ARGS__) template void debug_out(T t) { cerr << t << endl; } template void debug_out(T t, Args... args) { cerr << t << ", "; debug_out(args...); } #endif int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, m; cin >> n >> m; vector p(n); rep(i, n) cin >> p[i]; auto inv = [](const vector& v) { fenwick_tree bit(v.size()); using P = pair; vector

v_idx(v.size()); rep(i, v.size()) { v_idx.at(i) = {v.at(i), i}; } sort(rall(v_idx)); ll ans = 0; rep(i, v.size()) { ans += bit.sum(0, v_idx.at(i).second); bit.add(v_idx.at(i).second, 1); } return ans; }; ll invNum = inv(p); debug(invNum); if (m % 2 == 0 && invNum % 2 != 0) { cout << -1 << endl; return 0; } ll c = (invNum + m - 1) / m * m; debug(c); if (c % 2 == invNum % 2) { cout << c << "\n"; } else { cout << c + m << "\n"; } return 0; }