n,k=gets.split.map(&:to_i)
highs=[]
n.times do
	highs << gets.to_i
end
highs.sort!
difference=10**9
i=0
while i < n-k+1 do
	difference = highs[i+k-1] - highs[i] if difference > highs[i+k-1] - highs[i]
	i+=1
end
puts difference