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 c = 0; int MV = N - K - 1; flag: for(int i = 0; i <= MV; i++){ for(int j = 0; j <= MV; j++){ if(0 > MV){ break flag; } if(val[j] > val[j + K]){ long tmp = val[j]; val[j] = val[j + K]; val[j + K] = tmp; c++; } } } for(int i = 1; i < N; i++){ if(val[i - 1] > val[i]){ c = -1; break; } } System.out.println(c); } }