open System


[<EntryPoint>]
let main argv =
    let N,K =stdin.ReadLine().Split()|>fun c -> int c.[0],int c.[1]
    let h=[|for i in [0..N-1] do yield (stdin.ReadLine()|>int)|]|>Array.sort
    [for i in [0..N-K] do yield ((h.[i+K-1] - h.[i])|>abs)]
    |>List.min
    |>stdout.WriteLine
    0
