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]; HashMap map = new HashMap<>(); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); map.put(arr[i], i); } int[] sorted = (int[])(arr.clone()); Arrays.sort(sorted); int count = 0; for (int i = 0; i < n - 1; i++) { int idx = map.get(sorted[i]); if (i == idx) { continue; } if ((idx - i) % k != 0) { System.out.println(-1); return; } while (i < idx) { int tmp = arr[idx - k]; arr[idx - k] = arr[idx]; arr[idx] = tmp; map.put(sorted[i], idx - k); map.put(arr[idx], idx); idx -= k; count++; } } System.out.println(count); } }