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

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

H_sort = sorted(H)

ans = H_sort[K-1] - H_sort[0]

for j in range(K, N):

    h_diff = H_sort[j] - H_sort[j + 1 -K]

    ans = min(ans, h_diff)

print(ans)
import sys

input = sys.stdin.readline



INF = float("inf")





def main():

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

    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)



if __name__ == '__main__':

    main()
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]
H_sort = sorted(H)
ans = H_sort[K - 1] - H_sort[0]
for j in range(K, N):
    h_diff = H_sort[j] - H_sort[j + 1 - K]
    ans = min(ans, h_diff)
print(ans)

import sys

input = sys.stdin.readline
INF = float("inf")


def main():
    n, k = list(map(int, input().split()))
    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)


if __name__ == "__main__":
    main()

-N, K = list(map(int, input().split())) -H = [int(eval(input())) for _ in range(N)] -H_sort = sorted(H) -ans = H_sort[K - 1] - H_sort[0] -for j in range(K, N): -    h_diff = H_sort[j] - H_sort[j + 1 - K] -    ans = min(ans, h_diff) -print(ans) +import sys + +input = sys.stdin.readline +INF = float("inf") + + +def main(): +    n, k = list(map(int, input().split())) +    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) + + +if __name__ == "__main__": +    main()