#!/usr/bin/env python #coding:utf8 def read(): N, K = map(int, raw_input().split()) vList = map(int, raw_input().split()) return K, vList def calcCost(subList): ret = 0 for i in range(len(subList)): for j in range(len(subList) - i - 1): if subList[j] > subList[j + 1]: subList[j], subList[j + 1] = subList[j + 1], subList[j] ret += 1 return ret def work((K, vList)): for i in range(K): if i + K >= len(vList) and i + 1 < len(vList) and vList[i] > vList[i + 1]: print -1 return ans = 0 for i in range(min(K, len(vList) - K)): print vList[i::K] ans += calcCost(vList[i::K]) print ans if __name__ == "__main__": work(read())