import java.util.*; import java.io.*; public class Main { private static int[] dy = {0,1,0,-1}; private static int[] dx = {1,0,-1,0}; private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { int[] n_k = readIntArray(); int n = n_k[0]; int k = n_k[1]; int[] aArr = readIntArray(); int count = 0; if(k < n){ for(int i = 0 ; i < n ; i++){ for(int j = i + k ; j < n ; j = j + k){ if(aArr[i] > aArr[j]){ int tmp = aArr[i]; aArr[i] = aArr[j]; aArr[j] = tmp; count++; } } } } for(int i = 0 ; i < n - 1 ; i++){ if(aArr[i] > aArr[i+1]){ System.out.println(-1); return; } } System.out.println(count); } private static int[] readIntArray() throws Exception{ int[] intArray = Arrays.stream(br.readLine().split(" ")) .mapToInt(Integer::parseInt).toArray(); return intArray; } }