N,K=map(int, input().split()) A=list(map(int, input().split())) SA = sorted(A) cnt = 0 for i in range(N-K): for j in range(0,N-K): if A[j] > A[j+K]: A[j],A[j+K]=A[j+K],A[j] cnt+=1 for i in range(N): if SA[i]!=A[i]: print(-1) exit() print(cnt)