#include using namespace std; using ll = long long; class SegTree { private: void update(int idx, int lo, int hi, int pos, int val) { if (hi < pos || pos < lo) return; if (lo == hi) { tree[idx] = val; return; } int mid = (lo + hi) >> 1; update(idx << 1, lo, mid, pos, val); update(idx << 1 | 1, mid + 1, hi, pos, val); tree[idx] = tree[idx << 1] + tree[idx << 1 | 1]; } int query(int idx, int lo, int hi, int s, int e) { if (hi < s || e < lo) return 0; if (s <= lo && hi <= e) return tree[idx]; int mid = (lo + hi) >> 1; return query(idx << 1, lo, mid, s, e) + query(idx << 1 | 1, mid + 1, hi, s, e); } public: SegTree(int n): n(n), tree(4 * n) { } void update(int pos, int val) { update(1, 1, n, pos, val); } int query(int s, int e) { return query(1, 1, n, s, e); } int n; vector tree; }; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; ll m; cin >> n >> m; vector p(n); for (int i = 0; i < n; i++) { cin >> p[i]; } ll s = 0; SegTree seg(n); for (int i = 0; i < n; i++) { seg.update(p[i], 1); s += seg.query(p[i] + 1, n); } if (s % 2 == 1 && m % 2 == 0) { cout << -1 << '\n'; } else { if (s % m == 0) cout << s << '\n'; else cout << (s / m + 1) * m << '\n'; } return 0; }