def main():

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

    h=[]

    for i in range(N):

        a=int(eval(input()))

        h.append(a)

    

    h.sort()

    result=10**10

    count=0

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

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

        result=min(result,count)

    print(result)

if __name__=="__main__":

    main()
from sys import stdin

def main():

    #入力

    readline=stdin.readline

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

    h=[0]*n

    for i in range(n):

        h[i]=int(readline())

    h.sort()



    ans=float("inf")

    for i in range(k-1,n):

        s=h[i]-h[i-k+1]

        ans=min(s,ans)



    print(ans)

    

if __name__=="__main__":

    main()
def main():
    N, K = list(map(int, input().split()))
    h = []
    for i in range(N):
        a = int(eval(input()))
        h.append(a)
    h.sort()
    result = 10**10
    count = 0
    for i in range(N - K + 1):
        count = h[i + K - 1] - h[i]
        result = min(result, count)
    print(result)


if __name__ == "__main__":
    main()

from sys import stdin


def main():
    # 入力
    readline = stdin.readline
    n, k = list(map(int, readline().split()))
    h = [0] * n
    for i in range(n):
        h[i] = int(readline())
    h.sort()
    ans = float("inf")
    for i in range(k - 1, n):
        s = h[i] - h[i - k + 1]
        ans = min(s, ans)
    print(ans)


if __name__ == "__main__":
    main()

+from sys import stdin + + -    N, K = list(map(int, input().split())) -    h = [] -    for i in range(N): -        a = int(eval(input())) -        h.append(a) +    # 入力 +    readline = stdin.readline +    n, k = list(map(int, readline().split())) +    h = [0] * n +    for i in range(n): +        h[i] = int(readline()) -    result = 10**10 -    count = 0 -    for i in range(N - K + 1): -        count = h[i + K - 1] - h[i] -        result = min(result, count) -    print(result) +    ans = float("inf") +    for i in range(k - 1, n): +        s = h[i] - h[i - k + 1] +        ans = min(s, ans) +    print(ans)