def ok(l): for i in range(len(l) - 1): if l[i] > l[i + 1]: return False return True def func(a: list, k): if len(a) == 0: return 0 ans = 0 index = a.index(min(a)) if index % k != 0: print(-1) exit() while index - k >= 0: ans += 1 a[index], a[index - k] = a[index - k], a[index] index -= k ans += func(a[1:], k) return ans n, k = map(int, input().split()) a = list(map(int, input().split())) print(func(a, k))