# ABC115C - Christmas Eve

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

lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))

ans = lst[k - 1] - lst[0]

for i, j in zip(lst, lst[k - 1 :]):

    ans = min(ans, j - i)

print(ans)
# ABC115C - Christmas Eve

import sys

input = sys.stdin.readline



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

lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))

ans = lst[k - 1] - lst[0]

for i, j in zip(lst, lst[k - 1 :]):

    ans = min(ans, j - i)

print(ans)
# ABC115C - Christmas Eve
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)

# ABC115C - Christmas Eve
import sys

input = sys.stdin.readline
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)

+import sys + +input = sys.stdin.readline