import java.util.Scanner
import scala.collection.mutable

object Main extends App {
  val sc = new Scanner(System.in)
  def next(): String = sc.next()
  def nextInt(): Int = next().toInt
  def nextLong(): Long = next().toLong

  def task(): Unit = {
    val n, k = nextInt()
    val hs = Seq.fill(n)(nextInt())
    val map = new mutable.TreeMap[Int, Int]
    for (i <- 0 until k) {
      map.get(hs(i)) match {
        case None => map += ((hs(i), 1))
        case Some(v) => map += ((hs(i), v + 1))
      }
    }

    var ans = map.max._1 - map.max._1
    for (i <- k until n) {
      val prev = map.remove(hs(i - k)).get
      if (prev != 1) {
        map += ((hs(i - k), prev - 1))
      }

      map.get(hs(i)) match {
        case None => map += ((hs(i), 1))
        case Some(v) => map += ((hs(i), v + 1))
      }

      println(map)

      ans = Math.min(ans, map.max._1 - map.min._1)
    }
    println(ans)
  }

  task()
}
