function Main(input) {
  input = input.split("\n")
 
  var n = parseInt(input[0].split(" ")[0], 10)
  var k = parseInt(input[0].split(" ")[1], 10)
  var h = input.slice(1, n + 1).map(n => parseInt(n ,10))
  var hSort = h.sort()

  var min = undefined
  for(var i = 0; i <= hSort.length - k; i++) {
    var abs = Math.abs(hSort[i] - hSort[i + k -1])
    if(min > abs || min == undefined) {
      min = abs
    }
  }
  console.log(min)
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));