def resolve():

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

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



    ans=10**20

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

        ans=min(H[i+K-1]-H[i], ans)

    print(ans)

resolve()

import sys

sys.setrecursionlimit(10 ** 5 + 10)

def input(): return sys.stdin.readline().strip()



def resolve():

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

    h=sorted([int(eval(input())) for i in range(n)])

    ans=10**20

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

        ans=min(ans,h[i+k-1]-h[i])

    print(ans)





resolve()
def resolve():
    N, K = list(map(int, input().split()))
    H = sorted([int(eval(input())) for i in range(N)])
    ans = 10**20
    for i in range(N - K + 1):
        ans = min(H[i + K - 1] - H[i], ans)
    print(ans)


resolve()

import sys

sys.setrecursionlimit(10**5 + 10)


def input():
    return sys.stdin.readline().strip()


def resolve():
    n, k = list(map(int, input().split()))
    h = sorted([int(eval(input())) for i in range(n)])
    ans = 10**20
    for i in range(n - k + 1):
        ans = min(ans, h[i + k - 1] - h[i])
    print(ans)


resolve()

+import sys + +sys.setrecursionlimit(10**5 + 10) + + +def input(): +    return sys.stdin.readline().strip() + + -    N, K = list(map(int, input().split())) -    H = sorted([int(eval(input())) for i in range(N)]) +    n, k = list(map(int, input().split())) +    h = sorted([int(eval(input())) for i in range(n)]) -    for i in range(N - K + 1): -        ans = min(H[i + K - 1] - H[i], ans) +    for i in range(n - k + 1): +        ans = min(ans, h[i + k - 1] - h[i])