import java.util.Scanner;

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

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