object Main {
  def main(args: Array[String]): Unit = {
    val factors      = scala.io.StdIn.readLine.split(" ").map{_.toInt}
    var tree_heights = Array[Int]()

    for(i <- 0 until factors(0)) {
      tree_heights = tree_heights :+ scala.io.StdIn.readInt
    }

    tree_heights = tree_heights.sorted

    var min_diff = 400000000
    for(i <- 0 until (factors(0) - factors(1) + 1)) {
      var diff = Math.abs(tree_heights(i) - tree_heights(i + factors(1) - 1))
      if (diff < min_diff) min_diff = diff
    }
    println(min_diff)
  }
}