import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        int[] dp = new int[N];

        for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for(int i = 1; i < N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        }
        System.out.println(dp[N-1]);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        int[] dp = new int[N];

        for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for(int i = 1; i < N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        }
        System.out.println(dp[N-1]);
    }
} Main public static void main(String[] args) {
        new Main().run();
    } main {
        new Main().run();
    } new Main().run() new Main().run new Main() String[] args args private void run() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        int[] dp = new int[N];

        for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for(int i = 1; i < N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        }
        System.out.println(dp[N-1]);
    } run {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        int[] dp = new int[N];

        for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for(int i = 1; i < N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        }
        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 int[] dp = new int[N]; dp new int[N] N for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        } int i = 0 ; int i = 0 ; i 0 i < N i N i++ i++ i {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        } {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        } h[i] = scanner.nextInt() h[i] h i scanner.nextInt() scanner.nextInt scanner dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0] = 0 dp[0] dp 0 0 for(int i = 1; i < N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        } {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        } for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            } {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            } if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]); i-j >= 0 i-j i j 0 dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]) dp[i] dp i Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]) Math.min Math dp[i] dp i Math.abs(h[i] - h[i-j]) + dp[i-j] Math.abs(h[i] - h[i-j]) Math.abs Math h[i] - h[i-j] h[i] h i h[i-j] h i-j i j dp[i-j] dp i-j i 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) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        int[] dp = new int[N];

        for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for(int i = 1; i < N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        }
        System.out.println(dp[N-1]);
    }
} public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        int[] dp = new int[N];

        for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for(int i = 1; i < N; i++) {
            for(int j = 1; j <= K; j++) {
                if(i-j >= 0) dp[i] = Math.min(dp[i], Math.abs(h[i] - h[i-j]) + dp[i-j]);
            }
        }
        System.out.println(dp[N-1]);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] h = new int[N];
        int[] dp = new int[N];

        for(int i = 0 ; i < N; i++) {
            h[i] = scanner.nextInt();
            dp[i] = Integer.MAX_VALUE;
        }

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