# encoding:utf-8



import copy

import numpy as np

import random



n,k = list(map(int,input().split()))



h = []

for i in range(n):

    h.append(int(eval(input())))



h.sort()

# print(h)

max_tree = []

min_tree = []

ans = 10**9

for i in range(n-k+1):

    ans = min(h[i+k-1]-h[i],ans)



print(ans)

#!/usr/bin/env python3

# encoding:utf-8

import copy

import random

import bisect #bisect_left　これで二部探索の大小検索が行える

import fractions #最小公倍数などはこっち

import math

import sys

import collections

from decimal import Decimal # 10進数で考慮できる



mod = 10**9+7

sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000



d = collections.deque()

def LI(): return list(map(int, sys.stdin.readline().split()))



N, K = LI()

h = [0 for i in range(N)]

for i in range(N):

    h[i] = int(eval(input()))



h.sort()

ans = mod

for i in range(N - K + 1):

    # print(h[i+K - 1] , h[i])

    ans = min(ans, h[i + K - 1] - h[i])

print(ans)
# encoding:utf-8
import copy
import numpy as np
import random

n, k = list(map(int, input().split()))
h = []
for i in range(n):
    h.append(int(eval(input())))
h.sort()
# print(h)
max_tree = []
min_tree = []
ans = 10**9
for i in range(n - k + 1):
    ans = min(h[i + k - 1] - h[i], ans)
print(ans)

#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect  # bisect_left　これで二部探索の大小検索が行える
import fractions  # 最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal  # 10進数で考慮できる

mod = 10**9 + 7
sys.setrecursionlimit(mod)  # 再帰回数上限はでdefault1000
d = collections.deque()


def LI():
    return list(map(int, sys.stdin.readline().split()))


N, K = LI()
h = [0 for i in range(N)]
for i in range(N):
    h[i] = int(eval(input()))
h.sort()
ans = mod
for i in range(N - K + 1):
    # print(h[i+K - 1] , h[i])
    ans = min(ans, h[i + K - 1] - h[i])
print(ans)

+#!/usr/bin/env python3 -import numpy as np +import bisect  # bisect_left　これで二部探索の大小検索が行える +import fractions  # 最小公倍数などはこっち +import math +import sys +import collections +from decimal import Decimal  # 10進数で考慮できる -n, k = list(map(int, input().split())) -h = [] -for i in range(n): -    h.append(int(eval(input()))) +mod = 10**9 + 7 +sys.setrecursionlimit(mod)  # 再帰回数上限はでdefault1000 +d = collections.deque() + + +def LI(): +    return list(map(int, sys.stdin.readline().split())) + + +N, K = LI() +h = [0 for i in range(N)] +for i in range(N): +    h[i] = int(eval(input())) -# print(h) -max_tree = [] -min_tree = [] -ans = 10**9 -for i in range(n - k + 1): -    ans = min(h[i + k - 1] - h[i], ans) +ans = mod +for i in range(N - K + 1): +    # print(h[i+K - 1] , h[i]) +    ans = min(ans, h[i + K - 1] - h[i])