import Data.List

main = do
    [n, k] <- map read . words <$> getLine
    hs <- map read <$> (sequence $ replicate n getLine)
    let hs' = sort hs
    let mms = zip hs' (drop (k - 1) hs')
    let diffs = map (\(x, y) -> y - x) mms
    print $ minimum diffs