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

trees = []

N.times do
  trees << gets.chomp.to_i
end

trees.sort!

min_diff = 1000000000
trees.each_with_index do |tree, i|
  break if i + K > N
  diff = trees[i + K - 1] - tree
  min_diff = diff if diff < min_diff
end

puts min_diff