#include void add_BIT(int N, int BIT[], int i, int k) { while (i <= N) { BIT[i] += k; i += (i & -i); } } long long sum_BIT(int BIT[], int r) { long long sum = 0; while (r > 0) { sum += BIT[r]; r -= (r & -r); } return sum; } long long inversion_number(int N, int A[], int B[]) { int i, a[200001], BIT[200001] = {}; long long inv = 0; for (i = 1; i <= N; i++) a[A[i]] = i; for (i = N; i >= 1; i--) { inv += sum_BIT(BIT, a[B[i]]); add_BIT(N, BIT, a[B[i]], 1); } return inv; } int main() { int i, N, P[200001]; long long M; scanf("%d %lld", &N, &M); for (i = 1; i <= N; i++) scanf("%d", &(P[i])); int I[200001]; for (i = 1; i <= N; i++) I[i] = i; long long inv_num = inversion_number(N, P, I), ans; if (M % 2 == 0 && inv_num % 2 != 0) printf("-1\n"); else { ans = (inv_num + M - 1) / M * M; if (ans % 2 != inv_num % 2) ans += M; printf("%lld\n", ans); } fflush(stdout); return 0; }