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

public class Main {

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    public static void main(String[] args) {
        FastReader scanner = new FastReader();
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        int[] DP = new int[N];

        if (K > N) {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } else {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        }
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    public static void main(String[] args) {
        FastReader scanner = new FastReader();
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        int[] DP = new int[N];

        if (K > N) {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } else {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        }
    }
} Main public static void main(String[] args) {
        FastReader scanner = new FastReader();
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        int[] DP = new int[N];

        if (K > N) {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } else {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        }
    } main {
        FastReader scanner = new FastReader();
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        int[] DP = new int[N];

        if (K > N) {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } else {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        }
    } FastReader scanner = new FastReader(); scanner new FastReader() int N = scanner.nextInt(); N scanner.nextInt() scanner.nextInt scanner int K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner int[] A = new int[N]; A new int[N] N for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            A[i] = scanner.nextInt();
        } {
            A[i] = scanner.nextInt();
        } A[i] = scanner.nextInt() A[i] A i scanner.nextInt() scanner.nextInt scanner int[] DP = new int[N]; DP new int[N] N if (K > N) {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } else {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        } K > N K N {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            } {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            } DP[i] = Math.abs(A[i] - A[0]) DP[i] DP i Math.abs(A[i] - A[0]) Math.abs Math A[i] - A[0] A[i] A i A[0] A 0 min = Math.min(DP[i], min) min Math.min(DP[i], min) Math.min Math DP[i] DP i min System.out.println(min) System.out.println System.out System System.out min {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        } for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            } int i = 1; int i = 1; i 1 i < K i K i++ i++ i {
                DP[i] = Math.abs(A[i] - A[0]);
            } {
                DP[i] = Math.abs(A[i] - A[0]);
            } DP[i] = Math.abs(A[i] - A[0]) DP[i] DP i Math.abs(A[i] - A[0]) Math.abs Math A[i] - A[0] A[i] A i A[0] A 0 for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            } int i = K; int i = K; i K i < N i N i++ i++ i {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            } {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                } int j = 1; int j = 1; j 1 j <= K j K j++ j++ j {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                } {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                } min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j])) min Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j])) Math.min Math min DP[i - j] + Math.abs(A[i] - A[i - j]) DP[i - j] DP i - j i j Math.abs(A[i] - A[i - j]) Math.abs Math A[i] - A[i - j] A[i] A i A[i - j] A i - j i j DP[i] = min DP[i] DP i min System.out.println(DP[N - 1]) System.out.println System.out System System.out DP[N - 1] DP N - 1 N 1 String[] args args static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
    } FastReader BufferedReader br; br StringTokenizer st; st public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        } FastReader {
            br = new BufferedReader(new InputStreamReader(System.in));
        } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } next {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    st = new StringTokenizer(br.readLine());
                } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public class Main {

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    public static void main(String[] args) {
        FastReader scanner = new FastReader();
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        int[] DP = new int[N];

        if (K > N) {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } else {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        }
    }
} public class Main {

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    public static void main(String[] args) {
        FastReader scanner = new FastReader();
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        int[] DP = new int[N];

        if (K > N) {
            int min = Integer.MAX_VALUE;
            for (int i = 1; i < N; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
                min = Math.min(DP[i], min);
            }
            System.out.println(min);
        } else {
            for (int i = 1; i < K; i++) {
                DP[i] = Math.abs(A[i] - A[0]);
            }
            for (int i = K; i < N; i++) {
                int min = Integer.MAX_VALUE;
                for (int j = 1; j <= K; j++) {
                    min = Math.min(min, DP[i - j] + Math.abs(A[i] - A[i - j]));
                }
                DP[i] = min;
            }
            System.out.println(DP[N - 1]);
        }
    }
} Main
import java.io.*;
import java.util.*;

public class Main {

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

    public static void main(String[] args) {
        FastReader scanner = new FastReader();
        int N = scanner.nextInt();
        int K = scanner.nextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = scanner.nextInt();
        }

        int[] DP = new int[N];

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