
import java.io.*; 
import java.util.*;

class Main //for atcoder
{ 
	public static void main (String[] args) throws IOException
	{ 
		    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        
	        String[] ip;
	       
	            
	            ip = br.readLine().trim().split("\\s+");
				int n = Integer.parseInt(ip[0]);
				int k = Integer.parseInt(ip[1]);
	            
	            ip = br.readLine().trim().split("\\s+");
	            int[] arr = new int[n];
	            for(int i=0;i<n;i++){
	                arr[i] = Integer.parseInt(ip[i]);
	            }
	            fun(arr,n,k);


	
	} 

	static void fun(int[] arr,int n,int k){

		int[] dp = new int[n];

		dp[0]=0;
		dp[1] = Math.abs(arr[1]-arr[0]);

		for(int i=2;i<n;i++){
			dp[i] = Integer.MAX_VALUE;
		}

		for(int i=2;i<n;i++){
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		}

		
			System.out.print(dp[n-1]);
	}

	
} 
 import java.io.*; io.* java import java.util.*; util.* java class Main //for atcoder
{ 
	public static void main (String[] args) throws IOException
	{ 
		    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        
	        String[] ip;
	       
	            
	            ip = br.readLine().trim().split("\\s+");
				int n = Integer.parseInt(ip[0]);
				int k = Integer.parseInt(ip[1]);
	            
	            ip = br.readLine().trim().split("\\s+");
	            int[] arr = new int[n];
	            for(int i=0;i<n;i++){
	                arr[i] = Integer.parseInt(ip[i]);
	            }
	            fun(arr,n,k);


	
	} 

	static void fun(int[] arr,int n,int k){

		int[] dp = new int[n];

		dp[0]=0;
		dp[1] = Math.abs(arr[1]-arr[0]);

		for(int i=2;i<n;i++){
			dp[i] = Integer.MAX_VALUE;
		}

		for(int i=2;i<n;i++){
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		}

		
			System.out.print(dp[n-1]);
	}

	
} Main public static void main (String[] args) throws IOException
	{ 
		    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        
	        String[] ip;
	       
	            
	            ip = br.readLine().trim().split("\\s+");
				int n = Integer.parseInt(ip[0]);
				int k = Integer.parseInt(ip[1]);
	            
	            ip = br.readLine().trim().split("\\s+");
	            int[] arr = new int[n];
	            for(int i=0;i<n;i++){
	                arr[i] = Integer.parseInt(ip[i]);
	            }
	            fun(arr,n,k);


	
	} main { 
		    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        
	        String[] ip;
	       
	            
	            ip = br.readLine().trim().split("\\s+");
				int n = Integer.parseInt(ip[0]);
				int k = Integer.parseInt(ip[1]);
	            
	            ip = br.readLine().trim().split("\\s+");
	            int[] arr = new int[n];
	            for(int i=0;i<n;i++){
	                arr[i] = Integer.parseInt(ip[i]);
	            }
	            fun(arr,n,k);


	
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] ip; ip ip = br.readLine().trim().split("\\s+") ip br.readLine().trim().split("\\s+") br.readLine().trim().split br.readLine().trim() br.readLine().trim br.readLine() br.readLine br "\\s+" int n = Integer.parseInt(ip[0]); n Integer.parseInt(ip[0]) Integer.parseInt Integer ip[0] ip 0 int k = Integer.parseInt(ip[1]); k Integer.parseInt(ip[1]) Integer.parseInt Integer ip[1] ip 1 ip = br.readLine().trim().split("\\s+") ip br.readLine().trim().split("\\s+") br.readLine().trim().split br.readLine().trim() br.readLine().trim br.readLine() br.readLine br "\\s+" int[] arr = new int[n]; arr new int[n] n for(int i=0;i<n;i++){
	                arr[i] = Integer.parseInt(ip[i]);
	            } int i=0; int i=0; i 0 i<n i n i++ i++ i {
	                arr[i] = Integer.parseInt(ip[i]);
	            } {
	                arr[i] = Integer.parseInt(ip[i]);
	            } arr[i] = Integer.parseInt(ip[i]) arr[i] arr i Integer.parseInt(ip[i]) Integer.parseInt Integer ip[i] ip i fun(arr,n,k) fun arr n k String[] args args static void fun(int[] arr,int n,int k){

		int[] dp = new int[n];

		dp[0]=0;
		dp[1] = Math.abs(arr[1]-arr[0]);

		for(int i=2;i<n;i++){
			dp[i] = Integer.MAX_VALUE;
		}

		for(int i=2;i<n;i++){
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		}

		
			System.out.print(dp[n-1]);
	} fun {

		int[] dp = new int[n];

		dp[0]=0;
		dp[1] = Math.abs(arr[1]-arr[0]);

		for(int i=2;i<n;i++){
			dp[i] = Integer.MAX_VALUE;
		}

		for(int i=2;i<n;i++){
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		}

		
			System.out.print(dp[n-1]);
	} int[] dp = new int[n]; dp new int[n] n dp[0]=0 dp[0] dp 0 0 dp[1] = Math.abs(arr[1]-arr[0]) dp[1] dp 1 Math.abs(arr[1]-arr[0]) Math.abs Math arr[1]-arr[0] arr[1] arr 1 arr[0] arr 0 for(int i=2;i<n;i++){
			dp[i] = Integer.MAX_VALUE;
		} int i=2; int i=2; i 2 i<n i n i++ i++ i {
			dp[i] = Integer.MAX_VALUE;
		} {
			dp[i] = Integer.MAX_VALUE;
		} dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i=2;i<n;i++){
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		} int i=2; int i=2; i 2 i<n i n i++ i++ i {
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		} {
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		} for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			} int j=i-1; int j=i-1; j i-1 i 1 j>=i-k j i-k i k j-- j-- j {


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			} {


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			} if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]); j>=0 j 0 dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]) dp[i] dp i Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]) Math.min Math dp[i] dp i Math.abs(arr[i]-arr[j]) + dp[j] Math.abs(arr[i]-arr[j]) Math.abs Math arr[i]-arr[j] arr[i] arr i arr[j] arr j dp[j] dp j System.out.print(dp[n-1]) System.out.print System.out System System.out dp[n-1] dp n-1 n 1 int[] arr arr int n n int k k

import java.io.*; 
import java.util.*;

class Main //for atcoder
{ 
	public static void main (String[] args) throws IOException
	{ 
		    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        
	        String[] ip;
	       
	            
	            ip = br.readLine().trim().split("\\s+");
				int n = Integer.parseInt(ip[0]);
				int k = Integer.parseInt(ip[1]);
	            
	            ip = br.readLine().trim().split("\\s+");
	            int[] arr = new int[n];
	            for(int i=0;i<n;i++){
	                arr[i] = Integer.parseInt(ip[i]);
	            }
	            fun(arr,n,k);


	
	} 

	static void fun(int[] arr,int n,int k){

		int[] dp = new int[n];

		dp[0]=0;
		dp[1] = Math.abs(arr[1]-arr[0]);

		for(int i=2;i<n;i++){
			dp[i] = Integer.MAX_VALUE;
		}

		for(int i=2;i<n;i++){
			


			for(int j=i-1;j>=i-k;j--){


				
				
				if(j>=0)
				dp[i] = Math.min(dp[i], Math.abs(arr[i]-arr[j]) + dp[j]);

			}

		

		
		


		}

		
			System.out.print(dp[n-1]);
	}

	
} 

