package yuki_coder; import java.util.Scanner; public class No_366 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); long[] val = new long[N]; for(int i = 0; i < N; i++){ val[i] = sc.nextLong();//それぞれの値を受け取る } int Output = 0;//出力セット int MVal = N - K - 1;//MaxValue flag: for(int i = 0; i <= N; i++){ for(int j = 0; j <= MVal; j++){ if(0 > MVal){ break flag;//0以下だと動かせないのでbreak } if(val[j] > val[j + K]){//ソート long memory = val[j]; val[j] = val[j + K]; val[j + K] = memory; Output++; } } } for(int i = 1; i < N; i++){//数字のソートの確認 if(val[i - 1] > val[i]){ Output = -1; break; } } System.out.println(Output);//結果を表示 } }