import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int k=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		System.out.println(frog1(arr,k));
		
		
	}
	public static int frog1(int[] heights,int k) {
		
		int[] dp=new int[heights.length];
		dp[0]=0;
		dp[1]=Math.abs(heights[0]-heights[1]);
		
		int min=0;
		for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		}
		
		return dp[heights.length-1];
			
	}

} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int k=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		System.out.println(frog1(arr,k));
		
		
	}
	public static int frog1(int[] heights,int k) {
		
		int[] dp=new int[heights.length];
		dp[0]=0;
		dp[1]=Math.abs(heights[0]-heights[1]);
		
		int min=0;
		for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		}
		
		return dp[heights.length-1];
			
	}

} Main public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int k=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		System.out.println(frog1(arr,k));
		
		
	} main {
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int k=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		System.out.println(frog1(arr,k));
		
		
	} Scanner scn=new Scanner(System.in); scn new Scanner(System.in) int n=scn.nextInt(); n scn.nextInt() scn.nextInt scn int k=scn.nextInt(); k scn.nextInt() scn.nextInt scn int[] arr=new int[n]; arr new int[n] n for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			arr[i]=scn.nextInt();
		} {
			arr[i]=scn.nextInt();
		} arr[i]=scn.nextInt() arr[i] arr i scn.nextInt() scn.nextInt scn System.out.println(frog1(arr,k)) System.out.println System.out System System.out frog1(arr,k) frog1 arr k String[] args args public static int frog1(int[] heights,int k) {
		
		int[] dp=new int[heights.length];
		dp[0]=0;
		dp[1]=Math.abs(heights[0]-heights[1]);
		
		int min=0;
		for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		}
		
		return dp[heights.length-1];
			
	} frog1 {
		
		int[] dp=new int[heights.length];
		dp[0]=0;
		dp[1]=Math.abs(heights[0]-heights[1]);
		
		int min=0;
		for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		}
		
		return dp[heights.length-1];
			
	} int[] dp=new int[heights.length]; dp new int[heights.length] heights.length heights heights.length dp[0]=0 dp[0] dp 0 0 dp[1]=Math.abs(heights[0]-heights[1]) dp[1] dp 1 Math.abs(heights[0]-heights[1]) Math.abs Math heights[0]-heights[1] heights[0] heights 0 heights[1] heights 1 int min=0; min 0 for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		} int vidx=2; int vidx=2; vidx 2 vidx<heights.length vidx heights.length heights heights.length vidx++ vidx++ vidx {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		} {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		} min=Integer.MAX_VALUE min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			} int j=vidx-k; int j=vidx-k; j vidx-k vidx k j<vidx j vidx j++ j++ j {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			} {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			} if((j)<0) {
					continue;
				} (j)<0 (j) 0 {
					continue;
				} continue; int val=Math.abs(heights[vidx]-heights[j])+dp[j]; val Math.abs(heights[vidx]-heights[j])+dp[j] Math.abs(heights[vidx]-heights[j]) Math.abs Math heights[vidx]-heights[j] heights[vidx] heights vidx heights[j] heights j dp[j] dp j if(min>val) {
					min=val;
					dp[vidx]=min;
				} min>val min val {
					min=val;
					dp[vidx]=min;
				} min=val min val dp[vidx]=min dp[vidx] dp vidx min return dp[heights.length-1]; dp[heights.length-1] dp heights.length-1 heights.length heights heights.length 1 int[] heights heights int k k public class Main {
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int k=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		System.out.println(frog1(arr,k));
		
		
	}
	public static int frog1(int[] heights,int k) {
		
		int[] dp=new int[heights.length];
		dp[0]=0;
		dp[1]=Math.abs(heights[0]-heights[1]);
		
		int min=0;
		for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		}
		
		return dp[heights.length-1];
			
	}

} public class Main {
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int k=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		System.out.println(frog1(arr,k));
		
		
	}
	public static int frog1(int[] heights,int k) {
		
		int[] dp=new int[heights.length];
		dp[0]=0;
		dp[1]=Math.abs(heights[0]-heights[1]);
		
		int min=0;
		for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		}
		
		return dp[heights.length-1];
			
	}

} Main
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int k=scn.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++) {
			arr[i]=scn.nextInt();
		}
		System.out.println(frog1(arr,k));
		
		
	}
	public static int frog1(int[] heights,int k) {
		
		int[] dp=new int[heights.length];
		dp[0]=0;
		dp[1]=Math.abs(heights[0]-heights[1]);
		
		int min=0;
		for(int vidx=2;vidx<heights.length;vidx++) {
			min=Integer.MAX_VALUE;
			for(int j=vidx-k;j<vidx;j++) {
				if((j)<0) {
					continue;
				}
		
				int val=Math.abs(heights[vidx]-heights[j])+dp[j];
				
				if(min>val) {
					min=val;
					dp[vidx]=min;
				}
				
			}
			
			
		}
		
		return dp[heights.length-1];
			
	}

}
