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



height = []

for i in range(N):

  height.append(int(eval(input())))

                

height.sort()

dif = [0]*(N-1)

for i in range(N-1):

  dif[i] = height[i+1]-height[i]



diflist = [dif[0]]*(N-1)

for i in range(1,N-1):

  diflist[i] = diflist[i-1] + dif[i]



diflist.insert(0,0)

ans = 10**10

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

  pro = diflist[i+K-1]-diflist[i]

  ans = min(ans,pro)

  

print(ans)

import sys

input = sys.stdin.readline



def 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[K+i-1]-h[i])

        

    print(ans)

    

if __name__ == "__main__":

    main()

N, K = list(map(int, input().split()))
height = []
for i in range(N):
    height.append(int(eval(input())))
height.sort()
dif = [0] * (N - 1)
for i in range(N - 1):
    dif[i] = height[i + 1] - height[i]
diflist = [dif[0]] * (N - 1)
for i in range(1, N - 1):
    diflist[i] = diflist[i - 1] + dif[i]
diflist.insert(0, 0)
ans = 10**10
for i in range(N - K + 1):
    pro = diflist[i + K - 1] - diflist[i]
    ans = min(ans, pro)
print(ans)

import sys

input = sys.stdin.readline


def 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[K + i - 1] - h[i])
    print(ans)


if __name__ == "__main__":
    main()

-N, K = list(map(int, input().split())) -height = [] -for i in range(N): -    height.append(int(eval(input()))) -height.sort() -dif = [0] * (N - 1) -for i in range(N - 1): -    dif[i] = height[i + 1] - height[i] -diflist = [dif[0]] * (N - 1) -for i in range(1, N - 1): -    diflist[i] = diflist[i - 1] + dif[i] -diflist.insert(0, 0) -ans = 10**10 -for i in range(N - K + 1): -    pro = diflist[i + K - 1] - diflist[i] -    ans = min(ans, pro) -print(ans) +import sys + +input = sys.stdin.readline + + +def 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[K + i - 1] - h[i]) +    print(ans) + + +if __name__ == "__main__": +    main()