function Main(input) {
  var tmp = input.trim().split('\n')
  var N = parseInt(tmp[0].split(' ')[0])
  var K = parseInt(tmp[0].split(' ')[1])
  var h = []

  for (var i = 1; i <= N; i++) {
    h.push(tmp[i])
  }

  var H = h.sort()

  var result = 1000000000

  for (var j = 0; j <= N - K; j++) {
    var sliced = H.slice(j, j + K)
    var v = sliced[sliced.length - 1] - sliced[0]
    if (result > v) {
      result = v
    }
  }

  console.log(result)
  return result
}

Main(require('fs').readFileSync('/dev/stdin', 'utf8'))