n,k = [ int(v) for v in input().split() ] number_list = [ int(v) for v in input().split() ] number_list_sorted = sorted(number_list) difference_list = [ abs(number_list_sorted.index(number_list[i])-i) for i in range(n) ] difference_list = [ i % k == 0 for i in difference_list ] if all(difference_list) == False: print(-1) else: c = 0 for i in range(n): target = number_list_sorted.index(number_list[i]) temp = number_list[target] for j in range(target,i,-k): number_list[j] = number_list[j-k] c += 1 number_list[i] = temp print(c)