n, k = gets.split.map(&:to_i)
h = []
for i in 1..n do
  h << gets.to_i
end

h = h.sort
ans = 10**9
for i in 0..(n - k) do
  tmp = h[i..(i + k - 1)]
  diff = tmp.max - tmp.min
  ans = diff if diff < ans
end

puts ans