# -*- coding: utf-8 -*- N,K = map(int, input().split()) a = list(map(int, input().split())) ans = 0 for i in range(0, N-K, 1): for j in range(N-1-K, i-1, -1): if a[j] > a[j+K]: temp = a[j] a[j] = a[j+K] a[j+K] = temp ans += 1 is_sorted = True for i in range(N-1): if a[i] > a[i+1]: is_sorted = False break print(ans) if is_sorted else print(-1)