'use strict'

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

  let ans = h[K - 1] - h[0]
  for (let i = 1; i <= h.length - K; i++) {
    let a = h[i + K - 1] - h[i]
    if (ans > a) {
      ans = a
    }
  }
  console.log(ans)
}

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