package yuki_coder; import java.util.Scanner; public class No_366 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String S1 = sc.nextLine(); String S2 = sc.nextLine(); String[] sn = S1.split(" ", -1); String[] sn2 = S2.split(" ", -1); int[] n = new int[2]; for(int i = 0; i < 2; i++){//int型に変換 n[i] = Integer.parseInt(sn[i]); } long[] val = new long[n[0]]; for(int i = 0; i < n[0]; i++){//int型に変換 val[i] = Integer.parseInt(sn2[i]); } int Output = 0;//出力 int MVal = n[0] - n[1] - 1; flag: for(int i = 0; i <= n[0]; i++){ for(int j = 0; j <= MVal; j++){ if(0 > MVal){ break flag; } if(val[j] > val[j + n[1]]){//ソート long memory = val[j]; val[j] = val[j + n[1]]; val[j + n[1]] = memory; Output++; } } } for(int i = 1; i < n[0]; i++){//数字のソートの確認 if(val[i - 1] > val[i]){ Output = -1; break; } } System.out.println(Output); } }