import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

class Main {
    // static {
    //     try {
    //         System.setOut(new PrintStream(new File("output.txt")));
    //         new File("input.txt").createNewFile();
    //         System.setIn(new FileInputStream(new File("input.txt")));
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     }
    // }
    static FastReader fastReader = new FastReader();
    static BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer stringBuffer = new StringBuffer();

    public static void main(String[] args) throws Exception {
        try {
            // int testCases = fastReader.nextInt();
            int testCases=1;
            while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            }
            bufferedWriter.write(stringBuffer.toString());
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    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();
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

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

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.BufferedWriter; BufferedWriter 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.util.StringTokenizer; StringTokenizer java.util class Main {
    // static {
    //     try {
    //         System.setOut(new PrintStream(new File("output.txt")));
    //         new File("input.txt").createNewFile();
    //         System.setIn(new FileInputStream(new File("input.txt")));
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     }
    // }
    static FastReader fastReader = new FastReader();
    static BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer stringBuffer = new StringBuffer();

    public static void main(String[] args) throws Exception {
        try {
            // int testCases = fastReader.nextInt();
            int testCases=1;
            while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            }
            bufferedWriter.write(stringBuffer.toString());
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    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();
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

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

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }
} Main // static {
    //     try {
    //         System.setOut(new PrintStream(new File("output.txt")));
    //         new File("input.txt").createNewFile();
    //         System.setIn(new FileInputStream(new File("input.txt")));
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     }
    // }
    static FastReader fastReader = new FastReader(); fastReader new FastReader() static BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); bufferedWriter new BufferedWriter(new OutputStreamWriter(System.out)) static StringBuffer stringBuffer = new StringBuffer(); stringBuffer new StringBuffer() public static void main(String[] args) throws Exception {
        try {
            // int testCases = fastReader.nextInt();
            int testCases=1;
            while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            }
            bufferedWriter.write(stringBuffer.toString());
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    } main {
        try {
            // int testCases = fastReader.nextInt();
            int testCases=1;
            while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            }
            bufferedWriter.write(stringBuffer.toString());
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    } try {
            // int testCases = fastReader.nextInt();
            int testCases=1;
            while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            }
            bufferedWriter.write(stringBuffer.toString());
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        } Exception e {
            e.printStackTrace();
            return;
        } e.printStackTrace() e.printStackTrace e return; {
            // int testCases = fastReader.nextInt();
            int testCases=1;
            while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            }
            bufferedWriter.write(stringBuffer.toString());
            bufferedWriter.flush();
        } int testCases=1; testCases 1 while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            } testCases-- > 0 testCases-- testCases 0 {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            } int n=fastReader.nextInt(); n fastReader.nextInt() fastReader.nextInt fastReader int k=fastReader.nextInt(); k fastReader.nextInt() fastReader.nextInt fastReader int arr[]=new int[n]; arr new int[n] n for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                } int i=0; int i=0; i 0 i<n i n i++ i++ i {
                    arr[i]=fastReader.nextInt();
                } {
                    arr[i]=fastReader.nextInt();
                } arr[i]=fastReader.nextInt() arr[i] arr i fastReader.nextInt() fastReader.nextInt fastReader int dp[]=new int[n]; dp new int[n] n dp[0]=0 dp[0] dp 0 0 for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                } int i=1; int i=1; i 1 i<n i n i++ i++ i {
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                } {
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                } int min=Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    } int j=i-k; int j=i-k; j i-k i k j<i j i j++ j++ j {
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    } {
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    } if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j])); j>=0 j 0 min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j])) min Math.min(min,dp[j]+Math.abs(arr[i]-arr[j])) Math.min Math min dp[j]+Math.abs(arr[i]-arr[j]) dp[j] dp j Math.abs(arr[i]-arr[j]) Math.abs Math arr[i]-arr[j] arr[i] arr i arr[j] arr 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 bufferedWriter.write(stringBuffer.toString()) bufferedWriter.write bufferedWriter stringBuffer.toString() stringBuffer.toString stringBuffer bufferedWriter.flush() bufferedWriter.flush bufferedWriter 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();
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

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

        double nextDouble() {
            return Double.parseDouble(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 String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        } nextLine {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        } String str = ""; str "" try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } IOException e {
                e.printStackTrace();
            } e.printStackTrace() e.printStackTrace e {
                str = br.readLine();
            } str = br.readLine() str br.readLine() br.readLine br return str; str int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble() {
            return Double.parseDouble(next());
        } nextDouble {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

class Main {
    // static {
    //     try {
    //         System.setOut(new PrintStream(new File("output.txt")));
    //         new File("input.txt").createNewFile();
    //         System.setIn(new FileInputStream(new File("input.txt")));
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //     }
    // }
    static FastReader fastReader = new FastReader();
    static BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuffer stringBuffer = new StringBuffer();

    public static void main(String[] args) throws Exception {
        try {
            // int testCases = fastReader.nextInt();
            int testCases=1;
            while (testCases-- > 0) {
                int n=fastReader.nextInt();
                int k=fastReader.nextInt();
                int arr[]=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=fastReader.nextInt();
                }
                int dp[]=new int[n];
                dp[0]=0;
                for(int i=1;i<n;i++){
                    int min=Integer.MAX_VALUE;
                    for(int j=i-k;j<i;j++){
                        if(j>=0)
                        min=Math.min(min,dp[j]+Math.abs(arr[i]-arr[j]));
                    }
                    dp[i]=min;
                }
                System.out.println(dp[n-1]);
            }
            bufferedWriter.write(stringBuffer.toString());
            bufferedWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    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();
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

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

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