import java.util.Arrays; import java.util.Scanner; public class Yuki366 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int N, K; N = sc.nextInt(); K = sc.nextInt(); int[] A = new int[N]; int[] sortA = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); sortA[i] = A[i]; } Arrays.sort(sortA); int ans = Ksort(A, K); if (!Arrays.equals(A, sortA)) { ans = -1; } System.out.println(ans); } public static int Ksort(int[] A, int K) { int cnt = 0; for (int i = 0; i < A.length; i++) { for (int j = 0; j + K < A.length; j++) { if (A[j] > A[j + K]) { int buf = A[j]; A[j] = A[j + K]; A[j + K] = buf; cnt++; } } } return cnt; } }