import javax.swing.text.MutableAttributeSet;
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] cost = new int[n];
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i - j < 0) continue;
                min = Math.min(cost[i-j] + Math.abs(h[i-j] - h[i]) , min);
            }
            cost[i] = min;
        }

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

    }
} import javax.swing.text.MutableAttributeSet; MutableAttributeSet javax.swing.text import java.util.*; util.* java public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] cost = new int[n];
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i - j < 0) continue;
                min = Math.min(cost[i-j] + Math.abs(h[i-j] - h[i]) , min);
            }
            cost[i] = min;
        }

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

    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] cost = new int[n];
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i - j < 0) continue;
                min = Math.min(cost[i-j] + Math.abs(h[i-j] - h[i]) , min);
            }
            cost[i] = min;
        }

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

    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] cost = new int[n];
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i - j < 0) continue;
                min = Math.min(cost[i-j] + Math.abs(h[i-j] - h[i]) , min);
            }
            cost[i] = min;
        }

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

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

        int[] cost = new int[n];
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i - j < 0) continue;
                min = Math.min(cost[i-j] + Math.abs(h[i-j] - h[i]) , min);
            }
            cost[i] = min;
        }

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

    }
} public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] cost = new int[n];
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i - j < 0) continue;
                min = Math.min(cost[i-j] + Math.abs(h[i-j] - h[i]) , min);
            }
            cost[i] = min;
        }

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

    }
} Main
import javax.swing.text.MutableAttributeSet;
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] cost = new int[n];
        for (int i = 1; i < n; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i - j < 0) continue;
                min = Math.min(cost[i-j] + Math.abs(h[i-j] - h[i]) , min);
            }
            cost[i] = min;
        }

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

    }
}
