import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n + k];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] dp = new int[n + k];
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        }

        System.out.println(dp[n - 1]);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n + k];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] dp = new int[n + k];
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        }

        System.out.println(dp[n - 1]);
    }
} Main public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n + k];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] dp = new int[n + k];
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        }

        System.out.println(dp[n - 1]);
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n + k];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] dp = new int[n + k];
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        }

        System.out.println(dp[n - 1]);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int[] h = new int[n + k]; h new int[n + k] n + k n k for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            h[i] = sc.nextInt();
        } {
            h[i] = sc.nextInt();
        } h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc int[] dp = new int[n + k]; dp new int[n + k] n + k n k for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            dp[i] = Integer.MAX_VALUE;
        } {
            dp[i] = Integer.MAX_VALUE;
        } dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0] = 0 dp[0] dp 0 0 for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        } {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        } for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            } {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            } int a = dp[i] + Math.abs(h[i] - h[i + j]); a dp[i] + Math.abs(h[i] - h[i + j]) dp[i] dp i Math.abs(h[i] - h[i + j]) Math.abs Math h[i] - h[i + j] h[i] h i h[i + j] h i + j i j if (dp[i + j] > a) {
                    dp[i + j] = a;
                } dp[i + j] > a dp[i + j] dp i + j i j a {
                    dp[i + j] = a;
                } dp[i + j] = a dp[i + j] dp i + j i j a System.out.println(dp[n - 1]) System.out.println System.out System System.out dp[n - 1] dp n - 1 n 1 String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n + k];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] dp = new int[n + k];
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        }

        System.out.println(dp[n - 1]);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n + k];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] dp = new int[n + k];
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        }

        System.out.println(dp[n - 1]);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] h = new int[n + k];
        for (int i = 0; i < n; i++) {
            h[i] = sc.nextInt();
        }

        int[] dp = new int[n + k];
        for (int i = 0; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j <= k; j++) {
                int a = dp[i] + Math.abs(h[i] - h[i + j]);
                if (dp[i + j] > a) {
                    dp[i + j] = a;
                }
            }
        }

        System.out.println(dp[n - 1]);
    }
}
