# util functions

# debug
$debugFlag = ( ARGV[0] != nil )
p "==== DEBUG MODE ====" if $debugFlag

def pd(str)
  p str if $debugFlag
end

def printd(str)
  print str if $debugFlag
end

# input
arr = []
nn = 0
kk = 0
lc = 0
while l = $stdin.gets do
  if lc == 0
    tmp = l.chomp.split(" ").map(&:to_i)
    nn = tmp[0]
    kk = tmp[1]
  else
    arr << l.chomp.to_i
  end
  lc += 1
end

arr.sort!

# body
if $debugFlag
  p nn
  p kk
  p arr
end

cand = []
for i in 0..nn-kk do
  cand << (arr[i+kk-1]  - arr[i])
  p cand
end

puts cand.min
