
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String args[]) throws IOException {
        process();
    }
    public static void process() throws IOException{
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(in.readLine());
        int n=Integer.parseInt(st.nextToken());
        int k=Integer.parseInt(st.nextToken());
        int arr[]=new int[n];
        int dp[]=new int[n];
        st=new StringTokenizer(in.readLine()," ");
        for(int i=0;i<n;i++)
            arr[i]=Integer.parseInt(st.nextToken());
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++) {
            int jump=0;
            for (int j = i - 1; j >= 0 && jump < k; jump++, j--){
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(arr[i] - arr[j]));
            }
        }
        System.out.println(dp[n-1]);
    }
}
