import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int INF = (int) (1e9 + 5);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] stones = new int[N];
        for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        }
        int[] cost = new int[N];
        Arrays.fill(cost, INF);
        cost[0] = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        }
        System.out.println(cost[N-1]);
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int INF = (int) (1e9 + 5);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] stones = new int[N];
        for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        }
        int[] cost = new int[N];
        Arrays.fill(cost, INF);
        cost[0] = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        }
        System.out.println(cost[N-1]);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int INF = (int) (1e9 + 5);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] stones = new int[N];
        for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        }
        int[] cost = new int[N];
        Arrays.fill(cost, INF);
        cost[0] = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        }
        System.out.println(cost[N-1]);
    } main {
        Scanner sc = new Scanner(System.in);

        int INF = (int) (1e9 + 5);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] stones = new int[N];
        for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        }
        int[] cost = new int[N];
        Arrays.fill(cost, INF);
        cost[0] = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        }
        System.out.println(cost[N-1]);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int INF = (int) (1e9 + 5); INF (int) (1e9 + 5) 1e9 5 int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int[] stones = new int[N]; stones new int[N] N for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            stones[i] = sc.nextInt();
        } {
            stones[i] = sc.nextInt();
        } stones[i] = sc.nextInt() stones[i] stones i sc.nextInt() sc.nextInt sc int[] cost = new int[N]; cost new int[N] N Arrays.fill(cost, INF) Arrays.fill Arrays cost INF cost[0] = 0 cost[0] cost 0 0 for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        } int i = 0; int i = 0; i 0 i < N i N ++i ++i i {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        } {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        } for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            } int j = i + 1; int j = i + 1; j i + 1 i 1 j <= i + K j i + K i K ++j ++j j {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            } {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            } if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                } j < N j N {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                } cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j])) cost[j] cost j Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j])) Math.min Math cost[j] cost j cost[i] + Math.abs(stones[i] - stones[j]) cost[i] cost i Math.abs(stones[i] - stones[j]) Math.abs Math stones[i] - stones[j] stones[i] stones i stones[j] stones j System.out.println(cost[N-1]) System.out.println System.out System System.out cost[N-1] cost N-1 N 1 String[] args args public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int INF = (int) (1e9 + 5);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] stones = new int[N];
        for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        }
        int[] cost = new int[N];
        Arrays.fill(cost, INF);
        cost[0] = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        }
        System.out.println(cost[N-1]);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int INF = (int) (1e9 + 5);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] stones = new int[N];
        for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        }
        int[] cost = new int[N];
        Arrays.fill(cost, INF);
        cost[0] = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        }
        System.out.println(cost[N-1]);
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int INF = (int) (1e9 + 5);

        int N = sc.nextInt();
        int K = sc.nextInt();
        int[] stones = new int[N];
        for (int i = 0; i < N; i++) {
            stones[i] = sc.nextInt();
        }
        int[] cost = new int[N];
        Arrays.fill(cost, INF);
        cost[0] = 0;
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j <= i + K; ++j) {
                if (j < N) {
                    cost[j] = Math.min(cost[j], cost[i] + Math.abs(stones[i] - stones[j]));
                }
            }
        }
        System.out.println(cost[N-1]);
    }
}
