n, k = gets.split.map &:to_i
h = []

n.times{ 
	h << gets.to_i
}

h = h.sort

min = Float::INFINITY

(0...h.size - k + 1).each{ |i|
	diff = h[i + k - 1] - h[i]
	if diff < min then
		min = diff
	end
}

puts min
