import Control.Monad
import Data.List

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

-- fold or zipwithで書きたい