class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 1) def update(self, idx, delta): while idx <= self.n: self.tree[idx] += delta idx += idx & -idx def query(self, idx): res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def compute_inversion(p): max_val = len(p) ft = FenwickTree(max_val) inv_count = 0 for x in reversed(p): inv_count += ft.query(x - 1) ft.update(x, 1) return inv_count n, m = map(int, input().split()) p = list(map(int, input().split())) # Check if already sorted is_sorted = all(p[i] == i + 1 for i in range(n)) if is_sorted: print(0) else: s = compute_inversion(p) if m % 2 == 0: if s % 2 != 0: print(-1) else: t_min = (s + m - 1) // m print(t_min * m) else: t_min = (s + m - 1) // m required_parity = s % 2 if (t_min % 2) == required_parity: print(t_min * m) else: print((t_min + 1) * m)