import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(), k = in.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;++i) a[i]=in.nextInt();
		int[] dp = new int[n];
		for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        }
	System.out.println(dp[n-1]);
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(), k = in.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;++i) a[i]=in.nextInt();
		int[] dp = new int[n];
		for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        }
	System.out.println(dp[n-1]);
	}
} Main public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(), k = in.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;++i) a[i]=in.nextInt();
		int[] dp = new int[n];
		for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        }
	System.out.println(dp[n-1]);
	} main {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(), k = in.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;++i) a[i]=in.nextInt();
		int[] dp = new int[n];
		for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        }
	System.out.println(dp[n-1]);
	} Scanner in=new Scanner(System.in); in new Scanner(System.in) int n=in.nextInt() n in.nextInt() in.nextInt in k = in.nextInt(); k in.nextInt() in.nextInt in int[] a=new int[n]; a new int[n] n for (int i=0;i<n;++i) a[i]=in.nextInt(); int i=0; int i=0; i 0 i<n i n ++i ++i i a[i]=in.nextInt(); a[i]=in.nextInt() a[i] a i in.nextInt() in.nextInt in int[] dp = new int[n]; dp new int[n] n for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        } int i=1; int i=1; i 1 i<n i n ++i ++i i {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        } {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        } dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]) dp[i] dp i dp[i-1] + Math.abs(a[i] - a[i-1]) dp[i-1] dp i-1 i 1 Math.abs(a[i] - a[i-1]) Math.abs Math a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               } int j = 2; int j = 2; j 2 j <=k j k j++ j++ j {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               } {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               } if(i - j < 0) {
                            break;
                     } i - j < 0 i - j i j 0 {
                            break;
                     } break; dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j])) dp[i] dp i Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j])) Math.min Math dp[i] dp i dp[i-j] + Math.abs(a[i] - a[i-j]) dp[i-j] dp i-j i j Math.abs(a[i] - a[i-j]) Math.abs Math a[i] - a[i-j] a[i] a i a[i-j] a 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 public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(), k = in.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;++i) a[i]=in.nextInt();
		int[] dp = new int[n];
		for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        }
	System.out.println(dp[n-1]);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(), k = in.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;++i) a[i]=in.nextInt();
		int[] dp = new int[n];
		for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        }
	System.out.println(dp[n-1]);
	}
} Main
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt(), k = in.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;++i) a[i]=in.nextInt();
		int[] dp = new int[n];
		for (int i=1;i<n;++i) {
                    dp[i] = dp[i-1] + Math.abs(a[i] - a[i-1]);
                    for(int j = 2; j <=k; j++) {
                        if(i - j < 0) {
                            break;
                     }
                    dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
               }
        }
	System.out.println(dp[n-1]);
	}
}
