

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) h[i] = in.nextInt();
        System.out.println(minCost(N, K, h));
    }

    public static int minCost(int N, int K, int[] h) {
        int[] dp = new int[N];
        for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        return dp[N - 1];
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) h[i] = in.nextInt();
        System.out.println(minCost(N, K, h));
    }

    public static int minCost(int N, int K, int[] h) {
        int[] dp = new int[N];
        for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        return dp[N - 1];
    }
} Main public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) h[i] = in.nextInt();
        System.out.println(minCost(N, K, h));
    } main {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) h[i] = in.nextInt();
        System.out.println(minCost(N, K, h));
    } Scanner in = new Scanner(System.in); in new Scanner(System.in) int N = in.nextInt(); N in.nextInt() in.nextInt in int K = in.nextInt(); K in.nextInt() in.nextInt in int[] h = new int[N]; h new int[N] N for (int i = 0; i < N; i++) h[i] = in.nextInt(); int i = 0; int i = 0; i 0 i < N i N i++ i++ i h[i] = in.nextInt(); h[i] = in.nextInt() h[i] h i in.nextInt() in.nextInt in System.out.println(minCost(N, K, h)) System.out.println System.out System System.out minCost(N, K, h) minCost N K h String[] args args public static int minCost(int N, int K, int[] h) {
        int[] dp = new int[N];
        for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        return dp[N - 1];
    } minCost {
        int[] dp = new int[N];
        for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        return dp[N - 1];
    } int[] dp = new int[N]; dp new int[N] N for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[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 = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        } {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            } int j = i - 1; int j = i - 1; j i - 1 i 1 j >= 0 && j >= i - K j >= 0 j 0 j >= i - K j i - K i K j-- j-- j {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            } {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            } min = Math.min(min, dp[j] + Math.abs(h[i] - h[j])) min Math.min(min, dp[j] + Math.abs(h[i] - h[j])) Math.min Math min dp[j] + Math.abs(h[i] - h[j]) dp[j] dp j Math.abs(h[i] - h[j]) Math.abs Math h[i] - h[j] h[i] h i h[j] h j dp[i] = min dp[i] dp i min return dp[N - 1]; dp[N - 1] dp N - 1 N 1 int N N int K K int[] h h public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) h[i] = in.nextInt();
        System.out.println(minCost(N, K, h));
    }

    public static int minCost(int N, int K, int[] h) {
        int[] dp = new int[N];
        for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        return dp[N - 1];
    }
} public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) h[i] = in.nextInt();
        System.out.println(minCost(N, K, h));
    }

    public static int minCost(int N, int K, int[] h) {
        int[] dp = new int[N];
        for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        return dp[N - 1];
    }
} Main


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int K = in.nextInt();
        int[] h = new int[N];
        for (int i = 0; i < N; i++) h[i] = in.nextInt();
        System.out.println(minCost(N, K, h));
    }

    public static int minCost(int N, int K, int[] h) {
        int[] dp = new int[N];
        for (int i = 1; i < N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = i - 1; j >= 0 && j >= i - K; j--) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        return dp[N - 1];
    }
}
