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

main = do
    [n,k] <- map read . words <$> getLine :: IO [Int]
    hs <- fmap sort $ replicateM n readLn :: IO [Int]
    let hs1 = take k hs
    let (mx1, mn1) = (maximum hs1, minimum hs1)
    let hs2 = take k $ reverse hs
    let (mx2, mn2) = (maximum hs2, minimum hs2)
    print $ min (mx1 - mn1) (mx2 - mn2)