local n = io.read("n")
local k = io.read("n")
local list = {}
for i = 1, n do
  list[i] = io.read("n")
end
table.sort(list)
local min = nil
for i = 1, n - k + 1 do
  local d = list[i + k - 1] - list[i]
  if not min or d < min then
    min = d
  end
end
print(min)