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

class Main {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String[] s3=br.readLine().split(" ");
		int n=Integer.parseInt(s3[0].trim());
		int k=Integer.parseInt(s3[1].trim());
		String[] s=br.readLine().split(" ");
		int[] a=new int[n];
		int i=0;
		for(String s1:s) {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		}
		System.out.println(algo(a,n,k));
		
		
		

	}
	
	public static int algo(int[] a,int n,int k) {
		int[] dp=new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
//		dp[0]=0;
		dp[0]=0;
		for(int i=1;i<n;i++) {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		}
		return dp[n-1];
		
	}
	
	
	
	

} import java.util.*; util.* java import java.io.*; io.* java class Main {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String[] s3=br.readLine().split(" ");
		int n=Integer.parseInt(s3[0].trim());
		int k=Integer.parseInt(s3[1].trim());
		String[] s=br.readLine().split(" ");
		int[] a=new int[n];
		int i=0;
		for(String s1:s) {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		}
		System.out.println(algo(a,n,k));
		
		
		

	}
	
	public static int algo(int[] a,int n,int k) {
		int[] dp=new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
//		dp[0]=0;
		dp[0]=0;
		for(int i=1;i<n;i++) {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		}
		return dp[n-1];
		
	}
	
	
	
	

} Main public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String[] s3=br.readLine().split(" ");
		int n=Integer.parseInt(s3[0].trim());
		int k=Integer.parseInt(s3[1].trim());
		String[] s=br.readLine().split(" ");
		int[] a=new int[n];
		int i=0;
		for(String s1:s) {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		}
		System.out.println(algo(a,n,k));
		
		
		

	} main {
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String[] s3=br.readLine().split(" ");
		int n=Integer.parseInt(s3[0].trim());
		int k=Integer.parseInt(s3[1].trim());
		String[] s=br.readLine().split(" ");
		int[] a=new int[n];
		int i=0;
		for(String s1:s) {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		}
		System.out.println(algo(a,n,k));
		
		
		

	} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] s3=br.readLine().split(" "); s3 br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int n=Integer.parseInt(s3[0].trim()); n Integer.parseInt(s3[0].trim()) Integer.parseInt Integer s3[0].trim() s3[0].trim s3[0] s3 0 int k=Integer.parseInt(s3[1].trim()); k Integer.parseInt(s3[1].trim()) Integer.parseInt Integer s3[1].trim() s3[1].trim s3[1] s3 1 String[] s=br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int[] a=new int[n]; a new int[n] n int i=0; i 0 for(String s1:s) {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		} String s1 s {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		} {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		} a[i]=Integer.parseInt(s[i].trim()) a[i] a i Integer.parseInt(s[i].trim()) Integer.parseInt Integer s[i].trim() s[i].trim s[i] s i i++ i System.out.println(algo(a,n,k)) System.out.println System.out System System.out algo(a,n,k) algo a n k String[] args args public static int algo(int[] a,int n,int k) {
		int[] dp=new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
//		dp[0]=0;
		dp[0]=0;
		for(int i=1;i<n;i++) {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		}
		return dp[n-1];
		
	} algo {
		int[] dp=new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
//		dp[0]=0;
		dp[0]=0;
		for(int i=1;i<n;i++) {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		}
		return dp[n-1];
		
	} int[] dp=new int[n]; dp new int[n] n Arrays.fill(dp, Integer.MAX_VALUE) Arrays.fill Arrays dp Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0]=0 dp[0] dp 0 0 for(int i=1;i<n;i++) {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		} {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		} for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			} int k1=1; int k1=1; k1 1 k1<=k k1 k k1++ k1++ k1 {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			} {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			} if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			} i-k1>=0 i-k1 i k1 0 {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			} int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]); x dp[i-k1]+Math.abs(a[i]-a[i-k1]) dp[i-k1] dp i-k1 i k1 Math.abs(a[i]-a[i-k1]) Math.abs Math a[i]-a[i-k1] a[i] a i a[i-k1] a i-k1 i k1 dp[i]=Math.min(dp[i],x) dp[i] dp i Math.min(dp[i],x) Math.min Math dp[i] dp i x return dp[n-1]; dp[n-1] dp n-1 n 1 int[] a a int n n int k k

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

class Main {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String[] s3=br.readLine().split(" ");
		int n=Integer.parseInt(s3[0].trim());
		int k=Integer.parseInt(s3[1].trim());
		String[] s=br.readLine().split(" ");
		int[] a=new int[n];
		int i=0;
		for(String s1:s) {
			a[i]=Integer.parseInt(s[i].trim());
			i++;
		}
		System.out.println(algo(a,n,k));
		
		
		

	}
	
	public static int algo(int[] a,int n,int k) {
		int[] dp=new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
//		dp[0]=0;
		dp[0]=0;
		for(int i=1;i<n;i++) {
			for(int k1=1;k1<=k;k1++) {
			if(i-k1>=0) {
				int x=dp[i-k1]+Math.abs(a[i]-a[i-k1]);
				dp[i]=Math.min(dp[i],x);
			}
			
			}
		}
		return dp[n-1];
		
	}
	
	
	
	

}
