import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    // 頂点番号，各頂点から出る複数のエッジ
    static List<Edge>[] graph;

    // 最短コスト
    static int[] dist;
    static long[] mindist;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();

        dist = new int[1000000];
        for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        }

        mindist = new long[1000000];
        solve_0(N, K);
        System.out.println(mindist[N - 1]);
    }

    static void solve_0(int N, int K) {
        Arrays.fill(mindist, Long.MAX_VALUE);
        mindist[0] = 0;

        for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        }
    }
}

// エッジに関するクラス
class Edge {
    int to, cost;

    public Edge(int to, int cost) {
        this.to = to;
        this.cost = cost;
    }
}

// 頂点に関するクラス
class Vertex {
    int id;
    long dist;

    public Vertex(int id, long dist) {
        this.id = id;
        this.dist = dist;
    }
} import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.List; List java.util import java.util.PriorityQueue; PriorityQueue java.util import java.util.Scanner; Scanner java.util public class Main {
    // 頂点番号，各頂点から出る複数のエッジ
    static List<Edge>[] graph;

    // 最短コスト
    static int[] dist;
    static long[] mindist;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();

        dist = new int[1000000];
        for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        }

        mindist = new long[1000000];
        solve_0(N, K);
        System.out.println(mindist[N - 1]);
    }

    static void solve_0(int N, int K) {
        Arrays.fill(mindist, Long.MAX_VALUE);
        mindist[0] = 0;

        for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        }
    }
} Main // 頂点番号，各頂点から出る複数のエッジ
    static List<Edge>[] graph; graph // 最短コスト
    static int[] dist; dist static long[] mindist; mindist public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();

        dist = new int[1000000];
        for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        }

        mindist = new long[1000000];
        solve_0(N, K);
        System.out.println(mindist[N - 1]);
    } main {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();

        dist = new int[1000000];
        for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        }

        mindist = new long[1000000];
        solve_0(N, K);
        System.out.println(mindist[N - 1]);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int N = scanner.nextInt(); N scanner.nextInt() scanner.nextInt scanner int K = scanner.nextInt(); K scanner.nextInt() scanner.nextInt scanner dist = new int[1000000] dist new int[1000000] 1000000 for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            dist[i] = scanner.nextInt();
        } {
            dist[i] = scanner.nextInt();
        } dist[i] = scanner.nextInt() dist[i] dist i scanner.nextInt() scanner.nextInt scanner mindist = new long[1000000] mindist new long[1000000] 1000000 solve_0(N, K) solve_0 N K System.out.println(mindist[N - 1]) System.out.println System.out System System.out mindist[N - 1] mindist N - 1 N 1 String[] args args static void solve_0(int N, int K) {
        Arrays.fill(mindist, Long.MAX_VALUE);
        mindist[0] = 0;

        for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        }
    } solve_0 {
        Arrays.fill(mindist, Long.MAX_VALUE);
        mindist[0] = 0;

        for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        }
    } Arrays.fill(mindist, Long.MAX_VALUE) Arrays.fill Arrays mindist Long.MAX_VALUE Long Long.MAX_VALUE mindist[0] = 0 mindist[0] mindist 0 0 for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        } {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        } for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            } int k = 1; int k = 1; k 1 k < K + 1 k K + 1 K 1 k++ k++ k {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            } {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            } long a = mindist[i] + Math.abs(dist[i] - dist[i + k]); a mindist[i] + Math.abs(dist[i] - dist[i + k]) mindist[i] mindist i Math.abs(dist[i] - dist[i + k]) Math.abs Math dist[i] - dist[i + k] dist[i] dist i dist[i + k] dist i + k i k mindist[i + k] = Math.min(a, mindist[i + k]) mindist[i + k] mindist i + k i k Math.min(a, mindist[i + k]) Math.min Math a mindist[i + k] mindist i + k i k int N N int K K class Edge {
    int to, cost;

    public Edge(int to, int cost) {
        this.to = to;
        this.cost = cost;
    }
} Edge int to to cost; cost public Edge(int to, int cost) {
        this.to = to;
        this.cost = cost;
    } Edge {
        this.to = to;
        this.cost = cost;
    } this.to = to this.to this this.to to this.cost = cost this.cost this this.cost cost int to to int cost cost class Vertex {
    int id;
    long dist;

    public Vertex(int id, long dist) {
        this.id = id;
        this.dist = dist;
    }
} Vertex int id; id long dist; dist public Vertex(int id, long dist) {
        this.id = id;
        this.dist = dist;
    } Vertex {
        this.id = id;
        this.dist = dist;
    } this.id = id this.id this this.id id this.dist = dist this.dist this this.dist dist int id id long dist dist public class Main {
    // 頂点番号，各頂点から出る複数のエッジ
    static List<Edge>[] graph;

    // 最短コスト
    static int[] dist;
    static long[] mindist;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();

        dist = new int[1000000];
        for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        }

        mindist = new long[1000000];
        solve_0(N, K);
        System.out.println(mindist[N - 1]);
    }

    static void solve_0(int N, int K) {
        Arrays.fill(mindist, Long.MAX_VALUE);
        mindist[0] = 0;

        for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        }
    }
} public class Main {
    // 頂点番号，各頂点から出る複数のエッジ
    static List<Edge>[] graph;

    // 最短コスト
    static int[] dist;
    static long[] mindist;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();

        dist = new int[1000000];
        for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        }

        mindist = new long[1000000];
        solve_0(N, K);
        System.out.println(mindist[N - 1]);
    }

    static void solve_0(int N, int K) {
        Arrays.fill(mindist, Long.MAX_VALUE);
        mindist[0] = 0;

        for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        }
    }
} Main
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    // 頂点番号，各頂点から出る複数のエッジ
    static List<Edge>[] graph;

    // 最短コスト
    static int[] dist;
    static long[] mindist;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int K = scanner.nextInt();

        dist = new int[1000000];
        for (int i = 0; i < N; i++) {
            dist[i] = scanner.nextInt();
        }

        mindist = new long[1000000];
        solve_0(N, K);
        System.out.println(mindist[N - 1]);
    }

    static void solve_0(int N, int K) {
        Arrays.fill(mindist, Long.MAX_VALUE);
        mindist[0] = 0;

        for (int i = 0; i < N; i++) {
            for (int k = 1; k < K + 1; k++) {
                long a = mindist[i] + Math.abs(dist[i] - dist[i + k]);
                mindist[i + k] = Math.min(a, mindist[i + k]);
            }
        }
    }
}

// エッジに関するクラス
class Edge {
    int to, cost;

    public Edge(int to, int cost) {
        this.to = to;
        this.cost = cost;
    }
}

// 頂点に関するクラス
class Vertex {
    int id;
    long dist;

    public Vertex(int id, long dist) {
        this.id = id;
        this.dist = dist;
    }
}
