import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int count = 0; for (int i = 0; i < k; i++) { for (int j = n; i + k < j; j -= k) { for (int a = i; a + k < j; a += k) { if (arr[a] > arr[a + k]) { int tmp = arr[a]; arr[a] = arr[a + k]; arr[a + k] = tmp; count++; } } } } for (int i = 0; i < n - 1; i++) { if (arr[i] > arr[i + 1]) { System.out.println(-1); return; } } System.out.println(count); } }