import java.util.*;
import java.io.*;
class Main{
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inp[]=br.readLine().split(" ");
		int n = Integer.parseInt(inp[0]);
		int k = Integer.parseInt(inp[1]);
		String in[]=br.readLine().split(" ");
		int arr[]=new int[n];
		for(int i=0;i<n;i++)
			arr[i]=Integer.parseInt(in[i]);
		int dp[]=new int[n];
		Arrays.fill(dp,1000000005);	
		dp[0]=0;	
		for(int i=0;i<n;i++){
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

		}
		System.out.println(dp[n-1]);
	}
} import java.util.*; util.* java import java.io.*; io.* java class Main{
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inp[]=br.readLine().split(" ");
		int n = Integer.parseInt(inp[0]);
		int k = Integer.parseInt(inp[1]);
		String in[]=br.readLine().split(" ");
		int arr[]=new int[n];
		for(int i=0;i<n;i++)
			arr[i]=Integer.parseInt(in[i]);
		int dp[]=new int[n];
		Arrays.fill(dp,1000000005);	
		dp[0]=0;	
		for(int i=0;i<n;i++){
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

		}
		System.out.println(dp[n-1]);
	}
} Main public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inp[]=br.readLine().split(" ");
		int n = Integer.parseInt(inp[0]);
		int k = Integer.parseInt(inp[1]);
		String in[]=br.readLine().split(" ");
		int arr[]=new int[n];
		for(int i=0;i<n;i++)
			arr[i]=Integer.parseInt(in[i]);
		int dp[]=new int[n];
		Arrays.fill(dp,1000000005);	
		dp[0]=0;	
		for(int i=0;i<n;i++){
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

		}
		System.out.println(dp[n-1]);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inp[]=br.readLine().split(" ");
		int n = Integer.parseInt(inp[0]);
		int k = Integer.parseInt(inp[1]);
		String in[]=br.readLine().split(" ");
		int arr[]=new int[n];
		for(int i=0;i<n;i++)
			arr[i]=Integer.parseInt(in[i]);
		int dp[]=new int[n];
		Arrays.fill(dp,1000000005);	
		dp[0]=0;	
		for(int i=0;i<n;i++){
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

		}
		System.out.println(dp[n-1]);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String inp[]=br.readLine().split(" "); inp br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int n = Integer.parseInt(inp[0]); n Integer.parseInt(inp[0]) Integer.parseInt Integer inp[0] inp 0 int k = Integer.parseInt(inp[1]); k Integer.parseInt(inp[1]) Integer.parseInt Integer inp[1] inp 1 String in[]=br.readLine().split(" "); in br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int arr[]=new int[n]; arr new int[n] n for(int i=0;i<n;i++)
			arr[i]=Integer.parseInt(in[i]); int i=0; int i=0; i 0 i<n i n i++ i++ i arr[i]=Integer.parseInt(in[i]); arr[i]=Integer.parseInt(in[i]) arr[i] arr i Integer.parseInt(in[i]) Integer.parseInt Integer in[i] in i int dp[]=new int[n]; dp new int[n] n Arrays.fill(dp,1000000005) Arrays.fill Arrays dp 1000000005 dp[0]=0 dp[0] dp 0 0 for(int i=0;i<n;i++){
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

		} {
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

		} for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			} int j=1; int j=1; j 1 j<=k j k j++ j++ j {
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			} {
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			} if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j])); i+j <n i+j i j n dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j])) dp[i+j] dp i+j i j Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j])) Math.min Math dp[i+j] dp i+j i j dp[i]+Math.abs(arr[i]-arr[i+j]) dp[i] dp i Math.abs(arr[i]-arr[i+j]) Math.abs Math arr[i]-arr[i+j] arr[i] arr i arr[i+j] arr i+j i j 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
import java.util.*;
import java.io.*;
class Main{
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inp[]=br.readLine().split(" ");
		int n = Integer.parseInt(inp[0]);
		int k = Integer.parseInt(inp[1]);
		String in[]=br.readLine().split(" ");
		int arr[]=new int[n];
		for(int i=0;i<n;i++)
			arr[i]=Integer.parseInt(in[i]);
		int dp[]=new int[n];
		Arrays.fill(dp,1000000005);	
		dp[0]=0;	
		for(int i=0;i<n;i++){
			for(int j=1;j<=k;j++){
				if(i+j <n)
					dp[i+j]=Math.min(dp[i+j],dp[i]+Math.abs(arr[i]-arr[i+j]));
			}	

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