import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int n;
    int k;
    int[] h;
    long dp[];

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        dp = new long[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n; i++) {
            dp(i);            
        }
        System.out.println(dp[n-1]);
    }
    
    private void dp(int cur) {
        int i=0;
        if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        }
        for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        }
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int n;
    int k;
    int[] h;
    long dp[];

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        dp = new long[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n; i++) {
            dp(i);            
        }
        System.out.println(dp[n-1]);
    }
    
    private void dp(int cur) {
        int i=0;
        if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        }
        for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        }
    }
} Main // 変数一覧
    int n; n int k; k int[] h; h long dp[]; dp public static void main(String[] args) {
        Main main = new Main();
        main.run();
    } main {
        Main main = new Main();
        main.run();
    } Main main = new Main(); main new Main() main.run() main.run main String[] args args public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    } run {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) n = sc.nextInt() n sc.nextInt() sc.nextInt sc k = sc.nextInt() k sc.nextInt() sc.nextInt sc 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] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc sc.close() sc.close sc solve() solve private void solve() {
        dp = new long[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n; i++) {
            dp(i);            
        }
        System.out.println(dp[n-1]);
    } solve {
        dp = new long[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n; i++) {
            dp(i);            
        }
        System.out.println(dp[n-1]);
    } dp = new long[n] dp new long[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 for (int i = 1; i < n; i++) {
            dp(i);            
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            dp(i);            
        } {
            dp(i);            
        } dp(i) dp i System.out.println(dp[n-1]) System.out.println System.out System System.out dp[n-1] dp n-1 n 1 private void dp(int cur) {
        int i=0;
        if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        }
        for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        }
    } dp {
        int i=0;
        if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        }
        for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        }
    } int i=0; i 0 if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        } cur <= k cur k {
            i = 0;
        } i = 0 i 0 {
            i = cur - k;
        } i = cur - k i cur - k cur k for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        } ; i < cur i cur i++ i++ i {
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        } {
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        } dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]) dp[cur] dp cur Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]) Math.min Math dp[i] + Math.abs(h[i] - h[cur]) dp[i] dp i Math.abs(h[i] - h[cur]) Math.abs Math h[i] - h[cur] h[i] h i h[cur] h cur dp[cur] dp cur int cur cur public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int n;
    int k;
    int[] h;
    long dp[];

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        dp = new long[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n; i++) {
            dp(i);            
        }
        System.out.println(dp[n-1]);
    }
    
    private void dp(int cur) {
        int i=0;
        if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        }
        for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        }
    }
} public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int n;
    int k;
    int[] h;
    long dp[];

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        dp = new long[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n; i++) {
            dp(i);            
        }
        System.out.println(dp[n-1]);
    }
    
    private void dp(int cur) {
        int i=0;
        if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        }
        for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        }
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.run();
    }
    
    // 変数一覧
    int n;
    int k;
    int[] h;
    long dp[];

    public void run() {
        // 入力情報取得
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }
        sc.close();

        // 解答処理
        solve();
    }
    
    private void solve() {
        dp = new long[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i < n; i++) {
            dp(i);            
        }
        System.out.println(dp[n-1]);
    }
    
    private void dp(int cur) {
        int i=0;
        if (cur <= k) {
            i = 0;
        } else {
            i = cur - k;
        }
        for(; i < cur; i++){
            dp[cur] = Math.min(dp[i] + Math.abs(h[i] - h[cur]), dp[cur]);
        }
    }
}