'use strict'
function main(input) {
  input = input.trim().split('\n')
  const NM = input.shift().split(' ').map(v => Number(v))
  input = input.map(v => Number(v)).sort((a, b) => a - b)

  let distance = []
  for (let i = 0; i <= NM[0] - NM[1]; i++) {
    distance.push(input[i+NM[1]-1] - input[i])
  }
  console.log(Math.min(...distance))
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
