N,K=map(int,raw_input().split()) a = map(int,raw_input().split()) for o,(p,v) in enumerate(sorted(enumerate(a),key=lambda x:x[1])): a[p] = o i = ans = 0 while i < N: if a[i] == i: i+=1 continue if i-a[i] % K != 0: ans = -1 break j=i while a[j] != j: a[j],a[j+K] = a[j+K],a[j] j += K ans += 1 print ans