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++){ n[i] = Integer.parseInt(sn[i]); } int[] val = new int[n[0]]; for(int i = 0; i < n[0]; i++){ val[i] = Integer.parseInt(sn2[i]); } int Output = 0; int MVal = n[0] - n[1] - 1; for(int i = 0; i <= MVal; i++){ if(i==0 && i >= MVal){ break; } if(val[i] > val[i + n[1]]){//ソート int memory = val[i]; val[i] = val[i + n[1]]; val[i + n[1]] = memory; Output++; } } if(Output != 0 && n[0] >= n[1]){//一番最初を再比較 if(val[0] > val[n[1]]){ int memory = val[0]; val[0] = val[n[1]]; val[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); } }