module Main where

import Data.List
import Data.Maybe
import Control.Monad
import Debug.Trace
import qualified Data.Map.Strict as Map


readLs :: Read a => IO [a]
readLs = fmap read . words <$> getLine

readTuple :: Read a  => IO (a, a)
readTuple = do
    [f, s] <- readLs
    pure (f, s)

solve :: [Int] -> Int -> Int -> Int
solve hs n k = foldl' (\acc i -> min acc $ getHeightDiff i) maxHeight [0..(n-k)] where
    sortedMap = Map.fromAscList . zip [0..] . sort $ hs
    getHeightDiff i = fromMaybe maxHeight $ do
        high <- Map.lookup (i + k - 1) sortedMap
        low <- Map.lookup i sortedMap
        pure $ high - low
    maxHeight = 1000000000


main = do
    (n, k) <- readTuple
    hs <- replicateM n readLn
    print $ solve hs n k
