N, K = map(int, input().split()) a = list(map(int, input().split())) b = list(a) b.sort() ans = 0 for k in range(K): for j in range(k, N, K): for i in range(k, j, K): if a[i] > a[j]: a[i], a[j] = a[j], a[i] ans += 1 if a == b: print(ans) else: print(-1)