import java.util.Scanner; class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(),k=sc.nextInt(),a[]=new int[n],s=0,t,i,j; for(i=0;i=i+k;--j) { if(a[j-k]>a[j]) { t=a[j-k]; a[j-k]=a[j]; a[j]=t; ++s; } } } for(i=1;ia[i]) break; if(i==n) System.out.print(s); else System.out.print(-1); } }