// This template code suggested by KT BYTE Computer Science Academy
//   for use in reading and writing files for USACO problems.

// https://content.ktbyte.com/problem.java

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

public class Main {

    static StreamTokenizer in;

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws Exception {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

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

        int[] heights = new int[n];

        for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        }

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

        for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        }

        dp[0] = 0;
        dp[1] = 0;

        for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        }

        out.println(dp[n]);

        out.close();
    }
}

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

    static StreamTokenizer in;

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws Exception {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

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

        int[] heights = new int[n];

        for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        }

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

        for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        }

        dp[0] = 0;
        dp[1] = 0;

        for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        }

        out.println(dp[n]);

        out.close();
    }
} Main static StreamTokenizer in; in static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    } nextInt {
        in.nextToken();
        return (int) in.nval;
    } in.nextToken() in.nextToken in return (int) in.nval; (int) in.nval in in.nval public static void main(String[] args) throws Exception {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

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

        int[] heights = new int[n];

        for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        }

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

        for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        }

        dp[0] = 0;
        dp[1] = 0;

        for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        }

        out.println(dp[n]);

        out.close();
    } main {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

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

        int[] heights = new int[n];

        for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        }

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

        for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        }

        dp[0] = 0;
        dp[1] = 0;

        for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        }

        out.println(dp[n]);

        out.close();
    } in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))) in new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))) PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); out new PrintWriter(new BufferedOutputStream(System.out)) int n = nextInt(); n nextInt() nextInt int k = nextInt(); k nextInt() nextInt int[] heights = new int[n]; heights new int[n] n for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            heights[i] = nextInt();
        } {
            heights[i] = nextInt();
        } heights[i] = nextInt() heights[i] heights i nextInt() nextInt int[] dp = new int[n + 1]; dp new int[n + 1] n + 1 n 1 for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        } int i = 0; int i = 0; i 0 i <= n i n i++ i++ i {
            dp[i] = 1000000000;
        } {
            dp[i] = 1000000000;
        } dp[i] = 1000000000 dp[i] dp i 1000000000 dp[0] = 0 dp[0] dp 0 0 dp[1] = 0 dp[1] dp 1 0 for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        } int i = 2; int i = 2; i 2 i <= n i n i++ i++ i {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        } {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        } for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            } int j = Math.max(i - k, 1); int j = Math.max(i - k, 1); j Math.max(i - k, 1) Math.max Math i - k i k 1 j < i j i j++ j++ j {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            } {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            } dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1])) dp[i] dp i Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1])) Math.min Math dp[i] dp i dp[j] + Math.abs(heights[i - 1] - heights[j - 1]) dp[j] dp j Math.abs(heights[i - 1] - heights[j - 1]) Math.abs Math heights[i - 1] - heights[j - 1] heights[i - 1] heights i - 1 i 1 heights[j - 1] heights j - 1 j 1 out.println(dp[n]) out.println out dp[n] dp n out.close() out.close out String[] args args public class Main {

    static StreamTokenizer in;

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws Exception {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

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

        int[] heights = new int[n];

        for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        }

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

        for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        }

        dp[0] = 0;
        dp[1] = 0;

        for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        }

        out.println(dp[n]);

        out.close();
    }
} public class Main {

    static StreamTokenizer in;

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws Exception {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

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

        int[] heights = new int[n];

        for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        }

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

        for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        }

        dp[0] = 0;
        dp[1] = 0;

        for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        }

        out.println(dp[n]);

        out.close();
    }
} Main
// This template code suggested by KT BYTE Computer Science Academy
//   for use in reading and writing files for USACO problems.

// https://content.ktbyte.com/problem.java

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

public class Main {

    static StreamTokenizer in;

    static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws Exception {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

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

        int[] heights = new int[n];

        for (int i = 0; i < n; i++) {
            heights[i] = nextInt();
        }

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

        for (int i = 0; i <= n; i++)  {
            dp[i] = 1000000000;
        }

        dp[0] = 0;
        dp[1] = 0;

        for (int i = 2; i <= n; i++) {
            for (int j = Math.max(i - k, 1); j < i; j++) {
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i - 1] - heights[j - 1]));
            }
        }

        out.println(dp[n]);

        out.close();
    }
}


