N, K = list(map(int,input().split()))

a = []

for i in range(N):

    a.append(int(eval(input())))

a.sort()

ans = 10**9 + 7

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

    if ans > a[K-1+i] - a[i]:

        ans = a[K-1+i] - a[i]

print(ans)
# 20200612_yorukatsu_c.py

N, K = list(map(int,input().split()))

M = [int(eval(input())) for i in range(N)]

M.sort()

lis = [0]*(N-K+1)

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

    lis[i] = abs(M[i]-M[i+K-1])

print((min(lis)))

N, K = list(map(int, input().split()))
a = []
for i in range(N):
    a.append(int(eval(input())))
a.sort()
ans = 10**9 + 7
for i in range(N - K + 1):
    if ans > a[K - 1 + i] - a[i]:
        ans = a[K - 1 + i] - a[i]
print(ans)

# 20200612_yorukatsu_c.py
N, K = list(map(int, input().split()))
M = [int(eval(input())) for i in range(N)]
M.sort()
lis = [0] * (N - K + 1)
for i in range(N - K + 1):
    lis[i] = abs(M[i] - M[i + K - 1])
print((min(lis)))

+# 20200612_yorukatsu_c.py -a = [] -for i in range(N): -    a.append(int(eval(input()))) -a.sort() -ans = 10**9 + 7 +M = [int(eval(input())) for i in range(N)] +M.sort() +lis = [0] * (N - K + 1) -    if ans > a[K - 1 + i] - a[i]: -        ans = a[K - 1 + i] - a[i] -print(ans) +    lis[i] = abs(M[i] - M[i + K - 1]) +print((min(lis)))