import java.util.Arrays; import java.util.Scanner; public class Main { static int cnt = 0; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int K = scan.nextInt(); int []a = new int[N]; int []b = new int[N]; for(int i = 0; i < N; i++) { a[i] = scan.nextInt(); b[i] = a[i]; } scan.close(); Arrays.sort(b); for(int i = 0; i < K; i++) { kStepBubbleSort(a, N - i, K); } for(int i = 0; i < N; i++) { if(a[i] != b[i]) { System.out.println("-1"); System.exit(0); } } System.out.println(cnt); // for(int i : a) { // System.out.print(i + " "); // } // System.out.println(); } static void kStepBubbleSort(int[]A, int n, int K) { boolean flag = true; while(flag) { flag = false; for(int i = n - 1; i - K >= 0; i -= K) { if(A[i] < A[i - K]) { int t = A[i]; A[i] = A[i - K]; A[i - K] = t; flag = true; cnt ++; } } } } }