N, K = STDIN.gets.chomp.split(' ').map(&:to_i)

h = []
STDIN.each_line do |l|
  hn = l.to_i
  h.push(hn)
end

h.sort!

# puts h.join(',')
array = []

d = 0
min = h[K-1] - h[0]
((K-1)..(h.length-1)).each do |i|
  # puts "#{h[i]}-#{h[d]}"
  diff = h[i] - h[d]
  min = diff if diff < min
  d += 1
end

puts min

