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

main = do
  [n, k] <- map read . words <$> getLine
  h <- replicateM n readLn
  let a = sort h
  print $ minimum [a!!(i + k - 1) - a!!i | i <- [0..n-k]]