def max(a,b); a < b ? b : a; end
def min(a,b); a < b ? a : b; end

class Problem
  attr_accessor *(?a..?z).to_a.map(&:to_sym)

  def initialize
    @n,@k = gets.to_s.split.map{ |v| v.to_i }
    @h = Array.new(n){ gets.to_s.to_i }

    @gap = @h.sort.each_cons(2).map{ _2 - _1 }
    @cs = @gap.each_with_object([0]) { |v, h| h << h[-1] + v }
  end

  def solve
    ans = 10**12
    0.upto(n-k) do |i|
      ans = min(ans, @cs[i+k-1] - @cs[i])
    end
    ans
  end
  
  def show(ans)
    puts ans
  end
end

Problem.new.instance_eval do
  show(solve)
end
