import java.util.*; public class Exercises22{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); int size = sc.nextInt(); int armWidth = sc.nextInt(); int[] array = new int[size]; for (int n = 0; n < size; n++){ array[n] = sc.nextInt(); } int count = 0; boolean flag = false; for (int a = 0; a < armWidth; a++){ for (int k = a; k < getLastIndex(a, size, armWidth); k += armWidth){ for(int i = getLastIndex(a, size, armWidth); k < i; i -= armWidth){ if (array[i] < array[i - armWidth]){ int w = array[i]; array[i] = array[i - armWidth]; array[i - armWidth] = w; count++; } } } } for (int n = 1; n < size; n++){ if (array[n - 1] <= array[n]){ flag = true; }else{ flag = false; break; } } if(flag == true){ System.out.println(count); }else{ System.out.println(-1); } } private static int getLastIndex(int a, int size, int armWidth){ int extr = 0; if (size % armWidth > a){ extr = 1; } return (size / armWidth - 1 + extr) * armWidth + a; } }