require 'pp'
require 'set'

n, k = gets.chomp!.split(" ").map(&:to_i)
h_n = []
n.times do |i|
    h_n[i] = gets.to_i
end
h_n.sort!

min = 10 ** 20

h_n.each_cons(k) do |x|
    diff = x[-1] - x[0]
    if diff < min
        min = diff
    end
end

puts min
