n, k = STDIN.gets.split(/ +/).map(&:to_i)
p = []
n.times do
  p << STDIN.gets.to_i
end
p.sort!


min = p[k-1] - p[0]
i = 1
while i + k -1 < n do
  if min > p[k+i-1] - p[i]
    min = p[k+i-1] - p[i]
  end

  i += 1
end

puts min
