let input = readLine()!.characters.split(" ").map{Int(String($0))!}
let (N, K) = (input[0], input[1])
let h = (0..<N).map{_ in Int(readLine()!)!}.sort()
print((0...N-K).map{h[$0+K-1]-h[$0]}.minElement()!)