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