object Main extends App {
	val Array(n, k) = scala.io.StdIn.readLine.split(" ").map(_.toInt)
	val h = new Array[Long](n)

	for(i<-0 until n){
		h(i) = scala.io.StdIn.readLong
	}

	val h1 = h.sorted

	var ans = Long.MaxValue

	for(j<-0 to n - k){
		ans = math.min(ans, h1(j+k-1) - h1(j))
	}

	println(ans)

}