def bigger(a, b)
    if a > b then
        return a
    else
        return b
    end
end

def smaller(a, b)
    if a < b then
        return a
    else
        return b
    end
end

def sum(arr = [])
    s = 0
    for a in arr
        s += a
    end
    return s
end


input = STDIN.readlines.map{|line|line.strip.split()}
n = input[0][0].to_i
k = input[0][1].to_i
h_list = input.slice(1, n).map{|line|line[0].to_i}.sort
min = h_list[k - 1] - h_list[0]
(n - k).times do |i|
    min = smaller(min, h_list[i + k] - h_list[i + 1])
end
puts(min)