[<AutoOpen>]
module Program

open System

let read f = stdin.ReadLine() |> f
let reads f = stdin.ReadLine().Split() |> Array.toList |> List.map f

let f s xs =
    let d = List.head xs - List.last xs
    if d < s then d else s

[<EntryPoint>]
let main _ =
    let [n;k] = reads int
    let hs = List.map (fun _ -> read int) [1..n]
    let xss = List.sortDescending hs |> List.windowed k
    List.fold f Int32.MaxValue xss
    |> printfn "%i"
    0