fun main() { val (n, m) = readLongs() val p = readInts().map { it - 1 } val inv = inversionNumber(p) if (m % 2 == 0L && inv % 2 != 0L) { println(-1) } else { val s = (inv + m - 1) / m * m val k = s - inv if (k % 2 == 0L) { println(s) } else { val t = s + m val k2 = t - inv if (k2 % 2 == 0L) { println(t) } else { throw Exception() } } } } fun readLongs() = readln().split(" ").map { it.toLong() } fun readInts() = readln().split(" ").map { it.toInt() } class BIT(private val n: Int) { private val data = LongArray(n) { 0 } fun add(i: Int, x: Long) { var p = i + 1 while (p <= n) { data[p - 1] += x p += p and -p } } fun sum(range: IntRange): Long { val l = range.first val r = range.last + 1 return sum(r) - sum(l) } private fun sum(i: Int): Long { var r = i var s = 0L while(r > 0) { s += data[r - 1] r -= r and -r } return s } } fun inversionNumber(a: List): Long { val n = a.size val bit = BIT(n) var ans = 0L for (i in 0 until n) { ans += i - bit.sum(0..a[i]) bit.add(a[i], 1) } return ans }