import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    }

    static class B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        }

    }
}
 import java.io.OutputStream; OutputStream java.io import java.io.IOException; IOException java.io import java.io.InputStream; InputStream java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    }

    static class B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        }

    }
} Main public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out Scanner in = new Scanner(inputStream); in new Scanner(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) B solver = new B(); solver new B() solver.solve(1, in, out) solver.solve solver 1 in out out.close() out.close out String[] args args static class B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        }

    } B public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        } solve {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        } int n = Integer.parseInt(in.next()); n Integer.parseInt(in.next()) Integer.parseInt Integer in.next() in.next in int k = Integer.parseInt(in.next()); k Integer.parseInt(in.next()) Integer.parseInt Integer in.next() in.next in int[] h = new int[n]; h new int[n] n for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
                h[i] = Integer.parseInt(in.next());
            } {
                h[i] = Integer.parseInt(in.next());
            } h[i] = Integer.parseInt(in.next()) h[i] h i Integer.parseInt(in.next()) Integer.parseInt Integer in.next() in.next in int[] dp = new int[n]; dp new int[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 dp[1] = dp[0] + Math.abs(h[1] - h[0]) dp[1] dp 1 dp[0] + Math.abs(h[1] - h[0]) dp[0] dp 0 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++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            } int i = 2; int i = 2; i 2 i < n i n i++ i++ i {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            } {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            } for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                } {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                } if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j])); j <= i j i dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j])) dp[i] dp i Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j])) Math.min Math dp[i] dp i dp[i - j] + Math.abs(h[i] - h[i - j]) dp[i - j] dp i - j 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 out.println(dp[n - 1]) out.println out dp[n - 1] dp n - 1 n 1 int testNumber testNumber Scanner in in PrintWriter out out public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    }

    static class B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        }

    }
} public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    }

    static class B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        }

    }
} Main
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        B solver = new B();
        solver.solve(1, in, out);
        out.close();
    }

    static class B {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            // 入力
            int n = Integer.parseInt(in.next());
            int k = Integer.parseInt(in.next());
            int[] h = new int[n];
            for (int i = 0; i < n; i++) {
                h[i] = Integer.parseInt(in.next());
            }

            int[] dp = new int[n];
            Arrays.fill(dp, Integer.MAX_VALUE);

            dp[0] = 0;
            dp[1] = dp[0] + Math.abs(h[1] - h[0]);
            for (int i = 2; i < n; i++) {
                for (int j = 1; j <= k; j++) {
                    if (j <= i) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i] - h[i - j]));
                }
            }

            out.println(dp[n - 1]);

        }

    }
}

