
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main
{
  static class FastScanner
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    String next() {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    }
    
    int nextInt() {
      return Integer.parseInt(next());
    }
    
    long nextLong() {
      return Long.parseLong(next());
    }
    
  }
  
  public static void main(String[] args) {
    FastScanner fs = new FastScanner();
    int n = fs.nextInt();
    int k = fs.nextInt();
    int[] arr = new int[n];
    for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    }
    int[] dp = new int[n];
    for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    }
    PrintWriter out = new PrintWriter(System.out);
    out.println(dp[n-1]);
    out.close();
  }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.PrintWriter; PrintWriter java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main
{
  static class FastScanner
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    String next() {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    }
    
    int nextInt() {
      return Integer.parseInt(next());
    }
    
    long nextLong() {
      return Long.parseLong(next());
    }
    
  }
  
  public static void main(String[] args) {
    FastScanner fs = new FastScanner();
    int n = fs.nextInt();
    int k = fs.nextInt();
    int[] arr = new int[n];
    for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    }
    int[] dp = new int[n];
    for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    }
    PrintWriter out = new PrintWriter(System.out);
    out.println(dp[n-1]);
    out.close();
  }
} Main public static void main(String[] args) {
    FastScanner fs = new FastScanner();
    int n = fs.nextInt();
    int k = fs.nextInt();
    int[] arr = new int[n];
    for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    }
    int[] dp = new int[n];
    for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    }
    PrintWriter out = new PrintWriter(System.out);
    out.println(dp[n-1]);
    out.close();
  } main {
    FastScanner fs = new FastScanner();
    int n = fs.nextInt();
    int k = fs.nextInt();
    int[] arr = new int[n];
    for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    }
    int[] dp = new int[n];
    for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    }
    PrintWriter out = new PrintWriter(System.out);
    out.println(dp[n-1]);
    out.close();
  } FastScanner fs = new FastScanner(); fs new FastScanner() int n = fs.nextInt(); n fs.nextInt() fs.nextInt fs int k = fs.nextInt(); k fs.nextInt() fs.nextInt fs int[] arr = new int[n]; arr new int[n] n for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    } int i = 0; int i = 0; i 0 i < n i n ++i ++i i {
      arr[i] = fs.nextInt();
    } {
      arr[i] = fs.nextInt();
    } arr[i] = fs.nextInt() arr[i] arr i fs.nextInt() fs.nextInt fs int[] dp = new int[n]; dp new int[n] n for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      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 = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    } {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      } int j = 1; int j = 1; j 1 j <= k j k ++j ++j j {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      } {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      } int val = Integer.MAX_VALUE; val Integer.MAX_VALUE Integer Integer.MAX_VALUE if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        } i-j >= 0 i-j i j 0 {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        } val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]) val dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]) dp[i-j] dp i-j i j (int)Math.abs(arr[i-j]-arr[i]) Math.abs Math arr[i-j]-arr[i] arr[i-j] arr i-j i j arr[i] arr i if(val < min) {
          min = val;
        } val < min val min {
          min = val;
        } min = val min val dp[i] = min dp[i] dp i min PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) out.println(dp[n-1]) out.println out dp[n-1] dp n-1 n 1 out.close() out.close out String[] args args static class FastScanner
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    String next() {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    }
    
    int nextInt() {
      return Integer.parseInt(next());
    }
    
    long nextLong() {
      return Long.parseLong(next());
    }
    
  } FastScanner BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st = new StringTokenizer(""); st new StringTokenizer("") String next() {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    } next {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    } while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      } !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      } try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {} catch (IOException e) {} IOException e {} {
          st = new StringTokenizer(br.readLine());
        } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st 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 public class Main
{
  static class FastScanner
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    String next() {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    }
    
    int nextInt() {
      return Integer.parseInt(next());
    }
    
    long nextLong() {
      return Long.parseLong(next());
    }
    
  }
  
  public static void main(String[] args) {
    FastScanner fs = new FastScanner();
    int n = fs.nextInt();
    int k = fs.nextInt();
    int[] arr = new int[n];
    for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    }
    int[] dp = new int[n];
    for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    }
    PrintWriter out = new PrintWriter(System.out);
    out.println(dp[n-1]);
    out.close();
  }
} public class Main
{
  static class FastScanner
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    String next() {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    }
    
    int nextInt() {
      return Integer.parseInt(next());
    }
    
    long nextLong() {
      return Long.parseLong(next());
    }
    
  }
  
  public static void main(String[] args) {
    FastScanner fs = new FastScanner();
    int n = fs.nextInt();
    int k = fs.nextInt();
    int[] arr = new int[n];
    for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    }
    int[] dp = new int[n];
    for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    }
    PrintWriter out = new PrintWriter(System.out);
    out.println(dp[n-1]);
    out.close();
  }
} Main

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main
{
  static class FastScanner
  {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    String next() {
      while(!st.hasMoreTokens()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {}
      }
      return st.nextToken();
    }
    
    int nextInt() {
      return Integer.parseInt(next());
    }
    
    long nextLong() {
      return Long.parseLong(next());
    }
    
  }
  
  public static void main(String[] args) {
    FastScanner fs = new FastScanner();
    int n = fs.nextInt();
    int k = fs.nextInt();
    int[] arr = new int[n];
    for(int i = 0; i < n; ++i) {
      arr[i] = fs.nextInt();
    }
    int[] dp = new int[n];
    for(int i = 1; i < n; ++i) {
      int min = Integer.MAX_VALUE;
      for(int j = 1; j <= k; ++j) {
        int val = Integer.MAX_VALUE;
        if(i-j >= 0) {
          val = dp[i-j] + (int)Math.abs(arr[i-j]-arr[i]);
        }
        if(val < min) {
          min = val;
        }
        
      }
      dp[i] = min;
    }
    PrintWriter out = new PrintWriter(System.out);
    out.println(dp[n-1]);
    out.close();
  }
}
