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

public class Main {

    public static void solve(InputReader in, PrintWriter out) throws Exception {

        int n = in.nextInt();
        int k = in.nextInt();


        int[] arr = new int[n+1];

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

        int[] dp = new int[n+1];

        dp[0]=0;
        dp[1]=0;
        dp[2]= Math.abs(arr[2]-arr[1]);


        for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        }
        out.println(dp[n]);

    }

    /////////////////////////////////////////////////////////


    private final static long hash(long x, long y) {
        x += offset;
        y += offset;
        return x << 32 | y;
    }

    private final static int offset = (int) 3e5;

    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solve(in, out);
        out.close();
    }

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

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        private String nextToken() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        }

        public String nextLine() throws IOException {
            return reader.readLine();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {

    public static void solve(InputReader in, PrintWriter out) throws Exception {

        int n = in.nextInt();
        int k = in.nextInt();


        int[] arr = new int[n+1];

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

        int[] dp = new int[n+1];

        dp[0]=0;
        dp[1]=0;
        dp[2]= Math.abs(arr[2]-arr[1]);


        for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        }
        out.println(dp[n]);

    }

    /////////////////////////////////////////////////////////


    private final static long hash(long x, long y) {
        x += offset;
        y += offset;
        return x << 32 | y;
    }

    private final static int offset = (int) 3e5;

    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solve(in, out);
        out.close();
    }

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

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        private String nextToken() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        }

        public String nextLine() throws IOException {
            return reader.readLine();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

    }
} Main private final static int offset = (int) 3e5; offset (int) 3e5 public static void solve(InputReader in, PrintWriter out) throws Exception {

        int n = in.nextInt();
        int k = in.nextInt();


        int[] arr = new int[n+1];

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

        int[] dp = new int[n+1];

        dp[0]=0;
        dp[1]=0;
        dp[2]= Math.abs(arr[2]-arr[1]);


        for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        }
        out.println(dp[n]);

    } solve {

        int n = in.nextInt();
        int k = in.nextInt();


        int[] arr = new int[n+1];

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

        int[] dp = new int[n+1];

        dp[0]=0;
        dp[1]=0;
        dp[2]= Math.abs(arr[2]-arr[1]);


        for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        }
        out.println(dp[n]);

    } int n = in.nextInt(); n in.nextInt() in.nextInt in int k = in.nextInt(); k in.nextInt() in.nextInt in int[] arr = new int[n+1]; arr new int[n+1] n+1 n 1 for(int i=1;i<=n;i++)
            arr[i]=in.nextInt(); int i=1; int i=1; i 1 i<=n i n i++ i++ i arr[i]=in.nextInt(); arr[i]=in.nextInt() arr[i] arr i in.nextInt() in.nextInt in int[] dp = new int[n+1]; dp new int[n+1] n+1 n 1 dp[0]=0 dp[0] dp 0 0 dp[1]=0 dp[1] dp 1 0 dp[2]= Math.abs(arr[2]-arr[1]) dp[2] dp 2 Math.abs(arr[2]-arr[1]) Math.abs Math arr[2]-arr[1] arr[2] arr 2 arr[1] arr 1 for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        } int i=3; int i=3; i 3 i<=n i n i++ i++ i {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        } {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        } int j=i; j i int c=1; c 1 dp[i]=Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            } j-c>=1 && c<=k j-c>=1 j-c j c 1 c<=k c k {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            } dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]) dp[i] dp i Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]) Math.min Math dp[i] dp i Math.abs(arr[i]-arr[j-c])+dp[j-c] Math.abs(arr[i]-arr[j-c]) Math.abs Math arr[i]-arr[j-c] arr[i] arr i arr[j-c] arr j-c j c dp[j-c] dp j-c j c c++ c out.println(dp[n]) out.println out dp[n] dp n InputReader in in PrintWriter out out private final static long hash(long x, long y) {
        x += offset;
        y += offset;
        return x << 32 | y;
    } hash {
        x += offset;
        y += offset;
        return x << 32 | y;
    } x += offset x offset y += offset y offset return x << 32 | y; x << 32 | y x << 32 x 32 y long x x long y y public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solve(in, out);
        out.close();
    } main {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solve(in, out);
        out.close();
    } InputStream inputStream = System.in; inputStream System.in System System.in OutputStream outputStream = System.out; outputStream System.out System System.out InputReader in = new InputReader(inputStream); in new InputReader(inputStream) PrintWriter out = new PrintWriter(outputStream); out new PrintWriter(outputStream) solve(in, out) solve in out out.close() out.close out String[] args args static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        private String nextToken() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        }

        public String nextLine() throws IOException {
            return reader.readLine();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

    } InputReader private BufferedReader reader; reader private StringTokenizer tokenizer; tokenizer public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        } InputReader {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        } reader = new BufferedReader(new InputStreamReader(stream), 32768) reader new BufferedReader(new InputStreamReader(stream), 32768) tokenizer = null tokenizer null InputStream stream stream private String nextToken() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        } nextToken {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        } while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine()); tokenizer == null || !tokenizer.hasMoreTokens() tokenizer == null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer tokenizer = new StringTokenizer(reader.readLine()) tokenizer new StringTokenizer(reader.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer public String nextLine() throws IOException {
            return reader.readLine();
        } nextLine {
            return reader.readLine();
        } return reader.readLine(); reader.readLine() reader.readLine reader public int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        } nextInt {
            return Integer.parseInt(nextToken());
        } return Integer.parseInt(nextToken()); Integer.parseInt(nextToken()) Integer.parseInt Integer nextToken() nextToken public long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        } nextLong {
            return Long.parseLong(nextToken());
        } return Long.parseLong(nextToken()); Long.parseLong(nextToken()) Long.parseLong Long nextToken() nextToken public double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        } nextDouble {
            return Double.parseDouble(nextToken());
        } return Double.parseDouble(nextToken()); Double.parseDouble(nextToken()) Double.parseDouble Double nextToken() nextToken public class Main {

    public static void solve(InputReader in, PrintWriter out) throws Exception {

        int n = in.nextInt();
        int k = in.nextInt();


        int[] arr = new int[n+1];

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

        int[] dp = new int[n+1];

        dp[0]=0;
        dp[1]=0;
        dp[2]= Math.abs(arr[2]-arr[1]);


        for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        }
        out.println(dp[n]);

    }

    /////////////////////////////////////////////////////////


    private final static long hash(long x, long y) {
        x += offset;
        y += offset;
        return x << 32 | y;
    }

    private final static int offset = (int) 3e5;

    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solve(in, out);
        out.close();
    }

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

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        private String nextToken() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        }

        public String nextLine() throws IOException {
            return reader.readLine();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

    }
} public class Main {

    public static void solve(InputReader in, PrintWriter out) throws Exception {

        int n = in.nextInt();
        int k = in.nextInt();


        int[] arr = new int[n+1];

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

        int[] dp = new int[n+1];

        dp[0]=0;
        dp[1]=0;
        dp[2]= Math.abs(arr[2]-arr[1]);


        for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        }
        out.println(dp[n]);

    }

    /////////////////////////////////////////////////////////


    private final static long hash(long x, long y) {
        x += offset;
        y += offset;
        return x << 32 | y;
    }

    private final static int offset = (int) 3e5;

    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solve(in, out);
        out.close();
    }

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

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        private String nextToken() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        }

        public String nextLine() throws IOException {
            return reader.readLine();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

    }
} Main

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

public class Main {

    public static void solve(InputReader in, PrintWriter out) throws Exception {

        int n = in.nextInt();
        int k = in.nextInt();


        int[] arr = new int[n+1];

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

        int[] dp = new int[n+1];

        dp[0]=0;
        dp[1]=0;
        dp[2]= Math.abs(arr[2]-arr[1]);


        for(int i=3;i<=n;i++)
        {
            int j=i;
            int c=1;
            dp[i]=Integer.MAX_VALUE;
            while(j-c>=1 && c<=k)
            {
                dp[i]= Math.min(dp[i],Math.abs(arr[i]-arr[j-c])+dp[j-c]);
                c++;
            }

        }
        out.println(dp[n]);

    }

    /////////////////////////////////////////////////////////


    private final static long hash(long x, long y) {
        x += offset;
        y += offset;
        return x << 32 | y;
    }

    private final static int offset = (int) 3e5;

    public static void main(String[] args) throws Exception {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        solve(in, out);
        out.close();
    }

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

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        private String nextToken() throws IOException {
            while (tokenizer == null || !tokenizer.hasMoreTokens())
                tokenizer = new StringTokenizer(reader.readLine());
            return tokenizer.nextToken();
        }

        public String nextLine() throws IOException {
            return reader.readLine();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(nextToken());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(nextToken());
        }

    }
}
