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