N, K = gets.chomp.split.map(&:to_i)

A = N.times.map { gets.to_i }

newA =A.sort!

i = 0

reselt =[]

@S = 0

while i + K - 1 < N do
     @S = newA[i + K - 1] - newA[i] 
     reselt << @S
     i += 1
 end

 puts reselt.min