
import Data.Array (Array, listArray, (!))
import Data.List (sort)

main = do
  [n,k] <- map read . words <$> getLine
  hs <- listArray (1,n) . sort . map read . lines <$> getContents
  print $ minimum [(hs!j) - (hs!i) | i <- [1..n-k+1], let j=i+k-1]
