'use strict'

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

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

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