local n, k = io.read("*n", "*n")
local h = {}
for i = 1, n do h[i] = io.read("*n") end
table.sort(h)
local mindiff = h[k] - h[1]
for i = 2, n - k + 1 do
  mindiff = math.min(mindiff, h[i + k - 1] - h[i])
end
print(mindiff)
