

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 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')







print((min([trees[i+K-1]-trees[i] for i in range(N-K+1)])))
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 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")
print((min([trees[i + K - 1] - trees[i] for i in range(N - K + 1)])))

-for i in range(0, N - K + 1): -    ans = min(ans, trees[i + K - 1] - trees[i]) -    if ans == 0: -        break -print(ans) +print((min([trees[i + K - 1] - trees[i] for i in range(N - K + 1)])))