n, k = gets.split.map(&:to_i) # 整数半角区切り,非配列
x = [];  n.times {x <<  gets.chomp.to_i}   # 整数半角区切り,複数行
x.sort!
min = 10**10
for i in 0..(n-k)
	tmin =  x[i+k-1] - x[i]
	min = tmin < min ? tmin:min
end
p min 