import java.util.*;
import java.io.*;
class Main
{
	public static void main(String args[]) throws java.lang.Exception
	{
		BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
			String h[]=inp.readLine().split(" ");

			int n=Integer.parseInt(h[0]);
			int k=Integer.parseInt(h[1]);
			String aa[]=inp.readLine().split(" ");
			long arr[]=new long[n];

			for(int i=0;i<arr.length;i++)
			{
				arr[i]=Long.parseLong(aa[i]);
			}
			// PriorityQueue<Pair>pr=new PriorityQueue<>((a,b)->(int)(a.b-b.a));
			// boolean visited[]=new boolean[n];
			// visited[0]=true;
			// pr.offer(new Pair(0,(long)0));
			long cost[]=new long[n];
			cost[1]=Math.abs(arr[1]-arr[0]);
			for(int i=2;i<n;i++)
			{
				long min=Integer.MAX_VALUE;
				for(int j=1;j<=k && i>=j;j++){
				min=Math.min(cost[i-j]+Math.abs(arr[i]-arr[i-j]),min);}
				cost[i]=min;
			}
			System.out.println(cost[n-1]);
		
	}
	
}
class Pair
{
	int a;
	long b;
	
	Pair(int e, long f)
	{
		a=e;
		b=f;
		
	}
}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz