import java.util.Scanner;
public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	private static int dp[];
	public static int findMinCost(int arr[],int n,int k){
		if(n>=arr.length)
			return Integer.MAX_VALUE;
		if(n==arr.length-1)
			return 0;
		
		if(dp[n]!=Integer.MAX_VALUE)
			return dp[n];
		
		int cost=Integer.MAX_VALUE;
		
		for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		}
		dp[n]=cost;
		return cost;
	}
	
	public static void main(String []args){
		int n=sc.nextInt();
		int k=sc.nextInt();
		int arr[]=new int[n];
		dp=new int[arr.length];
		for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		}
		System.out.println(findMinCost(arr,0,k));
		// for(int i=0;i<n;i++)
		 	// System.out.print(dp[i]+" ");
		
	}
} import java.util.Scanner; Scanner java.util public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	private static int dp[];
	public static int findMinCost(int arr[],int n,int k){
		if(n>=arr.length)
			return Integer.MAX_VALUE;
		if(n==arr.length-1)
			return 0;
		
		if(dp[n]!=Integer.MAX_VALUE)
			return dp[n];
		
		int cost=Integer.MAX_VALUE;
		
		for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		}
		dp[n]=cost;
		return cost;
	}
	
	public static void main(String []args){
		int n=sc.nextInt();
		int k=sc.nextInt();
		int arr[]=new int[n];
		dp=new int[arr.length];
		for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		}
		System.out.println(findMinCost(arr,0,k));
		// for(int i=0;i<n;i++)
		 	// System.out.print(dp[i]+" ");
		
	}
} Main private static Scanner sc=new Scanner(System.in); sc new Scanner(System.in) private static int dp[]; dp public static int findMinCost(int arr[],int n,int k){
		if(n>=arr.length)
			return Integer.MAX_VALUE;
		if(n==arr.length-1)
			return 0;
		
		if(dp[n]!=Integer.MAX_VALUE)
			return dp[n];
		
		int cost=Integer.MAX_VALUE;
		
		for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		}
		dp[n]=cost;
		return cost;
	} findMinCost {
		if(n>=arr.length)
			return Integer.MAX_VALUE;
		if(n==arr.length-1)
			return 0;
		
		if(dp[n]!=Integer.MAX_VALUE)
			return dp[n];
		
		int cost=Integer.MAX_VALUE;
		
		for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		}
		dp[n]=cost;
		return cost;
	} if(n>=arr.length)
			return Integer.MAX_VALUE; n>=arr.length n arr.length arr arr.length return Integer.MAX_VALUE; Integer.MAX_VALUE Integer Integer.MAX_VALUE if(n==arr.length-1)
			return 0; n==arr.length-1 n arr.length-1 arr.length arr arr.length 1 return 0; 0 if(dp[n]!=Integer.MAX_VALUE)
			return dp[n]; dp[n]!=Integer.MAX_VALUE dp[n] dp n Integer.MAX_VALUE Integer Integer.MAX_VALUE return dp[n]; dp[n] dp n int cost=Integer.MAX_VALUE; cost Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		} int i=1; int i=1; i 1 i<=k i k i++ i++ i {
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		} {
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		} if(n+i>=arr.length)
				break; n+i>=arr.length n+i n i arr.length arr arr.length break; cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n])) cost Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n])) Math.min Math cost findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]) findMinCost(arr,n+i,k) findMinCost arr n+i n i k Math.abs(arr[n+i]-arr[n]) Math.abs Math arr[n+i]-arr[n] arr[n+i] arr n+i n i arr[n] arr n dp[n]=cost dp[n] dp n cost return cost; cost int arr[] arr int n n int k k public static void main(String []args){
		int n=sc.nextInt();
		int k=sc.nextInt();
		int arr[]=new int[n];
		dp=new int[arr.length];
		for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		}
		System.out.println(findMinCost(arr,0,k));
		// for(int i=0;i<n;i++)
		 	// System.out.print(dp[i]+" ");
		
	} main {
		int n=sc.nextInt();
		int k=sc.nextInt();
		int arr[]=new int[n];
		dp=new int[arr.length];
		for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		}
		System.out.println(findMinCost(arr,0,k));
		// for(int i=0;i<n;i++)
		 	// System.out.print(dp[i]+" ");
		
	} int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc int arr[]=new int[n]; arr new int[n] n dp=new int[arr.length] dp new int[arr.length] arr.length arr arr.length for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		} {
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		} arr[i]=sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc dp[i]=Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE System.out.println(findMinCost(arr,0,k)) System.out.println System.out System System.out findMinCost(arr,0,k) findMinCost arr 0 k String []args args public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	private static int dp[];
	public static int findMinCost(int arr[],int n,int k){
		if(n>=arr.length)
			return Integer.MAX_VALUE;
		if(n==arr.length-1)
			return 0;
		
		if(dp[n]!=Integer.MAX_VALUE)
			return dp[n];
		
		int cost=Integer.MAX_VALUE;
		
		for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		}
		dp[n]=cost;
		return cost;
	}
	
	public static void main(String []args){
		int n=sc.nextInt();
		int k=sc.nextInt();
		int arr[]=new int[n];
		dp=new int[arr.length];
		for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		}
		System.out.println(findMinCost(arr,0,k));
		// for(int i=0;i<n;i++)
		 	// System.out.print(dp[i]+" ");
		
	}
} public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	private static int dp[];
	public static int findMinCost(int arr[],int n,int k){
		if(n>=arr.length)
			return Integer.MAX_VALUE;
		if(n==arr.length-1)
			return 0;
		
		if(dp[n]!=Integer.MAX_VALUE)
			return dp[n];
		
		int cost=Integer.MAX_VALUE;
		
		for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		}
		dp[n]=cost;
		return cost;
	}
	
	public static void main(String []args){
		int n=sc.nextInt();
		int k=sc.nextInt();
		int arr[]=new int[n];
		dp=new int[arr.length];
		for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		}
		System.out.println(findMinCost(arr,0,k));
		// for(int i=0;i<n;i++)
		 	// System.out.print(dp[i]+" ");
		
	}
} Main
import java.util.Scanner;
public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	private static int dp[];
	public static int findMinCost(int arr[],int n,int k){
		if(n>=arr.length)
			return Integer.MAX_VALUE;
		if(n==arr.length-1)
			return 0;
		
		if(dp[n]!=Integer.MAX_VALUE)
			return dp[n];
		
		int cost=Integer.MAX_VALUE;
		
		for(int i=1;i<=k;i++){
			if(n+i>=arr.length)
				break;
			cost=Math.min(cost,findMinCost(arr,n+i,k)+Math.abs(arr[n+i]-arr[n]));			
		}
		dp[n]=cost;
		return cost;
	}
	
	public static void main(String []args){
		int n=sc.nextInt();
		int k=sc.nextInt();
		int arr[]=new int[n];
		dp=new int[arr.length];
		for(int i=0;i<n;i++){
			arr[i]=sc.nextInt();
			dp[i]=Integer.MAX_VALUE;
		}
		System.out.println(findMinCost(arr,0,k));
		// for(int i=0;i<n;i++)
		 	// System.out.print(dp[i]+" ");
		
	}
}