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)
import sys

def main():

    input = sys.stdin.readline

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

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



    H.sort()

    ans=10**12

    for i in range(N):

        if i+K-1 >= N: break

        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()
ans = 10**10
for i in range(N - K + 1):
    ans = min(ans, H[i + K - 1] - H[i])
print(ans)

import sys


def main():
    input = sys.stdin.readline
    N, K = list(map(int, input().split()))
    H = [int(eval(input())) for _ in range(N)]
    H.sort()
    ans = 10**12
    for i in range(N):
        if i + K - 1 >= N:
            break
        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() -ans = 10**10 -for i in range(N - K + 1): -    ans = min(ans, H[i + K - 1] - H[i]) -print(ans) +import sys + + +def main(): +    input = sys.stdin.readline +    N, K = list(map(int, input().split())) +    H = [int(eval(input())) for _ in range(N)] +    H.sort() +    ans = 10**12 +    for i in range(N): +        if i + K - 1 >= N: +            break +        ans = min(ans, H[i + K - 1] - H[i]) +    print(ans) + + +if __name__ == "__main__": +    main()