import Data.List

readInt :: String -> Int
readInt = read

main = do
    s <- map readInt.words<$>getContents
    print $ solve s

solve :: [Int] -> Int
solve (n:k:ts) = minimum $ dvs(n-k)
 where
  xs = sort ts
  dv m = abs $ xs!!m - xs!!(m+k-1)
  dvs 0 = [dv 0]
  dvs m = [dv m] ++ dvs (m-1)