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

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

h.sort()

m = 10 ** 10

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

    tmp = h[i+K-1] - h[i]

    if tmp < m:

        m = tmp

print(m)
# input 高速化

import sys

input = sys.stdin.readline



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

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

h.sort()

m = 10 ** 10

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

    tmp = h[i+K-1] - h[i]

    if tmp < m:

        m = tmp

print(m)
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]
h.sort()
m = 10**10
for i in range(N - K + 1):
    tmp = h[i + K - 1] - h[i]
    if tmp < m:
        m = tmp
print(m)

# input 高速化
import sys

input = sys.stdin.readline
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]
h.sort()
m = 10**10
for i in range(N - K + 1):
    tmp = h[i + K - 1] - h[i]
    if tmp < m:
        m = tmp
print(m)

+# input 高速化 +import sys + +input = sys.stdin.readline