n,k = gets.chomp.split(' ').map(&:to_i)
arr = []
n.times do
  arr.push(gets.to_i)
end
arr.sort!
i = 0
min = 10 ** 10
while i <= n - k
  if arr[i+k-1] - arr[i] < min
    min = arr[i+k-1] - arr[i]
  end
  i += 1
end
puts min