import java.io.*;
import java.util.*;
import java.math.*;

public class Main {


    public static void solve() {
        int n = in.nextInt();
        int k = in.nextInt();

        int[] stones = new int[n];
        for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt();

        int[] store = new int[n];

        for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        }

        out.println(store[n-1]);
    }


    public static InputReader in = new InputReader();
    public static PrintWriter out;

    public static void main(String[] args) {
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }


    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        /**
         * When you call next(), that entire line will be skipped.
         * No flushing buffers.
         * Doesn't work when you want to scan the remaining line.
         *
         * @return entire line
         */
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        }
    }
} import java.io.*; io.* java import java.util.*; util.* java import java.math.*; math.* java public class Main {


    public static void solve() {
        int n = in.nextInt();
        int k = in.nextInt();

        int[] stones = new int[n];
        for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt();

        int[] store = new int[n];

        for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        }

        out.println(store[n-1]);
    }


    public static InputReader in = new InputReader();
    public static PrintWriter out;

    public static void main(String[] args) {
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }


    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        /**
         * When you call next(), that entire line will be skipped.
         * No flushing buffers.
         * Doesn't work when you want to scan the remaining line.
         *
         * @return entire line
         */
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        }
    }
} Main public static InputReader in = new InputReader(); in new InputReader() public static PrintWriter out; out public static void solve() {
        int n = in.nextInt();
        int k = in.nextInt();

        int[] stones = new int[n];
        for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt();

        int[] store = new int[n];

        for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        }

        out.println(store[n-1]);
    } solve {
        int n = in.nextInt();
        int k = in.nextInt();

        int[] stones = new int[n];
        for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt();

        int[] store = new int[n];

        for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        }

        out.println(store[n-1]);
    } int n = in.nextInt(); n in.nextInt() in.nextInt in int k = in.nextInt(); k in.nextInt() in.nextInt in int[] stones = new int[n]; stones new int[n] n for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt(); int i = 0; int i = 0; i 0 i < n i n i += 1 i += 1 i 1 stones[i] = in.nextInt(); stones[i] = in.nextInt() stones[i] stones i in.nextInt() in.nextInt in int[] store = new int[n]; store new int[n] n for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        } int i = 1; int i = 1; i 1 i < n i n i += 1 i += 1 i 1 {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        } {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        } store[i] = Integer.MAX_VALUE store[i] store i Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            } int j = 1; int j = 1; j 1 j <= k j k j += 1 j += 1 j 1 {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            } {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            } if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j])); i-j >= 0 i-j i j 0 store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j])) store[i] store i Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j])) Math.min Math store[i] store i store[i-j] + Math.abs(stones[i] - stones[i-j]) store[i-j] store i-j i j Math.abs(stones[i] - stones[i-j]) Math.abs Math stones[i] - stones[i-j] stones[i] stones i stones[i-j] stones i-j i j out.println(store[n-1]) out.println out store[n-1] store n-1 n 1 public static void main(String[] args) {
        out = new PrintWriter(System.out);
        solve();
        out.close();
    } main {
        out = new PrintWriter(System.out);
        solve();
        out.close();
    } out = new PrintWriter(System.out) out new PrintWriter(System.out) solve() solve out.close() out.close out String[] args args static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        /**
         * When you call next(), that entire line will be skipped.
         * No flushing buffers.
         * Doesn't work when you want to scan the remaining line.
         *
         * @return entire line
         */
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        }
    } InputReader public BufferedReader reader; reader public StringTokenizer tokenizer; tokenizer public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        } InputReader {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        } reader = new BufferedReader(new InputStreamReader(System.in)) reader new BufferedReader(new InputStreamReader(System.in)) tokenizer = null tokenizer null public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        } next {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        } while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } tokenizer == null || !tokenizer.hasMoreTokens() tokenizer == null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } IOException e {
                    throw new RuntimeException(e);
                } throw new RuntimeException(e); new RuntimeException(e) {
                    tokenizer = new StringTokenizer(reader.readLine());
                } tokenizer = new StringTokenizer(reader.readLine()) tokenizer new StringTokenizer(reader.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer public int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public double nextDouble() {
            return Double.parseDouble(next());
        } nextDouble {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next public long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next /**
         * When you call next(), that entire line will be skipped.
         * No flushing buffers.
         * Doesn't work when you want to scan the remaining line.
         *
         * @return entire line
         */
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        } nextLine {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        } String str = ""; str "" try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } IOException e {
                throw new RuntimeException(e);
            } throw new RuntimeException(e); new RuntimeException(e) {
                str = reader.readLine();
                tokenizer = null;
            } str = reader.readLine() str reader.readLine() reader.readLine reader tokenizer = null tokenizer null return str; str public class Main {


    public static void solve() {
        int n = in.nextInt();
        int k = in.nextInt();

        int[] stones = new int[n];
        for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt();

        int[] store = new int[n];

        for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        }

        out.println(store[n-1]);
    }


    public static InputReader in = new InputReader();
    public static PrintWriter out;

    public static void main(String[] args) {
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }


    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        /**
         * When you call next(), that entire line will be skipped.
         * No flushing buffers.
         * Doesn't work when you want to scan the remaining line.
         *
         * @return entire line
         */
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        }
    }
} public class Main {


    public static void solve() {
        int n = in.nextInt();
        int k = in.nextInt();

        int[] stones = new int[n];
        for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt();

        int[] store = new int[n];

        for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        }

        out.println(store[n-1]);
    }


    public static InputReader in = new InputReader();
    public static PrintWriter out;

    public static void main(String[] args) {
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }


    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        /**
         * When you call next(), that entire line will be skipped.
         * No flushing buffers.
         * Doesn't work when you want to scan the remaining line.
         *
         * @return entire line
         */
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        }
    }
} Main
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {


    public static void solve() {
        int n = in.nextInt();
        int k = in.nextInt();

        int[] stones = new int[n];
        for(int i = 0; i < n; i += 1)
            stones[i] = in.nextInt();

        int[] store = new int[n];

        for(int i = 1; i < n; i += 1) {
            store[i] = Integer.MAX_VALUE;

            for(int j = 1; j <= k; j += 1) {
                if(i-j >= 0)
                    store[i] = Math.min(store[i], store[i-j] + Math.abs(stones[i] - stones[i-j]));
            }
        }

        out.println(store[n-1]);
    }


    public static InputReader in = new InputReader();
    public static PrintWriter out;

    public static void main(String[] args) {
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }


    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        /**
         * When you call next(), that entire line will be skipped.
         * No flushing buffers.
         * Doesn't work when you want to scan the remaining line.
         *
         * @return entire line
         */
        public String nextLine() {
            String str = "";
            try {
                str = reader.readLine();
                tokenizer = null;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return str;
        }
    }
}