#!/bin/python3

import math
import os
import random
import re
import sys
from itertools import combinations
# Complete the maxMin function below.
def maxMin(k, arr):
    arr.sort()
    maxVal=sys.maxsize
    n=0
    while(n+k<=len(arr)):
        res = (arr[n+k-1]) - (arr[n])
        maxVal  = min(maxVal , res)
        n = n+1
    return maxVal

n = int(input())

k = int(input())

arr = []

for _ in range(n):
    arr_item = int(input())
    arr.append(arr_item)

result = maxMin(k, arr)
print(result)
