import Control.Applicative
import Control.Monad
import Data.List
import Data.Array

main = do
  [n, k] <- map (read::String -> Int) . words <$> getLine
  arr <- listArray (1, n) . sort <$> forM [1..n] (\i -> (read::String -> Int) <$> getLine)
  print $ foldl (\acc i -> min acc (arr ! (i + k - 1) - arr ! i)) (maxBound::Int) [1..(n - k + 1)]