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