import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;

public class Main {
    private static final class FastInput {
        private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer st;

        private int nextInt() throws Exception {
            return Integer.parseInt(nextWord());
        }

        private long nextLong() throws Exception {
            return Long.parseLong(nextWord());
        }

        private String nextWord() throws Exception {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        }

        private long[] lineToIntArr(int len) throws Exception {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        }
    }

    public static void main(String[] args) throws Exception {
        FastInput in = new FastInput();
        int n = in.nextInt();
        int k = in.nextInt();
        long[] a = in.lineToIntArr(n);
        long[] dp = new long[n+1];
        for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        }
        dp[0] = 0;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(dp[n - 1]));
        bw.flush();
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.BufferedWriter; BufferedWriter java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.OutputStreamWriter; OutputStreamWriter java.io import java.util.*; util.* java public class Main {
    private static final class FastInput {
        private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer st;

        private int nextInt() throws Exception {
            return Integer.parseInt(nextWord());
        }

        private long nextLong() throws Exception {
            return Long.parseLong(nextWord());
        }

        private String nextWord() throws Exception {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        }

        private long[] lineToIntArr(int len) throws Exception {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        }
    }

    public static void main(String[] args) throws Exception {
        FastInput in = new FastInput();
        int n = in.nextInt();
        int k = in.nextInt();
        long[] a = in.lineToIntArr(n);
        long[] dp = new long[n+1];
        for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        }
        dp[0] = 0;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(dp[n - 1]));
        bw.flush();
    }
} Main public static void main(String[] args) throws Exception {
        FastInput in = new FastInput();
        int n = in.nextInt();
        int k = in.nextInt();
        long[] a = in.lineToIntArr(n);
        long[] dp = new long[n+1];
        for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        }
        dp[0] = 0;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(dp[n - 1]));
        bw.flush();
    } main {
        FastInput in = new FastInput();
        int n = in.nextInt();
        int k = in.nextInt();
        long[] a = in.lineToIntArr(n);
        long[] dp = new long[n+1];
        for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        }
        dp[0] = 0;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(dp[n - 1]));
        bw.flush();
    } FastInput in = new FastInput(); in new FastInput() int n = in.nextInt(); n in.nextInt() in.nextInt in int k = in.nextInt(); k in.nextInt() in.nextInt in long[] a = in.lineToIntArr(n); a in.lineToIntArr(n) in.lineToIntArr in n long[] dp = new long[n+1]; dp new long[n+1] n+1 n 1 for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        } int i = 0; int i = 0; i 0 i <= n i n ++i ++i i {
            dp[i] = 1000000007;
        } {
            dp[i] = 1000000007;
        } dp[i] = 1000000007 dp[i] dp i 1000000007 dp[0] = 0 dp[0] dp 0 0 for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        } int i = 0; int i = 0; i 0 i < n i n ++i ++i i {
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        } {
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        } for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            } int j = 1; int j = 1; j 1 j <= k j k ++j ++j j {
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            } {
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            } if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j])); i + j < n i + j i j n dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j])) dp[i+j] dp i+j i j Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j])) Math.min Math dp[i+j] dp i+j i j dp[i] + Math.abs(a[i] - a[i+j]) dp[i] dp i 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 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); bw new BufferedWriter(new OutputStreamWriter(System.out)) bw.write(String.valueOf(dp[n - 1])) bw.write bw String.valueOf(dp[n - 1]) String.valueOf String dp[n - 1] dp n - 1 n 1 bw.flush() bw.flush bw String[] args args private static final class FastInput {
        private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer st;

        private int nextInt() throws Exception {
            return Integer.parseInt(nextWord());
        }

        private long nextLong() throws Exception {
            return Long.parseLong(nextWord());
        }

        private String nextWord() throws Exception {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        }

        private long[] lineToIntArr(int len) throws Exception {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        }
    } FastInput private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) private StringTokenizer st; st private int nextInt() throws Exception {
            return Integer.parseInt(nextWord());
        } nextInt {
            return Integer.parseInt(nextWord());
        } return Integer.parseInt(nextWord()); Integer.parseInt(nextWord()) Integer.parseInt Integer nextWord() nextWord private long nextLong() throws Exception {
            return Long.parseLong(nextWord());
        } nextLong {
            return Long.parseLong(nextWord());
        } return Long.parseLong(nextWord()); Long.parseLong(nextWord()) Long.parseLong Long nextWord() nextWord private String nextWord() throws Exception {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        } nextWord {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        } if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " "); st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st st = new StringTokenizer(br.readLine(), " ") st new StringTokenizer(br.readLine(), " ") return st.nextToken(); st.nextToken() st.nextToken st private long[] lineToIntArr(int len) throws Exception {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        } lineToIntArr {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        } long[] arr = new long[len]; arr new long[len] len for (int i = 0; i < arr.length; i++) arr[i] = nextLong(); int i = 0; int i = 0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i arr[i] = nextLong(); arr[i] = nextLong() arr[i] arr i nextLong() nextLong return arr; arr int len len public class Main {
    private static final class FastInput {
        private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer st;

        private int nextInt() throws Exception {
            return Integer.parseInt(nextWord());
        }

        private long nextLong() throws Exception {
            return Long.parseLong(nextWord());
        }

        private String nextWord() throws Exception {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        }

        private long[] lineToIntArr(int len) throws Exception {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        }
    }

    public static void main(String[] args) throws Exception {
        FastInput in = new FastInput();
        int n = in.nextInt();
        int k = in.nextInt();
        long[] a = in.lineToIntArr(n);
        long[] dp = new long[n+1];
        for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        }
        dp[0] = 0;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(dp[n - 1]));
        bw.flush();
    }
} public class Main {
    private static final class FastInput {
        private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer st;

        private int nextInt() throws Exception {
            return Integer.parseInt(nextWord());
        }

        private long nextLong() throws Exception {
            return Long.parseLong(nextWord());
        }

        private String nextWord() throws Exception {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        }

        private long[] lineToIntArr(int len) throws Exception {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        }
    }

    public static void main(String[] args) throws Exception {
        FastInput in = new FastInput();
        int n = in.nextInt();
        int k = in.nextInt();
        long[] a = in.lineToIntArr(n);
        long[] dp = new long[n+1];
        for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        }
        dp[0] = 0;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(dp[n - 1]));
        bw.flush();
    }
} Main
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;

public class Main {
    private static final class FastInput {
        private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        private StringTokenizer st;

        private int nextInt() throws Exception {
            return Integer.parseInt(nextWord());
        }

        private long nextLong() throws Exception {
            return Long.parseLong(nextWord());
        }

        private String nextWord() throws Exception {
            if (st == null || !st.hasMoreElements()) st = new StringTokenizer(br.readLine(), " ");
            return st.nextToken();
        }

        private long[] lineToIntArr(int len) throws Exception {
            long[] arr = new long[len];
            for (int i = 0; i < arr.length; i++) arr[i] = nextLong();
            return arr;
        }
    }

    public static void main(String[] args) throws Exception {
        FastInput in = new FastInput();
        int n = in.nextInt();
        int k = in.nextInt();
        long[] a = in.lineToIntArr(n);
        long[] dp = new long[n+1];
        for(int i = 0; i <= n; ++i){
            dp[i] = 1000000007;
        }
        dp[0] = 0;
        for(int i = 0; i < n; ++i){
            for(int j = 1; j <= k; ++j){
                if(i + j < n)
                    dp[i+j] = Math.min(dp[i+j], dp[i] + Math.abs(a[i] - a[i+j]));
            }
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(String.valueOf(dp[n - 1]));
        bw.flush();
    }
}