N, K = map(int, input().split()) a = list(map(int, input().split())) b = a.copy() b.sort() if a == b: print(0) exit() cnt = 0 for _ in range(N - K): for i in range(N - K): if a[i] > a[i + K]: a[i], a[i + K] = a[i + K], a[i] cnt += 1 if a == b: print(cnt) exit() print(-1)