n,k = gets.split.map &:to_i; h=[]
n.times do |i|
	h[i] = gets.to_i
end
h = h.sort; arr = Array.new(n-k+1).map{Array.new(k)}; ans = Array.new(arr.length)
arr.length.times do |i|
	0.upto(k-1) do |j|
		arr[i][j] = h[i+j]
	end
	ans[i] = arr[i][k-1] - arr[i][0]
end
puts ans.min