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

arr = []

n.times do
  arr << gets.to_i
end

arr = arr.sort
tmp = arr[k-1] - arr[0]
(n - k + 1).times do |i|
  num = arr[i+k-1] - arr[i]
  tmp = [tmp, num].min
end

p tmp