n, k = gets.chomp.split(' ').map(&:to_i)
h = []

(1..n).each do
  h.push(gets.chomp.to_i)
end

h.sort!

ans = h[k-1] - h[0]
(1..h.size-k).each do |i|
  diff = h[i+k-1] - h[i]
  ans = diff if ans > diff
  break if ans == 0
end

puts ans
