import java.util.*;
import java.io.*;
class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[] arr = new int[N];
        
        st = new StringTokenizer(br.readLine());

        for (int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(arr[1]-arr[0]);

        for (int i=2; i<N; i++) {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        }
        System.out.println(dp[N-1]);
    }
} import java.util.*; util.* java import java.io.*; io.* java class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[] arr = new int[N];
        
        st = new StringTokenizer(br.readLine());

        for (int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(arr[1]-arr[0]);

        for (int i=2; i<N; i++) {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        }
        System.out.println(dp[N-1]);
    }
} Main public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[] arr = new int[N];
        
        st = new StringTokenizer(br.readLine());

        for (int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(arr[1]-arr[0]);

        for (int i=2; i<N; i++) {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        }
        System.out.println(dp[N-1]);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[] arr = new int[N];
        
        st = new StringTokenizer(br.readLine());

        for (int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(arr[1]-arr[0]);

        for (int i=2; i<N; i++) {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        }
        System.out.println(dp[N-1]);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int N = Integer.parseInt(st.nextToken()); N Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int K = Integer.parseInt(st.nextToken()); K Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int[] arr = new int[N]; arr new int[N] N st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) for (int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            arr[i] = Integer.parseInt(st.nextToken());
        } {
            arr[i] = Integer.parseInt(st.nextToken());
        } arr[i] = Integer.parseInt(st.nextToken()) arr[i] arr i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int[] dp = new int[N]; dp new int[N] N Arrays.fill(dp, Integer.MAX_VALUE) Arrays.fill Arrays dp Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0]=0 dp[0] dp 0 0 dp[1]=Math.abs(arr[1]-arr[0]) dp[1] dp 1 Math.abs(arr[1]-arr[0]) Math.abs Math arr[1]-arr[0] arr[1] arr 1 arr[0] arr 0 for (int i=2; i<N; i++) {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        } int i=2; int i=2; i 2 i<N i N i++ i++ i {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        } {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        } for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            } int j=i-1; int j=i-1; j i-1 i 1 j>=0 && i-j<=K j>=0 j 0 i-j<=K i-j i j K j-- j-- j {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            } {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            } dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j])) dp[i] dp i Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j])) Math.min Math dp[i] dp i dp[j]+Math.abs(arr[i]-arr[j]) dp[j] dp j Math.abs(arr[i]-arr[j]) Math.abs Math arr[i]-arr[j] arr[i] arr i arr[j] arr j 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.util.*;
import java.io.*;
class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[] arr = new int[N];
        
        st = new StringTokenizer(br.readLine());

        for (int i=0; i<N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int[] dp = new int[N];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(arr[1]-arr[0]);

        for (int i=2; i<N; i++) {
            for (int j=i-1; j>=0 && i-j<=K; j--) {
                dp[i] = Math.min(dp[i],dp[j]+Math.abs(arr[i]-arr[j]));
            }
        }
        System.out.println(dp[N-1]);
    }
}
