n, k = STDIN.gets.split(' ').map(&:to_i)

trees = n.times.map do
  STDIN.gets.to_i
end

trees.sort!

# p trees

diff = 10 ** 9

(n - k + 1).times do |i|
  selected = trees[i..(i + k -1)]
  # p selected[-1] - selected[0]
  if diff > (selected[-1] - selected[0])
    diff = selected[-1] - selected[0]
  end
end

puts diff
