#include<iostream> #include<vector> #include<algorithm> using namespace std; using ll=long long; #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} template <class T> struct FenwickTree{ int n; vector<T> bit; FenwickTree(int N){ bit.resize(N+1); n = N; } T sum(int i){ i++; T s = 0; while (i > 0){ s += bit[i]; i -= i & -i; } return s; } T sum(int l, int r){ if (l >= r) return 0; return sum(r-1) - sum(l-1); } void add(int i, T x){ i++; while (i <= n){ bit[i] += x; i += i & -i; } } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; ll M; cin>>N>>M; vector<int>P(N); FenwickTree<int>fw(N); ll cnt=0; for(int i=0;i<N;i++){ cin>>P[i]; P[i]--; cnt+=fw.sum(P[i],N); fw.add(P[i],1); } if(cnt==0)cout<<0<<"\n"; else if(cnt%2==0){ if(M%2==0)cout<<max(cnt,M)<<"\n"; else cout<<max(cnt,2*M)<<"\n"; }else{ if(M%2==0)cout<<-1<<"\n"; else cout<<max(cnt,M)<<"\n"; } }