#include using namespace std; #define F0(i,n) for (int i=0; i pii; typedef long long ll; const double EPS = 1e-9; const int MOD = 998244353; #define PR(x) cerr << #x << "=" << x << endl template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template ostream& operator<<(ostream& os, const tuple& p) { os << "(" << get<0>(p) << ", " << get<1>(p) << ", " << get<2>(p) << ")"; return os; } istream& operator>>(istream& is, pii& p) { is>>p.first>>p.second; return is; } template ostream& operator<<(ostream& os, const vector& v) { os << "["; F0(i,SZ(v)) { if (i>0) os << ","; os << v[i]; } os << "]"; return os; } template ostream& operator<<(ostream& os, const set& v) { os << "{"; int f=1; for(auto i:v) { if(f)f=0;else os << ","; cerr << i; } os << "}" << endl; return os; } template ostream& operator<<(ostream& os, const map& v) { os << "{"; int f=1; for(auto i:v) { if(f)f=0;else os << ", "; cerr << i.first << ":" << i.second; } os << "}" << endl; return os; } int i, j, k, n; ll m, ans; const int N = 200005; const int M = 435; int a[N], h[N]; const int DX[]={-1,0,1,0}; const int DY[]={0,1,0,-1}; const string CS="nesw"; const string HS="URDL"; void Add(int& x, int y) { x += y; if (x >= MOD) x -= MOD; } ll Mult(int x, int y) { return 1LL * x * y % MOD; } int get(int i) { int ret = 0; while (i) { ret += h[i]; i &= (i-1); } return ret; } void add(int i) { while (i <= n) { h[i]++; i = (i|(i-1))+1; } } void Solve() { F1(i, n) { scanf("%d", &a[i]); h[i] = 0; } //PR(m); ll inv = 0; for (int i = n; i >= 1; i--) { //PR(a[i]); inv += get(a[i] - 1); add(a[i]); } ans = ((inv + m - 1) / m) * m; if (ans % 2 != inv % 2) { ans += m; if (ans % 2 != inv % 2) ans = -1; } cout << ans << endl; } int main() { //ignore = freopen("x.in", "r", stdin); //freopen("x.out", "w", stdout); //int tn; cin >> tn; //F1(ti, tn) { while (cin >> n >> m) { //PR(n); Solve(); } return 0; }