import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int K = sc.nextInt(); int[] array = new int[N]; // 重複がないと楽で助かる. int[] sorted = new int[N]; for(int i = 0; i < N; i++){ array[i] = sc.nextInt(); sorted[i] = array[i]; } Arrays.sort(sorted); int answer = 0; CONT: for(int i = 0; i < N; i++){ for(int pos = i; pos < N; pos += K){ if(array[pos] == sorted[i]){ while(i != pos){ answer++; { int tmp = array[pos]; array[pos] = array[pos - K]; array[pos - K] = tmp; } pos -= K; } continue CONT; } } answer = -1; break; } System.out.println(answer); } }