import java.util.*;

public class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for(int i = 0; i < n; i++) h[i] = scan.nextInt();
        int[] dp = new int[n];
        dp[1] = Math.abs(h[1] - h[0]);
        for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        }
        System.out.println(dp[n - 1]);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
} import java.util.*; util.* java public class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for(int i = 0; i < n; i++) h[i] = scan.nextInt();
        int[] dp = new int[n];
        dp[1] = Math.abs(h[1] - h[0]);
        for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        }
        System.out.println(dp[n - 1]);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
} Main void solve(){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for(int i = 0; i < n; i++) h[i] = scan.nextInt();
        int[] dp = new int[n];
        dp[1] = Math.abs(h[1] - h[0]);
        for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        }
        System.out.println(dp[n - 1]);
    } solve {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for(int i = 0; i < n; i++) h[i] = scan.nextInt();
        int[] dp = new int[n];
        dp[1] = Math.abs(h[1] - h[0]);
        for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        }
        System.out.println(dp[n - 1]);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int[] h = new int[n]; h new int[n] n for(int i = 0; i < n; i++) h[i] = scan.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i h[i] = scan.nextInt(); h[i] = scan.nextInt() h[i] h i scan.nextInt() scan.nextInt scan int[] dp = new int[n]; dp new int[n] n dp[1] = Math.abs(h[1] - h[0]) dp[1] dp 1 Math.abs(h[1] - h[0]) Math.abs Math h[1] - h[0] h[1] h 1 h[0] h 0 for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        } int i = 2; int i = 2; i 2 i < n i n i++ i++ i {
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        } {
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            } int j = i - 1; int j = i - 1; j i - 1 i 1 j >= Math.max(0, i - k) j Math.max(0, i - k) Math.max Math 0 i - k i k j-- j-- j {
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            } {
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            } min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]) min Math.min(min, Math.abs(h[i] - h[j]) + dp[j]) Math.min Math min Math.abs(h[i] - h[j]) + dp[j] Math.abs(h[i] - h[j]) Math.abs Math h[i] - h[j] h[i] h i h[j] h j dp[j] dp 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 public static void main(String[] args){
        new Main().solve();
    } main {
        new Main().solve();
    } new Main().solve() new Main().solve new Main() String[] args args public class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for(int i = 0; i < n; i++) h[i] = scan.nextInt();
        int[] dp = new int[n];
        dp[1] = Math.abs(h[1] - h[0]);
        for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        }
        System.out.println(dp[n - 1]);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
} public class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for(int i = 0; i < n; i++) h[i] = scan.nextInt();
        int[] dp = new int[n];
        dp[1] = Math.abs(h[1] - h[0]);
        for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        }
        System.out.println(dp[n - 1]);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
} Main
import java.util.*;

public class Main{
    void solve(){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for(int i = 0; i < n; i++) h[i] = scan.nextInt();
        int[] dp = new int[n];
        dp[1] = Math.abs(h[1] - h[0]);
        for(int i = 2; i < n; i++){
            int min = Integer.MAX_VALUE;
            for(int j = i - 1; j >= Math.max(0, i - k); j--){
                min = Math.min(min, Math.abs(h[i] - h[j]) + dp[j]);
            }
            dp[i] = min;
        }
        System.out.println(dp[n - 1]);
    }
    
    public static void main(String[] args){
        new Main().solve();
    }
}
