import java.util.*;
public class Main{ 

   public static void main(String args[]) {
	 
    Scanner s=new Scanner(System.in);
    
    	int n=s.nextInt();
    	int k=s.nextInt();
    	long h[]=new long[n];
    	for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	}
    	long dp[]=new long[n];
    	dp[0]=0;
    	dp[1]=Math.abs(h[1]-h[0]);
    	
    	for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	}
    	System.out.println(dp[n-1]);
    	
    }
} import java.util.*; util.* java public class Main{ 

   public static void main(String args[]) {
	 
    Scanner s=new Scanner(System.in);
    
    	int n=s.nextInt();
    	int k=s.nextInt();
    	long h[]=new long[n];
    	for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	}
    	long dp[]=new long[n];
    	dp[0]=0;
    	dp[1]=Math.abs(h[1]-h[0]);
    	
    	for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	}
    	System.out.println(dp[n-1]);
    	
    }
} Main public static void main(String args[]) {
	 
    Scanner s=new Scanner(System.in);
    
    	int n=s.nextInt();
    	int k=s.nextInt();
    	long h[]=new long[n];
    	for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	}
    	long dp[]=new long[n];
    	dp[0]=0;
    	dp[1]=Math.abs(h[1]-h[0]);
    	
    	for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	}
    	System.out.println(dp[n-1]);
    	
    } main {
	 
    Scanner s=new Scanner(System.in);
    
    	int n=s.nextInt();
    	int k=s.nextInt();
    	long h[]=new long[n];
    	for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	}
    	long dp[]=new long[n];
    	dp[0]=0;
    	dp[1]=Math.abs(h[1]-h[0]);
    	
    	for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	}
    	System.out.println(dp[n-1]);
    	
    } Scanner s=new Scanner(System.in); s new Scanner(System.in) int n=s.nextInt(); n s.nextInt() s.nextInt s int k=s.nextInt(); k s.nextInt() s.nextInt s long h[]=new long[n]; h new long[n] n for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	} int i=0; int i=0; i 0 i<n i n i++ i++ i {
    		h[i]=s.nextLong();
    	} {
    		h[i]=s.nextLong();
    	} h[i]=s.nextLong() h[i] h i s.nextLong() s.nextLong s long dp[]=new long[n]; dp new long[n] n dp[0]=0 dp[0] dp 0 0 dp[1]=Math.abs(h[1]-h[0]) dp[1] dp 1 Math.abs(h[1]-h[0]) Math.abs Math h[1]-h[0] h[1] h 1 h[0] h 0 for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	} int i=2; int i=2; i 2 i<n i n i++ i++ i {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	} {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	} dp[i]=Long.MAX_VALUE dp[i] dp i Long.MAX_VALUE Long Long.MAX_VALUE for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 } int j=1; int j=1; j 1 j<=k j k j++ j++ j {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 } {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 } if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 } i-j>=0 i-j i j 0 {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 } long op1=dp[i-j] + Math.abs(h[i]-h[i-j]); op1 dp[i-j] + Math.abs(h[i]-h[i-j]) dp[i-j] dp i-j i j Math.abs(h[i]-h[i-j]) Math.abs Math h[i]-h[i-j] h[i] h i h[i-j] h i-j i j dp[i]=Math.min(dp[i], op1) dp[i] dp i Math.min(dp[i], op1) Math.min Math dp[i] dp i op1 {
    			 break;
    		 } break; System.out.println(dp[n-1]) System.out.println System.out System System.out dp[n-1] dp n-1 n 1 String args[] args public class Main{ 

   public static void main(String args[]) {
	 
    Scanner s=new Scanner(System.in);
    
    	int n=s.nextInt();
    	int k=s.nextInt();
    	long h[]=new long[n];
    	for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	}
    	long dp[]=new long[n];
    	dp[0]=0;
    	dp[1]=Math.abs(h[1]-h[0]);
    	
    	for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	}
    	System.out.println(dp[n-1]);
    	
    }
} public class Main{ 

   public static void main(String args[]) {
	 
    Scanner s=new Scanner(System.in);
    
    	int n=s.nextInt();
    	int k=s.nextInt();
    	long h[]=new long[n];
    	for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	}
    	long dp[]=new long[n];
    	dp[0]=0;
    	dp[1]=Math.abs(h[1]-h[0]);
    	
    	for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	}
    	System.out.println(dp[n-1]);
    	
    }
} Main
import java.util.*;
public class Main{ 

   public static void main(String args[]) {
	 
    Scanner s=new Scanner(System.in);
    
    	int n=s.nextInt();
    	int k=s.nextInt();
    	long h[]=new long[n];
    	for(int i=0;i<n;i++) {
    		h[i]=s.nextLong();
    	}
    	long dp[]=new long[n];
    	dp[0]=0;
    	dp[1]=Math.abs(h[1]-h[0]);
    	
    	for(int i=2;i<n;i++) {
    		dp[i]=Long.MAX_VALUE;
    		for(int j=1;j<=k;j++) {
    		 if(i-j>=0) {
	    		 long op1=dp[i-j] + Math.abs(h[i]-h[i-j]);
	    		 dp[i]=Math.min(dp[i], op1);
    		 }else {
    			 break;
    		 }
    	 }
    	}
    	System.out.println(dp[n-1]);
    	
    }
}