class BIT: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): # 配列0番目からiまでの総和を出力 # i: 1-indexed s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): # i番目にxを可算 while i <= self.size: self.tree[i] += x i += i & -i def update(self, i, x): # i番目を加算処理でxに更新する self.add(i, -self.sum(i) + self.sum(i - 1)) self.add(i, x) def debug(self): # 現在の配列状況を出力する res = [] for i in range(self.size): res.append(self.sum(i + 1) - self.sum(i)) return res def inversion_number(A): b = BIT(N) invert = 0 for i in range(N): invert += b.sum(N) - b.sum(A[i]) b.add(A[i], 1) return invert N, M = map(int, input().split()) P = list(map(int, input().split())) inv = inversion_number(P) moyori = M * -(-inv // M) # print(inv, moyori) if (moyori - inv) % 2 == 0: print(moyori) else: print(-1)