#coding: utf-8 #yuki_366 n,k=map(int,raw_input().split()) a=map(int,raw_input().split()) b=a[:] b.sort() res=0 for i in range(k): for j in range(i, n, k): for l in range(i, j, k): if a[l] > a[j]: a[l], a[j] = a[j], a[l] res += 1 if a==b: print res else: print -1