N, K = gets.chomp.split(' ').map(&:to_i)
h_list = N.times.map { gets.to_i }
sorted_h_list = h_list.sort
min_diff = 10 ** 9 + 1
answer = (N - K + 1).times do |i|
  diff = sorted_h_list[i + K - 1] - sorted_h_list[i]
  min_diff = diff if diff < min_diff
end
puts min_diff
