'use strict'

const main = (INPUT) => {
  let input = INPUT.split('\n')
  const K = Number(input[0].split(' ')[1])
  input.shift()
  const h = input.map(n => Number(n)).sort((a, b) => a - b)

  let max = Number.MAX_SAFE_INTEGER
  for (let i = 0; i <= h.length - K; i++) {
    let tmp = h[i + K - 1] - h[i]
    if (tmp < max) {
      max = tmp
    }
  }
  console.log(max)
}

main(require('fs').readFileSync('/dev/stdin', 'utf-8'))
