import heapq

import queue



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



trees = []

heapq.heapify(trees)



for _ in range(N):

    heapq.heappush(trees, int(eval(input())))



ans = 1 + 10**9



q = queue.Queue(maxsize=K-1)



for _ in range(K-1):

    q.put(heapq.heappop(trees))



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

    curmin = q.get()

    nextmin = heapq.heappop(trees)



    ans = min(ans, nextmin-curmin)



    q.put(nextmin)



print(ans)



import sys

INPUT = sys.stdin.readline



def SINGLE_INT(): return int(INPUT())

def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))

def MULTIPLE_INT_MAP(): return list(map(int, INPUT().split()))

def SINGLE_STRING(): return INPUT()

def MULTIPLE_STRING(): return INPUT().split()



N, K = MULTIPLE_INT_MAP()

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

trees.sort()

ans = float('inf')



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

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

    if ans == 0:

        break



print(ans)
import heapq
import queue

N, K = list(map(int, input().split()))
trees = []
heapq.heapify(trees)
for _ in range(N):
    heapq.heappush(trees, int(eval(input())))
ans = 1 + 10**9
q = queue.Queue(maxsize=K - 1)
for _ in range(K - 1):
    q.put(heapq.heappop(trees))
for _ in range(N - K + 1):
    curmin = q.get()
    nextmin = heapq.heappop(trees)
    ans = min(ans, nextmin - curmin)
    q.put(nextmin)
print(ans)

import sys

INPUT = sys.stdin.readline


def SINGLE_INT():
    return int(INPUT())


def MULTIPLE_INT_LIST():
    return list(map(int, INPUT().split()))


def MULTIPLE_INT_MAP():
    return list(map(int, INPUT().split()))


def SINGLE_STRING():
    return INPUT()


def MULTIPLE_STRING():
    return INPUT().split()


N, K = MULTIPLE_INT_MAP()
trees = [int(eval(input())) for _ in range(N)]
trees.sort()
ans = float("inf")
for i in range(0, N - K + 1):
    ans = min(ans, trees[i + K - 1] - trees[i])
    if ans == 0:
        break
print(ans)

-import heapq -import queue +import sys -N, K = list(map(int, input().split())) -trees = [] -heapq.heapify(trees) -for _ in range(N): -    heapq.heappush(trees, int(eval(input()))) -ans = 1 + 10**9 -q = queue.Queue(maxsize=K - 1) -for _ in range(K - 1): -    q.put(heapq.heappop(trees)) -for _ in range(N - K + 1): -    curmin = q.get() -    nextmin = heapq.heappop(trees) -    ans = min(ans, nextmin - curmin) -    q.put(nextmin) +INPUT = sys.stdin.readline + + +def SINGLE_INT(): +    return int(INPUT()) + + +def MULTIPLE_INT_LIST(): +    return list(map(int, INPUT().split())) + + +def MULTIPLE_INT_MAP(): +    return list(map(int, INPUT().split())) + + +def SINGLE_STRING(): +    return INPUT() + + +def MULTIPLE_STRING(): +    return INPUT().split() + + +N, K = MULTIPLE_INT_MAP() +trees = [int(eval(input())) for _ in range(N)] +trees.sort() +ans = float("inf") +for i in range(0, N - K + 1): +    ans = min(ans, trees[i + K - 1] - trees[i]) +    if ans == 0: +        break