K=int(input().split()[1]) S=list(map(int,input().split())) C=0 def s(): global S,K c=0 for i in range(len(S)-K): if S[i]>S[i+K]: S[i],S[i+K]=S[i+K],S[i] c+=1 return c while 1: c=s() C+=c if c<1:break print(C if S==sorted(S) else -1)