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

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



def solve(N,K,trees):

    trees.sort()



    diffs = []

    for i,h in enumerate(trees[:N-K+1]):

        min_h = h

        max_h = trees[i+K-1]

        #print(min_h,max_h)

        diffs.append(max_h-min_h)



    print((min(diffs)))

    return



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

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



def solve(N,K,trees):

    trees.sort()

    

    diffs = [max_h-min_h for min_h,max_h in zip(trees,trees[K-1:])]

    ans = min(diffs)

    print(ans)

    return



solve(N,K,trees)
N, K = list(map(int, input().split()))
trees = [int(eval(input())) for _ in range(N)]


def solve(N, K, trees):
    trees.sort()
    diffs = []
    for i, h in enumerate(trees[: N - K + 1]):
        min_h = h
        max_h = trees[i + K - 1]
        # print(min_h,max_h)
        diffs.append(max_h - min_h)
    print((min(diffs)))
    return


solve(N, K, trees)

N, K = list(map(int, input().split()))
trees = [int(eval(input())) for _ in range(N)]


def solve(N, K, trees):
    trees.sort()
    diffs = [max_h - min_h for min_h, max_h in zip(trees, trees[K - 1 :])]
    ans = min(diffs)
    print(ans)
    return


solve(N, K, trees)

-    diffs = [] -    for i, h in enumerate(trees[: N - K + 1]): -        min_h = h -        max_h = trees[i + K - 1] -        # print(min_h,max_h) -        diffs.append(max_h - min_h) -    print((min(diffs))) +    diffs = [max_h - min_h for min_h, max_h in zip(trees, trees[K - 1 :])] +    ans = min(diffs) +    print(ans)