	import java.util.*;
	import java.io.*;
class Main{
		static int n,arr[],k	;
		static long dp[];
		public static void main(String[] args) throws IOException{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			int t=1;
	//		Integer.parseInt(br.readLine().trim());
			StringBuilder printans=new StringBuilder();
			while(t-->0)
			{
				StringTokenizer tok=new StringTokenizer(br.readLine()," ");
				n=Integer.parseInt(tok.nextToken());
				k=Integer.parseInt(tok.nextToken());
				
				arr=new int[n];
				tok=new StringTokenizer(br.readLine()," ");
				for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				}
				dp=new long[n];
//				memset();
				
				printans.append(call());
			    
			}
			System.out.println(printans);
	
		}
		
		static long call() {
			dp[0]=0;
			dp[1]=Math.abs(arr[1]-arr[0]);
			for(int i=2;i<n;i++) {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			}
//			System.out.println(Arrays.toString(dp));
			return dp[dp.length-1];
		}
	
	} import java.util.*; util.* java import java.io.*; io.* java class Main{
		static int n,arr[],k	;
		static long dp[];
		public static void main(String[] args) throws IOException{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			int t=1;
	//		Integer.parseInt(br.readLine().trim());
			StringBuilder printans=new StringBuilder();
			while(t-->0)
			{
				StringTokenizer tok=new StringTokenizer(br.readLine()," ");
				n=Integer.parseInt(tok.nextToken());
				k=Integer.parseInt(tok.nextToken());
				
				arr=new int[n];
				tok=new StringTokenizer(br.readLine()," ");
				for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				}
				dp=new long[n];
//				memset();
				
				printans.append(call());
			    
			}
			System.out.println(printans);
	
		}
		
		static long call() {
			dp[0]=0;
			dp[1]=Math.abs(arr[1]-arr[0]);
			for(int i=2;i<n;i++) {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			}
//			System.out.println(Arrays.toString(dp));
			return dp[dp.length-1];
		}
	
	} Main static int n n arr[] arr k	; k static long dp[]; dp public static void main(String[] args) throws IOException{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			int t=1;
	//		Integer.parseInt(br.readLine().trim());
			StringBuilder printans=new StringBuilder();
			while(t-->0)
			{
				StringTokenizer tok=new StringTokenizer(br.readLine()," ");
				n=Integer.parseInt(tok.nextToken());
				k=Integer.parseInt(tok.nextToken());
				
				arr=new int[n];
				tok=new StringTokenizer(br.readLine()," ");
				for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				}
				dp=new long[n];
//				memset();
				
				printans.append(call());
			    
			}
			System.out.println(printans);
	
		} main {
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			int t=1;
	//		Integer.parseInt(br.readLine().trim());
			StringBuilder printans=new StringBuilder();
			while(t-->0)
			{
				StringTokenizer tok=new StringTokenizer(br.readLine()," ");
				n=Integer.parseInt(tok.nextToken());
				k=Integer.parseInt(tok.nextToken());
				
				arr=new int[n];
				tok=new StringTokenizer(br.readLine()," ");
				for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				}
				dp=new long[n];
//				memset();
				
				printans.append(call());
			    
			}
			System.out.println(printans);
	
		} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int t=1; t 1 StringBuilder printans=new StringBuilder(); printans new StringBuilder() while(t-->0)
			{
				StringTokenizer tok=new StringTokenizer(br.readLine()," ");
				n=Integer.parseInt(tok.nextToken());
				k=Integer.parseInt(tok.nextToken());
				
				arr=new int[n];
				tok=new StringTokenizer(br.readLine()," ");
				for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				}
				dp=new long[n];
//				memset();
				
				printans.append(call());
			    
			} t-->0 t-- t 0 {
				StringTokenizer tok=new StringTokenizer(br.readLine()," ");
				n=Integer.parseInt(tok.nextToken());
				k=Integer.parseInt(tok.nextToken());
				
				arr=new int[n];
				tok=new StringTokenizer(br.readLine()," ");
				for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				}
				dp=new long[n];
//				memset();
				
				printans.append(call());
			    
			} StringTokenizer tok=new StringTokenizer(br.readLine()," "); tok new StringTokenizer(br.readLine()," ") n=Integer.parseInt(tok.nextToken()) n Integer.parseInt(tok.nextToken()) Integer.parseInt Integer tok.nextToken() tok.nextToken tok k=Integer.parseInt(tok.nextToken()) k Integer.parseInt(tok.nextToken()) Integer.parseInt Integer tok.nextToken() tok.nextToken tok arr=new int[n] arr new int[n] n tok=new StringTokenizer(br.readLine()," ") tok new StringTokenizer(br.readLine()," ") for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				} int i=0; int i=0; i 0 i<n i n i++ i++ i {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				} {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				} arr[i]=Integer.parseInt(tok.nextToken()) arr[i] arr i Integer.parseInt(tok.nextToken()) Integer.parseInt Integer tok.nextToken() tok.nextToken tok dp=new long[n] dp new long[n] n printans.append(call()) printans.append printans call() call System.out.println(printans) System.out.println System.out System System.out printans String[] args args static long call() {
			dp[0]=0;
			dp[1]=Math.abs(arr[1]-arr[0]);
			for(int i=2;i<n;i++) {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			}
//			System.out.println(Arrays.toString(dp));
			return dp[dp.length-1];
		} call {
			dp[0]=0;
			dp[1]=Math.abs(arr[1]-arr[0]);
			for(int i=2;i<n;i++) {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			}
//			System.out.println(Arrays.toString(dp));
			return dp[dp.length-1];
		} 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++) {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			} int i=2; int i=2; i 2 i<n i n i++ i++ i {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			} {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			} long temp=Integer.MAX_VALUE; temp Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				} int j=i-k; int j=i-k; j i-k i k j<i j i j++ j++ j {
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				} {
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				} if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					} j>=0 j 0 {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					} temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j])) temp Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j])) Math.min Math temp dp[j]+Math.abs(arr[i]-arr[j]) dp[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[i]=temp dp[i] dp i temp return dp[dp.length-1]; dp[dp.length-1] dp dp.length-1 dp.length dp dp.length 1
	import java.util.*;
	import java.io.*;
class Main{
		static int n,arr[],k	;
		static long dp[];
		public static void main(String[] args) throws IOException{
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			int t=1;
	//		Integer.parseInt(br.readLine().trim());
			StringBuilder printans=new StringBuilder();
			while(t-->0)
			{
				StringTokenizer tok=new StringTokenizer(br.readLine()," ");
				n=Integer.parseInt(tok.nextToken());
				k=Integer.parseInt(tok.nextToken());
				
				arr=new int[n];
				tok=new StringTokenizer(br.readLine()," ");
				for(int i=0;i<n;i++) {
					arr[i]=Integer.parseInt(tok.nextToken());
				
				}
				dp=new long[n];
//				memset();
				
				printans.append(call());
			    
			}
			System.out.println(printans);
	
		}
		
		static long call() {
			dp[0]=0;
			dp[1]=Math.abs(arr[1]-arr[0]);
			for(int i=2;i<n;i++) {
				long temp=Integer.MAX_VALUE;
				for(int j=i-k;j<i ;j++){
					if(j>=0) {
						temp=Math.min(temp,dp[j]+Math.abs(arr[i]-arr[j]));
					}
//					
				}
				dp[i]=temp;
				
			}
//			System.out.println(Arrays.toString(dp));
			return dp[dp.length-1];
		}
	
	}
