import java.util.Scanner;

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();
        long[] memo = new long[n];
        for(int i = 0;i < n;i++) memo[i] = -1;
        System.out.println(dp(n, k, h, n - 1, memo));
    }

    private static long dp(int n, int k, int[] h, int i, long[] memo) {
        if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        }
    }

} import java.util.Scanner; Scanner 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();
        long[] memo = new long[n];
        for(int i = 0;i < n;i++) memo[i] = -1;
        System.out.println(dp(n, k, h, n - 1, memo));
    }

    private static long dp(int n, int k, int[] h, int i, long[] memo) {
        if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        }
    }

} 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();
        long[] memo = new long[n];
        for(int i = 0;i < n;i++) memo[i] = -1;
        System.out.println(dp(n, k, h, n - 1, memo));
    } 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();
        long[] memo = new long[n];
        for(int i = 0;i < n;i++) memo[i] = -1;
        System.out.println(dp(n, k, h, n - 1, memo));
    } 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] h i sc.nextInt() sc.nextInt sc long[] memo = new long[n]; memo new long[n] n for(int i = 0;i < n;i++) memo[i] = -1; int i = 0; int i = 0; i 0 i < n i n i++ i++ i memo[i] = -1; memo[i] = -1 memo[i] memo i -1 1 System.out.println(dp(n, k, h, n - 1, memo)) System.out.println System.out System System.out dp(n, k, h, n - 1, memo) dp n k h n - 1 n 1 memo String[] args args private static long dp(int n, int k, int[] h, int i, long[] memo) {
        if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        }
    } dp {
        if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        }
    } if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        } i == 0 i 0 return 0; 0 if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        } memo[i] != -1 memo[i] memo i -1 1 return memo[i]; memo[i] memo i {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        } long minCost = Long.MAX_VALUE; minCost Long.MAX_VALUE Long Long.MAX_VALUE for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            } int j = k; int j = k; j k j > 0 j 0 j-- j-- j {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            } {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            } int tmpIdx = i - j; tmpIdx i - j i j long tmpCost = Long.MAX_VALUE; tmpCost Long.MAX_VALUE Long Long.MAX_VALUE if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]); tmpIdx >= 0 tmpIdx 0 tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]) tmpCost dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]) dp(n, k, h, tmpIdx, memo) dp n k h tmpIdx memo Math.abs(h[i] - h[tmpIdx]) Math.abs Math h[i] - h[tmpIdx] h[i] h i h[tmpIdx] h tmpIdx if(minCost > tmpCost) minCost = tmpCost; minCost > tmpCost minCost tmpCost minCost = tmpCost minCost tmpCost memo[i] = minCost memo[i] memo i minCost return minCost; minCost int n n int k k int[] h h int i i long[] memo memo 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();
        long[] memo = new long[n];
        for(int i = 0;i < n;i++) memo[i] = -1;
        System.out.println(dp(n, k, h, n - 1, memo));
    }

    private static long dp(int n, int k, int[] h, int i, long[] memo) {
        if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        }
    }

} 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();
        long[] memo = new long[n];
        for(int i = 0;i < n;i++) memo[i] = -1;
        System.out.println(dp(n, k, h, n - 1, memo));
    }

    private static long dp(int n, int k, int[] h, int i, long[] memo) {
        if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        }
    }

} Main
import java.util.Scanner;

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();
        long[] memo = new long[n];
        for(int i = 0;i < n;i++) memo[i] = -1;
        System.out.println(dp(n, k, h, n - 1, memo));
    }

    private static long dp(int n, int k, int[] h, int i, long[] memo) {
        if(i == 0) return 0;
        else if(memo[i] != -1) return memo[i];
        else {
            long minCost = Long.MAX_VALUE;
            for(int j = k;j > 0;j--) {
                int tmpIdx = i - j;
                long tmpCost = Long.MAX_VALUE;
                if(tmpIdx >= 0) tmpCost = dp(n, k, h, tmpIdx, memo) + Math.abs(h[i] - h[tmpIdx]);
                if(minCost > tmpCost) minCost = tmpCost;
            }

            memo[i] = minCost;
            return minCost;
        }
    }

}
