jump :: Int -> [Int] -> Int
jump k [] = 0
jump k l = minimum $ js k k
  where
    js k 0 = []
    js k n | (length l) > k = ((jump k $ drop n l) + (abs $ (head l) - (head (drop n l)))) : (js k $ n-1)
         | otherwise = ((jump k $ drop n l) + (abs $ (head l) - (last l))) : (js k $ n-1)
main :: IO ()
main = do
  [n,k] <- map read.words <$> getLine
  l <- map read.words <$> getLine
  putStrLn $ show $ jump k l