import java.util.*;
public class Main {
    
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n, k;
        n = sc.nextInt();
        k = sc.nextInt();
        int[] height = new int[n];

        for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        }

        int[] dp = new int[n];

        for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        }

        System.out.println(dp[n - 1]);
    }
} import java.util.*; util.* java public class Main {
    
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n, k;
        n = sc.nextInt();
        k = sc.nextInt();
        int[] height = new int[n];

        for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        }

        int[] dp = new int[n];

        for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        }

        System.out.println(dp[n - 1]);
    }
} Main public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n, k;
        n = sc.nextInt();
        k = sc.nextInt();
        int[] height = new int[n];

        for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        }

        int[] dp = new int[n];

        for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        }

        System.out.println(dp[n - 1]);
    } main {

        Scanner sc = new Scanner(System.in);
        int n, k;
        n = sc.nextInt();
        k = sc.nextInt();
        int[] height = new int[n];

        for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        }

        int[] dp = new int[n];

        for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        }

        System.out.println(dp[n - 1]);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n n k; k n = sc.nextInt() n sc.nextInt() sc.nextInt sc k = sc.nextInt() k sc.nextInt() sc.nextInt sc int[] height = new int[n]; height new int[n] n for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        } int ind = 0; int ind = 0; ind 0 ind < n ind n ind++ ind++ ind {
            height[ind] = sc.nextInt();
        } {
            height[ind] = sc.nextInt();
        } height[ind] = sc.nextInt() height[ind] height ind sc.nextInt() sc.nextInt sc int[] dp = new int[n]; dp new int[n] n for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        } int ind = 1; int ind = 1; ind 1 ind < n ind n ind++ ind++ ind {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        } {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        } int i = ind - 1; i ind - 1 ind 1 int minCost = Integer.MAX_VALUE; minCost Integer.MAX_VALUE Integer Integer.MAX_VALUE while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            } i >= 0 && ind - i <= k i >= 0 i 0 ind - i <= k ind - i ind i k {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            } minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]) minCost Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]) Math.min Math minCost Math.abs(height[ind] - height[i]) + dp[i] Math.abs(height[ind] - height[i]) Math.abs Math height[ind] - height[i] height[ind] height ind height[i] height i dp[i] dp i i-- i dp[ind] = minCost dp[ind] dp ind minCost 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 sc = new Scanner(System.in);
        int n, k;
        n = sc.nextInt();
        k = sc.nextInt();
        int[] height = new int[n];

        for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        }

        int[] dp = new int[n];

        for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        }

        System.out.println(dp[n - 1]);
    }
} public class Main {
    
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n, k;
        n = sc.nextInt();
        k = sc.nextInt();
        int[] height = new int[n];

        for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        }

        int[] dp = new int[n];

        for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        }

        System.out.println(dp[n - 1]);
    }
} Main
import java.util.*;
public class Main {
    
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n, k;
        n = sc.nextInt();
        k = sc.nextInt();
        int[] height = new int[n];

        for(int ind = 0; ind < n; ind++) {
            height[ind] = sc.nextInt();
        }

        int[] dp = new int[n];

        for(int ind = 1; ind < n; ind++) {

            int i = ind - 1;
            int minCost = Integer.MAX_VALUE;
            while(i >= 0 && ind - i <= k) {
                minCost = Math.min(minCost, Math.abs(height[ind] - height[i]) + dp[i]);
                i--;
            }
            dp[ind] = minCost;
        }

        System.out.println(dp[n - 1]);
    }
}
