import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo =reader.readLine().split(" ");
        int N=Integer.parseInt(lo[0]);
        int K = Integer.parseInt(lo[1]);
        String[] lop = reader.readLine().split(" ");
        int[] arr = new int[N];
        for (int i = 0; i <N ; i++) {
            arr[i] = Integer.parseInt(lop[i]);
        }
        int[] dp = new int[N];
        dp[0] = 0;
        for (int i = 1; i <N ; i++) {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        }
        System.out.println(dp[N-1]);
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo =reader.readLine().split(" ");
        int N=Integer.parseInt(lo[0]);
        int K = Integer.parseInt(lo[1]);
        String[] lop = reader.readLine().split(" ");
        int[] arr = new int[N];
        for (int i = 0; i <N ; i++) {
            arr[i] = Integer.parseInt(lop[i]);
        }
        int[] dp = new int[N];
        dp[0] = 0;
        for (int i = 1; i <N ; i++) {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        }
        System.out.println(dp[N-1]);
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo =reader.readLine().split(" ");
        int N=Integer.parseInt(lo[0]);
        int K = Integer.parseInt(lo[1]);
        String[] lop = reader.readLine().split(" ");
        int[] arr = new int[N];
        for (int i = 0; i <N ; i++) {
            arr[i] = Integer.parseInt(lop[i]);
        }
        int[] dp = new int[N];
        dp[0] = 0;
        for (int i = 1; i <N ; i++) {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        }
        System.out.println(dp[N-1]);
    } main {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo =reader.readLine().split(" ");
        int N=Integer.parseInt(lo[0]);
        int K = Integer.parseInt(lo[1]);
        String[] lop = reader.readLine().split(" ");
        int[] arr = new int[N];
        for (int i = 0; i <N ; i++) {
            arr[i] = Integer.parseInt(lop[i]);
        }
        int[] dp = new int[N];
        dp[0] = 0;
        for (int i = 1; i <N ; i++) {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        }
        System.out.println(dp[N-1]);
    } BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); reader new BufferedReader(new InputStreamReader(System.in)) String[] lo =reader.readLine().split(" "); lo reader.readLine().split(" ") reader.readLine().split reader.readLine() reader.readLine reader " " int N=Integer.parseInt(lo[0]); N Integer.parseInt(lo[0]) Integer.parseInt Integer lo[0] lo 0 int K = Integer.parseInt(lo[1]); K Integer.parseInt(lo[1]) Integer.parseInt Integer lo[1] lo 1 String[] lop = reader.readLine().split(" "); lop reader.readLine().split(" ") reader.readLine().split reader.readLine() reader.readLine reader " " int[] arr = new int[N]; arr new int[N] N for (int i = 0; i <N ; i++) {
            arr[i] = Integer.parseInt(lop[i]);
        } int i = 0; int i = 0; i 0 i <N i N i++ i++ i {
            arr[i] = Integer.parseInt(lop[i]);
        } {
            arr[i] = Integer.parseInt(lop[i]);
        } arr[i] = Integer.parseInt(lop[i]) arr[i] arr i Integer.parseInt(lop[i]) Integer.parseInt Integer lop[i] lop i int[] dp = new int[N]; dp new int[N] N dp[0] = 0 dp[0] dp 0 0 for (int i = 1; i <N ; i++) {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        } int i = 1; int i = 1; i 1 i <N i N i++ i++ i {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        } {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE int j = 1; j 1 while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            } j<=K && i-j>=0 j<=K j K i-j>=0 i-j i j 0 {
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            } min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j])) min Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j])) Math.min Math min dp[i-j]+Math.abs(arr[i]-arr[i-j]) dp[i-j] dp i-j i j Math.abs(arr[i]-arr[i-j]) Math.abs Math arr[i]-arr[i-j] arr[i] arr i arr[i-j] arr i-j i j j++ j dp[i] = min dp[i] dp i min 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] lo =reader.readLine().split(" ");
        int N=Integer.parseInt(lo[0]);
        int K = Integer.parseInt(lo[1]);
        String[] lop = reader.readLine().split(" ");
        int[] arr = new int[N];
        for (int i = 0; i <N ; i++) {
            arr[i] = Integer.parseInt(lop[i]);
        }
        int[] dp = new int[N];
        dp[0] = 0;
        for (int i = 1; i <N ; i++) {
            int min = Integer.MAX_VALUE;
            int j = 1;
            while(j<=K && i-j>=0){
                min = Math.min(min,dp[i-j]+Math.abs(arr[i]-arr[i-j]));
                j++;
            }
            dp[i] = min;
        }
        System.out.println(dp[N-1]);
    }
}
