import java.util.Scanner;

public class Main {

	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int h[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		}
		
		int cost[] = new int[n+1];
		cost[1] = 0;
		
		for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		}
		
		System.out.println(cost[n]);
	}
} import java.util.Scanner; Scanner java.util public class Main {

	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int h[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		}
		
		int cost[] = new int[n+1];
		cost[1] = 0;
		
		for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		}
		
		System.out.println(cost[n]);
	}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int h[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		}
		
		int cost[] = new int[n+1];
		cost[1] = 0;
		
		for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		}
		
		System.out.println(cost[n]);
	} main {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int h[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		}
		
		int cost[] = new int[n+1];
		cost[1] = 0;
		
		for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		}
		
		System.out.println(cost[n]);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int h[] = new int[n+1]; h new int[n+1] n+1 n 1 for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		} int i=1; int i=1; i 1 i<=n i n i++ i++ i {
			h[i] = scan.nextInt();
		} {
			h[i] = scan.nextInt();
		} h[i] = scan.nextInt() h[i] h i scan.nextInt() scan.nextInt scan int cost[] = new int[n+1]; cost new int[n+1] n+1 n 1 cost[1] = 0 cost[1] cost 1 0 for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		} int i=2; int i=2; i 2 i<=n i n i++ i++ i {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		} {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		} int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			} int j=1; int j=1; j 1 j<=k j k j++ j++ j {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			} {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			} if(i - j <=0) {
					break;
				} i - j <=0 i - j i j 0 {
					break;
				} break; min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]) min Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]) Math.min Math min Math.abs(h[i]-h[i-j]) + cost[i-j] Math.abs(h[i]-h[i-j]) Math.abs Math h[i]-h[i-j] h[i] h i h[i-j] h i-j i j cost[i-j] cost i-j i j cost[i] = min cost[i] cost i min System.out.println(cost[n]) System.out.println System.out System System.out cost[n] cost n String[] args args public class Main {

	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int h[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		}
		
		int cost[] = new int[n+1];
		cost[1] = 0;
		
		for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		}
		
		System.out.println(cost[n]);
	}
} public class Main {

	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int h[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		}
		
		int cost[] = new int[n+1];
		cost[1] = 0;
		
		for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		}
		
		System.out.println(cost[n]);
	}
} Main
import java.util.Scanner;

public class Main {

	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int h[] = new int[n+1];
		for(int i=1;i<=n;i++) {
			h[i] = scan.nextInt();
		}
		
		int cost[] = new int[n+1];
		cost[1] = 0;
		
		for(int i=2;i<=n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j=1;j<=k;j++) {
				if(i - j <=0) {
					break;
				}
				
				min = Math.min(min, Math.abs(h[i]-h[i-j]) + cost[i-j]);
				
			}
			
			cost[i] = min;
			
		}
		
		System.out.println(cost[n]);
	}
}
