# coding:utf-8



import sys

from collections import Counter





input = sys.stdin.readline

INF = float('inf')

MOD = 10 ** 9 + 7





def inpl(): return list(map(int, input().split()))





N, K = inpl()



H = []

for i in range(N):

    H.append(int(eval(input())))



C = Counter(H)



H2 = sorted(H)

S = [0]

for i in range(N - 1):

    S.append(S[-1] + H2[i + 1] - H2[i])



# print(H2)

ans = INF

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

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

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



print(ans)

# coding:utf-8



import sys





input = sys.stdin.readline

INF = float('inf')

MOD = 10 ** 9 + 7





def inpl(): return list(map(int, input().split()))





N, K = inpl()

H = [int(eval(input())) for _ in range(N)]



H.sort()

ans = INF

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

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



print(ans)

# coding:utf-8
import sys
from collections import Counter

input = sys.stdin.readline
INF = float("inf")
MOD = 10**9 + 7


def inpl():
    return list(map(int, input().split()))


N, K = inpl()
H = []
for i in range(N):
    H.append(int(eval(input())))
C = Counter(H)
H2 = sorted(H)
S = [0]
for i in range(N - 1):
    S.append(S[-1] + H2[i + 1] - H2[i])
# print(H2)
ans = INF
for i in range(N - K + 1):
    # print(i + K - 1, S[i + K - 1] - S[i])
    ans = min(ans, S[i + K - 1] - S[i])
print(ans)

# coding:utf-8
import sys

input = sys.stdin.readline
INF = float("inf")
MOD = 10**9 + 7


def inpl():
    return list(map(int, input().split()))


N, K = inpl()
H = [int(eval(input())) for _ in range(N)]
H.sort()
ans = INF
for i in range(N - K + 1):
    ans = min(ans, H[i + K - 1] - H[i])
print(ans)

-from collections import Counter -H = [] -for i in range(N): -    H.append(int(eval(input()))) -C = Counter(H) -H2 = sorted(H) -S = [0] -for i in range(N - 1): -    S.append(S[-1] + H2[i + 1] - H2[i]) -# print(H2) +H = [int(eval(input())) for _ in range(N)] +H.sort() -    # print(i + K - 1, S[i + K - 1] - S[i]) -    ans = min(ans, S[i + K - 1] - S[i]) +    ans = min(ans, H[i + K - 1] - H[i])