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

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



H.sort()

ans = 10**10

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

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



print(ans)
# Python3 (3.4.3)

import sys

input = sys.stdin.readline



# -------------------------------------------------------------

# function

# -------------------------------------------------------------





# -------------------------------------------------------------

# main

# -------------------------------------------------------------

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

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



H.sort()



ans = 10**10

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

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



print(ans)
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]
H.sort()
ans = 10**10
for i in range(N - K + 1):
    ans = min(ans, H[i - 1 + K] - H[i])
print(ans)

# Python3 (3.4.3)
import sys

input = sys.stdin.readline
# -------------------------------------------------------------
# function
# -------------------------------------------------------------
# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]
H.sort()
ans = 10**10
for i in range(N - K + 1):
    ans = min(ans, H[i + K - 1] - H[i])
print(ans)

+# Python3 (3.4.3) +import sys + +input = sys.stdin.readline +# function +# main -    ans = min(ans, H[i - 1 + K] - H[i]) +    ans = min(ans, H[i + K - 1] - H[i])