import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    static PrintWriter out;
    static CF_Reader in;

    public static void main(String[] args) throws IOException {
        out = new PrintWriter(new OutputStreamWriter(System.out));
        in = new CF_Reader();

        int n = in.intNext(), k = in.intNext();
        int[] arr = in.nextIntArray(n);

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(arr[1] - arr[0]);

        for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        }
        out.println(dp[n-1]);

        out.close();
    }


    static class CF_Reader {
        BufferedReader br;
        StringTokenizer st;

        public CF_Reader() throws IOException {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        }

        long longNext() throws IOException {
            return Long.parseLong(next());
        }

        int intNext() throws IOException {
            return Integer.parseInt(next());
        }

        double doubleNext() throws IOException {
            return Double.parseDouble(next());
        }

        char charNext() throws IOException {
            return next().charAt(0);
        }

        public int[] nextIntArray(final int n) throws IOException {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        }

        public long[] nextLongArray(final int n) throws IOException {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        }

        String line() throws IOException {
            return br.readLine().trim();
        }
    }
}

 import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.OutputStreamWriter; OutputStreamWriter java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Arrays; Arrays java.util import java.util.StringTokenizer; StringTokenizer java.util public class Main {
    static PrintWriter out;
    static CF_Reader in;

    public static void main(String[] args) throws IOException {
        out = new PrintWriter(new OutputStreamWriter(System.out));
        in = new CF_Reader();

        int n = in.intNext(), k = in.intNext();
        int[] arr = in.nextIntArray(n);

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(arr[1] - arr[0]);

        for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        }
        out.println(dp[n-1]);

        out.close();
    }


    static class CF_Reader {
        BufferedReader br;
        StringTokenizer st;

        public CF_Reader() throws IOException {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        }

        long longNext() throws IOException {
            return Long.parseLong(next());
        }

        int intNext() throws IOException {
            return Integer.parseInt(next());
        }

        double doubleNext() throws IOException {
            return Double.parseDouble(next());
        }

        char charNext() throws IOException {
            return next().charAt(0);
        }

        public int[] nextIntArray(final int n) throws IOException {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        }

        public long[] nextLongArray(final int n) throws IOException {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        }

        String line() throws IOException {
            return br.readLine().trim();
        }
    }
} Main static PrintWriter out; out static CF_Reader in; in public static void main(String[] args) throws IOException {
        out = new PrintWriter(new OutputStreamWriter(System.out));
        in = new CF_Reader();

        int n = in.intNext(), k = in.intNext();
        int[] arr = in.nextIntArray(n);

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(arr[1] - arr[0]);

        for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        }
        out.println(dp[n-1]);

        out.close();
    } main {
        out = new PrintWriter(new OutputStreamWriter(System.out));
        in = new CF_Reader();

        int n = in.intNext(), k = in.intNext();
        int[] arr = in.nextIntArray(n);

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(arr[1] - arr[0]);

        for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        }
        out.println(dp[n-1]);

        out.close();
    } out = new PrintWriter(new OutputStreamWriter(System.out)) out new PrintWriter(new OutputStreamWriter(System.out)) in = new CF_Reader() in new CF_Reader() int n = in.intNext() n in.intNext() in.intNext in k = in.intNext(); k in.intNext() in.intNext in int[] arr = in.nextIntArray(n); arr in.nextIntArray(n) in.nextIntArray in n int[] dp = new int[n]; dp new int[n] n dp[0] = 0 dp[0] dp 0 0 dp[1] = Math.abs(arr[1] - arr[0]) dp[1] dp 1 Math.abs(arr[1] - arr[0]) Math.abs Math arr[1] - arr[0] arr[1] arr 1 arr[0] arr 0 for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        } int i = 2; int i = 2; i 2 i < n i n i++ i++ i {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        } {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        } int minIdx = Math.max(i - k, 0); minIdx Math.max(i - k, 0) Math.max Math i - k i k 0 int minVal = Integer.MAX_VALUE; minVal Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            } int j = minIdx; int j = minIdx; j minIdx j < i j i j++ j++ j {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            } {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            } minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i]))) minVal Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i]))) Math.min Math minVal (dp[j] + Math.abs(arr[j] - arr[i])) dp[j] dp j Math.abs(arr[j] - arr[i]) Math.abs Math arr[j] - arr[i] arr[j] arr j arr[i] arr i dp[i] = minVal dp[i] dp i minVal out.println(dp[n-1]) out.println out dp[n-1] dp n-1 n 1 out.close() out.close out String[] args args static class CF_Reader {
        BufferedReader br;
        StringTokenizer st;

        public CF_Reader() throws IOException {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        }

        long longNext() throws IOException {
            return Long.parseLong(next());
        }

        int intNext() throws IOException {
            return Integer.parseInt(next());
        }

        double doubleNext() throws IOException {
            return Double.parseDouble(next());
        }

        char charNext() throws IOException {
            return next().charAt(0);
        }

        public int[] nextIntArray(final int n) throws IOException {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        }

        public long[] nextLongArray(final int n) throws IOException {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        }

        String line() throws IOException {
            return br.readLine().trim();
        }
    } CF_Reader BufferedReader br; br StringTokenizer st; st public CF_Reader() throws IOException {
            br = new BufferedReader(new InputStreamReader(System.in));
        } CF_Reader {
            br = new BufferedReader(new InputStreamReader(System.in));
        } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        } next {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        } while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine().trim()) st new StringTokenizer(br.readLine().trim()) return st.nextToken(); st.nextToken() st.nextToken st long longNext() throws IOException {
            return Long.parseLong(next());
        } longNext {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next int intNext() throws IOException {
            return Integer.parseInt(next());
        } intNext {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next double doubleNext() throws IOException {
            return Double.parseDouble(next());
        } doubleNext {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next char charNext() throws IOException {
            return next().charAt(0);
        } charNext {
            return next().charAt(0);
        } return next().charAt(0); next().charAt(0) next().charAt next() next 0 public int[] nextIntArray(final int n) throws IOException {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        } nextIntArray {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        } final int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++)
                a[i] = intNext(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = intNext(); a[i] = intNext() a[i] a i intNext() intNext return a; a final int n n public long[] nextLongArray(final int n) throws IOException {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        } nextLongArray {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        } final long[] a = new long[n]; a new long[n] n for (int i = 0; i < n; i++)
                a[i] = longNext(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = longNext(); a[i] = longNext() a[i] a i longNext() longNext return a; a final int n n String line() throws IOException {
            return br.readLine().trim();
        } line {
            return br.readLine().trim();
        } return br.readLine().trim(); br.readLine().trim() br.readLine().trim br.readLine() br.readLine br public class Main {
    static PrintWriter out;
    static CF_Reader in;

    public static void main(String[] args) throws IOException {
        out = new PrintWriter(new OutputStreamWriter(System.out));
        in = new CF_Reader();

        int n = in.intNext(), k = in.intNext();
        int[] arr = in.nextIntArray(n);

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(arr[1] - arr[0]);

        for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        }
        out.println(dp[n-1]);

        out.close();
    }


    static class CF_Reader {
        BufferedReader br;
        StringTokenizer st;

        public CF_Reader() throws IOException {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        }

        long longNext() throws IOException {
            return Long.parseLong(next());
        }

        int intNext() throws IOException {
            return Integer.parseInt(next());
        }

        double doubleNext() throws IOException {
            return Double.parseDouble(next());
        }

        char charNext() throws IOException {
            return next().charAt(0);
        }

        public int[] nextIntArray(final int n) throws IOException {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        }

        public long[] nextLongArray(final int n) throws IOException {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        }

        String line() throws IOException {
            return br.readLine().trim();
        }
    }
} public class Main {
    static PrintWriter out;
    static CF_Reader in;

    public static void main(String[] args) throws IOException {
        out = new PrintWriter(new OutputStreamWriter(System.out));
        in = new CF_Reader();

        int n = in.intNext(), k = in.intNext();
        int[] arr = in.nextIntArray(n);

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(arr[1] - arr[0]);

        for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        }
        out.println(dp[n-1]);

        out.close();
    }


    static class CF_Reader {
        BufferedReader br;
        StringTokenizer st;

        public CF_Reader() throws IOException {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        }

        long longNext() throws IOException {
            return Long.parseLong(next());
        }

        int intNext() throws IOException {
            return Integer.parseInt(next());
        }

        double doubleNext() throws IOException {
            return Double.parseDouble(next());
        }

        char charNext() throws IOException {
            return next().charAt(0);
        }

        public int[] nextIntArray(final int n) throws IOException {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        }

        public long[] nextLongArray(final int n) throws IOException {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        }

        String line() throws IOException {
            return br.readLine().trim();
        }
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    static PrintWriter out;
    static CF_Reader in;

    public static void main(String[] args) throws IOException {
        out = new PrintWriter(new OutputStreamWriter(System.out));
        in = new CF_Reader();

        int n = in.intNext(), k = in.intNext();
        int[] arr = in.nextIntArray(n);

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(arr[1] - arr[0]);

        for (int i = 2; i < n; i++) {
            int minIdx = Math.max(i - k, 0);
            int minVal = Integer.MAX_VALUE;
            for (int j = minIdx; j < i; j++) {
                minVal = Math.min(minVal, (dp[j] + Math.abs(arr[j] - arr[i])));
            }
            dp[i] = minVal;
        }
        out.println(dp[n-1]);

        out.close();
    }


    static class CF_Reader {
        BufferedReader br;
        StringTokenizer st;

        public CF_Reader() throws IOException {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine().trim());
            return st.nextToken();
        }

        long longNext() throws IOException {
            return Long.parseLong(next());
        }

        int intNext() throws IOException {
            return Integer.parseInt(next());
        }

        double doubleNext() throws IOException {
            return Double.parseDouble(next());
        }

        char charNext() throws IOException {
            return next().charAt(0);
        }

        public int[] nextIntArray(final int n) throws IOException {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = intNext();
            return a;
        }

        public long[] nextLongArray(final int n) throws IOException {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = longNext();
            return a;
        }

        String line() throws IOException {
            return br.readLine().trim();
        }
    }
}


