CodeEff/ECCO huggingface dataset:
# Original solution
INF = 1000000007

input = open(0).read

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


def main():

    h.sort()

    ans = INF

    for i in range(N - K + 1):
        ans = min(ans, h[i + K - 1] - h[i])

    print(ans)
    return


main()


# Optimized solution
def main():
    N, K, *h = list(map(int, open(0).read().split()))
    h.sort()
    print((min(j - i for i, j in zip(h, h[K - 1:]))))
    return


main()
# Original solution
n, k = list(map(int, input().split()))
h = sorted([int(eval(input())) for i in range(n)])
diff = [h[i+1] - h[i] for i in range(n-1)]
ans = float("inf")

for i in range(n-k+1):
    ans = min(ans, sum(diff[i:i+k-1]))
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
h = sorted([int(eval(input())) for i in range(n)])
ans = float("inf")

for i in range(n-k+1):
    ans = min(ans, h[i+k-1]-h[i])
print(ans)
# Original solution
n,k=list(map(int,input().split()))
h=list(int(eval(input())) for i in range(n))
h.sort()

c=[]

for i in range(n-k+1):
  a=h[i+k-1]-h[i]
  c.append(a)

print((min(c)))

# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]

h.sort()

l=[]
for i in range(n-k+1):
        a=h[i+k-1]-h[i]
        l.append(a)

print((min(l)))
# Original solution
N, K=list(map(int, input().split()))
h=[int(eval(input())) for i in range(N)]
h.sort()
diff=None
for i in range(N-(K-1)):
    td=h[i+(K-1)]-h[i]
    if diff is None or td<diff:
        diff=td

print(diff)

# Optimized solution
N,K=list(map(int,input().split()))
h=[int(eval(input())) for _ in range(N)]

h.sort(reverse=True)

ans=None
for i in range(N-(K-1)):
  diff=h[i]-h[i+(K-1)]
  if ans is None or diff<ans:
    ans=diff
    
print(ans)
# Original solution
import sys
import math

#sys.setrecursionlimit(10000)

n,k = list(map(int, input().split()))
h = sorted(list(map(int, (eval(input()) for i in range(n)))))

l = [0] * (n-1)
for i in range(n - 1):
    l[i] = h[i + 1] - h[i]

ans = max(l)
for j in range(len(l) - (k - 2)):
    ans = min(ans,sum(l[j:j + k - 1]))

print(ans)


# Optimized solution
import sys
import math

#sys.setrecursionlimit(10000)

n,k = list(map(int, input().split()))
h = sorted(list(map(int, (eval(input()) for i in range(n)))))

ans = max(h)

for i in range(n - k + 1):
    ans = min(ans,h[i + k - 1] - h[i])
print(ans)
# Original solution
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)


# Optimized solution

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)
# Original solution
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]
H.sort()
ans = []

for i in range(N-K+1):
    tree = H[i:i+K]
    ans.append(tree[-1]-tree[0])
print((min(ans)))

# Optimized solution
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]
H.sort()
ans = []

for i in range(N-K+1):
    ans.append(H[i+K-1]-H[i])
print((min(ans)))
# Original solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]
h = sorted(h)
diff_h = []
for i in range(N-1):
  diff_h += [h[i+1] - h[i]]

ans = float('inf')
for i in range(N-K+1):
  ans = min(ans, sum(diff_h[i:i+K-1]))
print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]
h = sorted(h)

ans = float('inf')
for i in range(N-K+1):
  ans = min(ans, h[i+K-1]-h[i])
print(ans)
# Original solution
import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	#print(name)
	sys.stdin = open(name)

input = sys.stdin.readline

n,k = list(map(int,input().split()))
l = [int(eval(input())) for _ in range(n)]

#print(l)

l = sorted(l)

#print(l)

ansmin = float("inf")
for i in range(n-k+1):
	if ansmin > max(l[i:i+k])-min(l[i:i+k]):
		ansmin = max(l[i:i+k])-min(l[i:i+k])

print(ansmin)





# Optimized solution
import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	#print(name)
	sys.stdin = open(name)

input = sys.stdin.readline

n,k = list(map(int,input().split()))
l = [int(eval(input())) for _ in range(n)]

#print(l)

l = sorted(l)

#print(l)

ansmin = float("inf")
for i in range(n-k+1):
	if ansmin > l[i+k-1]-l[i]:
		ansmin = l[i+k-1]-l[i]

print(ansmin)
# Original solution
N,s = input().split()
S = int(s)
a = []
for i in range(int(N)):
    a.append(int(eval(input())))

a.sort()

mindiff = 1000000000

for i in range(int(N)-(S - 1)):
    v = 0
    for j in range(int(S)-1):
        v += a[i+j+1]-a[i+j]
        if i%3 == 0 and mindiff < v:break

    if mindiff > v: mindiff = v

print(mindiff)



# Optimized solution
N,s = input().split()
S = int(s)
a = []
for i in range(int(N)):
    a.append(int(eval(input())))

a.sort()

mindiff = 1000000000

for i in range(int(N)-(S - 1)):
    v = a[i+S-1]-a[i]

    if mindiff > v: mindiff = v

print(mindiff)
# Original solution
N,K = list(map(int,input().split()))
h = []
h2 = []
ans = 10**10
for i in range(N):
  tmp = int(eval(input()))
  h.append(tmp)
h = sorted(h)
for i in range(N-K+1):
  h2 = h[i:i+K]
  ans = min(max(h2) - min(h2),ans)
print((str(ans)))

# Optimized solution
N,K = list(map(int,input().split()))
h = []
ans = 10**10
for i in range(N):
  tmp = int(eval(input()))
  h.append(tmp)
h = sorted(h)
for i in range(N-K+1):
  h2 = h[i]
  h3 = h[i+K-1]
  ans = min(h3 - h2,ans)
print((str(ans)))
# Original solution
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return list(map(int,sys.stdin.readline().replace("\n","").split()))
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list([int(x)*-k for x in sys.stdin.readline().replace("\n","").split()])

if __name__ == "__main__":
    n ,k = I2()
    h = [I() for _ in range(n)]
    h.sort()
    ans = float("inf")
    for i in range(n-k+1):
        l = h[i:i+k]
        #print(l)
        ans = min(ans,l[-1]-l[0])
    print(ans)

# Optimized solution
import sys
import math
import itertools as it
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return list(map(int,sys.stdin.readline().replace("\n","").split()))
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list([int(x)*-k for x in sys.stdin.readline().replace("\n","").split()])

if __name__ == "__main__":
    n ,k = I2()
    h = [I() for _ in range(n)]
    h.sort()
    ans = float("inf")
    for i in range(n-k+1):
        a,b = h[i],h[i+k-1]
        #print(a,b)
        ans = min(ans,b-a)
    print(ans)
# Original solution
in_1 = input().split()
n = int(in_1[0])
k = int(in_1[1])

trees_height = []
for i in range(n):
    trees_height.append(int(eval(input())))

trees_height.sort()


def sub_minmax(list):
    return max(list)-min(list)

subs = []

for i in range(n+1-k):
    subs.append(sub_minmax(trees_height[i:i+k]))

print((min(subs)))



# Optimized solution
in_1 = input().split()
n = int(in_1[0])
k = int(in_1[1])

trees_height = []
for i in range(n):
    trees_height.append(int(eval(input())))

trees_height.sort()


def sub_minmax(list):
    return max(list)-min(list)

subs = []

for i in range(n+1-k):
    subs.append(trees_height[i+k-1]-trees_height[i])


print((min(subs)))
# Original solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h=sorted(h)
min_=float('inf')
for i in range(n-k+1):
    min_=min(min_,abs(h[i]-h[i+k-1]))
print(min_)

# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h=sorted(h)
ans=10**10
for i in range(n-k+1):
    ans=min(ans,h[i+k-1]-h[i])
print(ans)
# Original solution
N,K = (int(i) for i in input().split())  
h = [int(eval(input())) for i in range(N)]

h.sort()

ans=[]

for i in range(N):
	if i+K==N+1:
		break
	else:
		ans.append(max(h[i:i+K]) - min(h[i:i+K]))
print((min(ans)))


# Optimized solution
N,K = (int(i) for i in input().split())  
h = [int(eval(input())) for i in range(N)]
 
h.sort()
 
ans=[]
 
for i in range(N):
	if i+K==N+1:
		break
	else:
		ans.append(h[i+K-1] - h[i])
print((min(ans)))
# Original solution
N, K = list(map(int,input().split()))
h = [int(eval(input())) for i in range(N)]
h.sort()
for i in list(set(h)):
  if(h.count(i) >= K):
    print('0')
    exit()
trees = h[:K]
h_max = max(trees)
h_min = min(trees)
print((h_max - h_min))

# Optimized solution
N, K = list(map(int,input().split()))
h = sorted([int(eval(input())) for i in range(N)])
trees = [h[i+K-1] - h[i] for i in range(N-K+1)]
print((min(trees)))
# Original solution
N, K = list(map(int,input().split()))
H = sorted([int(eval(input())) for _ in range(N)])

heights = []
for i in range(N-K+1):
  heights.append(H[i+K-1]-H[i])
print((min(heights)))

# Optimized solution
N, K = list(map(int,input().split()))
H = sorted([int(eval(input())) for _ in range(N)])
result = []
for i in range(N-K+1):
  result.append(H[i+K-1] - H[i])
print((min(result)))
# Original solution
def ans():
  N, K = list(map(int,input().split()))
  H = sorted([int(eval(input())) for _ in range(N)])
  res = 1e9+7
  for i in range(N-K+1):
    tmp = H[i:i+K]
    res = min(res, max(tmp)-min(tmp))
  print(res)
ans()

# Optimized solution
def ans():
  N, K = list(map(int,input().split()))
  H = sorted([int(eval(input())) for _ in range(N)])
  res = 1e9+7
  for i in range(N-K+1):
    x = H[i+K-1] - H[i]
    if(res > x):
      res = x
  print(res)
ans()
# Original solution
N, K, *h = list(map(int, open(0).read().split()))
h = sorted(list(h))
ans = float("inf")
for i in range(N - K + 1):
    ans = min(ans, h[i + K - 1] - h[i])
print(ans)


# Optimized solution
N, K, *h = list(map(int, open(0).read().split()))
h = sorted(list(h))
print((min(h[i + K - 1] - h[i] for i in range(N - K + 1))))
# Original solution
# https://atcoder.jp/contests/abc115/tasks/abc115_c
# C - Christmas Eve
import itertools

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

for _ in range(n):
    h_list.append(int(input().split()[0]))

# 全探索で頑張る方法
# 多分時間が足りない
p_list = itertools.combinations(h_list, k)
min_value = float('inf')

for p in p_list:
    p = list(p)
    v = max(p) - min(p)
    if v < min_value:
        min_value = v

print(min_value)


# Optimized solution
# https://atcoder.jp/contests/abc115/tasks/abc115_c
# C - Christmas Eve
import itertools

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

for _ in range(n):
    h_list.append(int(input().split()[0]))

h_list = sorted(h_list)

min_value = float('inf')

for i, v in enumerate(h_list[:-k+1]):
    diff_h = h_list[i+k-1] - v
    if diff_h < min_value:
        min_value = diff_h

print(min_value)
# Original solution
import itertools

N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]
hmin = 10**9
for selected in itertools.combinations(h, K):
    delta = max(selected) - min(selected)
    hmin = min(hmin, delta)
    
print(hmin)

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]
h.sort()
hmin = min(h[i+K-1] - h[i] for i in range(N-K+1))
print(hmin)
# Original solution
n,k=list(map(int,input().split()))
h=[]
for i in range(n):
   h.append(int(eval(input()))) 
h.sort()
ans=10**9
a=[]
for i in range(n-k+1):
    ans=min(ans,max(h[i:i+k])-min(h[i:i+k]))
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
h = [int(eval(input())) for i in range(n)]
h.sort()
ans=10**9
for i in range(n-k+1):
    ans=min(ans,h[i+k-1]-h[i])
print(ans)
# Original solution
N, K = list(map(int, input().split()))
h = sorted([int(eval(input())) for i in range(N)])
min_max = lambda x: max(x) - min(x)
print((min([min_max(h[i:i+K]) for i in range(N-K+1)])))




# Optimized solution
N, K = list(map(int, input().split()))
h = sorted([int(eval(input())) for i in range(N)])
print((min([h[i+K-1] - h[i] for i in range(N-K+1)])))
# Original solution
N,K = list(map(int,input().split(" ")))
h = sorted([ int(eval(input())) for _ in range(N) ])
ans = float('inf')
for i in range(N-K+1):
    lst = h[i:i+K]
    ans = min(lst[-1]-lst[0],ans)
print(ans)

# Optimized solution
N,K = list(map(int,input().split(" ")))
h = sorted([ int(eval(input())) for _ in range(N) ])
print((min([ h[i+K-1]-h[i] for i in range(N-K+1) ])))
# Original solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h=sorted(h)
s=[]
for i in range(n-1):
    s.append(h[i+1]-h[i])
t=[]
for i in range(n-k+1):
    t.append(sum(s[i:i+k-1]))

print((min(t)))

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

h.sort()
ans = max(h)
for i in range(n-k+1):
  hi = h[i:i+k]
  ans = min(ans, max(hi) - min(hi))
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for i in range(n)]

h.sort()
ans = max(h)
for i in range(n-k+1):
  ans = min(ans, h[i+k-1] - h[i])
print(ans)
# Original solution
n, k = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(n)]
H = sorted(H)
ans = float('inf')
for i in range(n - k + 1):
    ans = min(ans, H[i + k - 1] - H[i])
print(ans)

# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    H = [int(eval(input())) for _ in range(n)]
    H.sort()
    ans = 10 ** 9 + 2
    for i in range(n - k + 1):
        ans = min(ans, H[i + k - 1] - H[i])
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
n,k=list(map(int,input().split()))
h=sorted([int(eval(input())) for _ in range(n)])
a=10**9
for i in range(n-k+1):
    h1=h[i:i+k]
    a1=h1[-1]-h1[0]
    a=min(a,a1)
print(a)

# Optimized solution
n,k=list(map(int,input().split()))
h=sorted([int(eval(input())) for _ in range(n)])
a=10**9
for i in range(n-k+1):
    a1=h[i+k-1]-h[i]
    a=min(a,a1)
print(a)
# Original solution
N, K = [int(x) for x in input().split()]
h = [int(eval(input())) for x in range(N)]
h.sort()
print((min([h[i+K-1] - h[i] for i in range(N-K+1)])))


# Optimized solution
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, K: int, h: "List[int]"):
    h.sort()
    print((min(h[i+K-1]-h[i] for i in range(N-K+1))))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)


if __name__ == '__main__':
    main()
# Original solution
n,k = list(map(int,input().split()))
H = sorted([int(eval(input())) for _ in range(n)])
ans = float("INF")
for i in range(0,n - k + 1):
  selected = H[i:k + i]
  ans = min(ans,max(selected) - min(selected))
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
H = sorted([int(eval(input())) for _ in range(n)])
ans = float("INF")
for i in range(n - k + 1):
  diff = H[k + i -1] - H[i]
  ans = min(ans,diff)
print(ans)
# Original solution
N, K = list(map(int, input().split()))
heights = sorted([int(eval(input())) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

# Optimized solution
import sys
input = sys.stdin.readline
N, K = list(map(int, input().rstrip().split()))
heights = sorted([int(input().rstrip()) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)
# Original solution
n,k=list(map(int,input().split()))
h=[]
for i in range(n):
    h.append(int(eval(input())))
h.sort()
ans=10**10
for i in range(n-k+1):
    t=h[i+k-1]-h[i]
    ans=min(t,ans)
print(ans)


# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h.sort()
ans=10**18
for i in range(n-k+1):
  ans=min(h[i+k-1]-h[i],ans)
print(ans)
# Original solution
N, K = list(map(int, input().split()))

h = []
for i in range(N):
    h.append(int(eval(input())))
h.sort()

diff = []
for i in range(K-1, N):
    diff.append(h[i] - h[i-K+1])

print((min(diff)))


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

h = []
for i in range(N):
    h.append(int(eval(input())))
h.sort()

diff = 10 ** 9
for i in range(K-1, N):
    diff = min(diff, h[i] - h[i-K+1])

print(diff)
# Original solution
n, k =list(map(int, input().split()))
T = []
D = []

for i in range(n):
    tree = int(eval(input()))
    T.append(tree)

T.sort(reverse=True)
                 
for i in range(n - k + 1):
    diff = T[i] - T[i + k -1]
    D.append(diff)
    
print((min(D)))

# Optimized solution
n, k = list(map(int, input().split()))
A = []
D = []

for i in range(n):
    A.append(int(eval(input())))

A.sort(reverse=True)
    
for i in range(n - k + 1):
    diff = A[i] - A[i + k -1]
    D.append(diff)
    
print((min(D)))
# Original solution
n, k = list(map(int, input().split()))
H = [int(eval(input())) for i in range(n)]
H.sort(reverse=True)
ans = 10 ** 9

for i in range(n-k+1):
    h = H[i] - H[i+k-1]
    ans = min(ans, h)
    
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
A = [int(eval(input())) for i in range(n)]
A.sort(reverse=True)
B = []

for i in range(n-k+1):
    diff = A[i] - A[i+k-1]
    B.append(diff)
    
print((min(B)))
# Original solution
# -*- coding: utf-8 -*-
n, k = list(map(int,input().split()))
h = []
for i in range(n):
    h.append(int(eval(input())))

ans = 10 ** 18
h.sort()
for i in range(n - k + 1):
    if ans > h[i + k - 1] - h[i]:
        ans = h[i + k - 1] - h[i]
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-
n, k = list(map(int,input().split()))
h = []
for i in range(n):
    h.append(int(eval(input())))

h.sort()
ans = 10 ** 5 * 10 ** 10
for i in range(n - k + 1):
    #print(i, i + k - 1)
    if h[i + k - 1] - h[i] < ans:
        ans = h[i + k - 1] - h[i]
print(ans)
# Original solution
N,K=list(map(int,input().split()))
H=[]
for i in range(N):
  H.append(int(eval(input())))
H.sort()
ans=H[K-1]-H[0]
for i in range(N-K+1):
  A=H[i:i+K]
  if ans>A[K-1]-A[0]:
    ans=A[K-1]-A[0]
print(ans)

# Optimized solution
N,K=list(map(int,input().split()))
H=[]
for i in range(N):
  H.append(int(eval(input())))
H.sort()
print((min(H[i+K-1]-H[i] for i in range(N-K+1))))
# Original solution
n,k = list(map(int,input().split()))
f = [int(eval(input())) for i in range(n)]
li = []
f.sort()
for i in range(n-k+1) :
    li.append(max(f[i:i+k])-min(f[i:i+k]))
print((min(li)))

# Optimized solution
n,k = list(map(int,input().split()))
f = [int(eval(input())) for i in range(n)]
li = []
f.sort()
for i in range(n-k+1) :
    li.append(f[i+k-1]-f[i])
print((min(li)))
# Original solution
from collections import Counter
N , K = list(map(int,input().split()))
h = list(int(eval(input())) for _ in range(N))
h.sort()
min = 10 ** 10
for i in range(len(h) - K + 1):
    tmp_min = h[i + K - 1] - h[i]
    if min > tmp_min:
        min = tmp_min
print(min)


# Optimized solution
N, K = list(map(int, input().split()))
trees = list(int(eval(input())) for _ in range(N))
trees.sort()
ans = float("inf")
for i in range(N - K + 1):
    tmp_ans = trees[i + K - 1] - trees[i]
    if tmp_ans < ans:
        ans = tmp_ans
print(ans)
# Original solution
import heapq

n, k = list(map(int, input().split()))
h = []
d = [0 for _ in range(n)]
ans = float('inf')

for _ in range(n):
    heapq.heappush(h, int(eval(input())))

h = [heapq.heappop(h) for _ in range(n)]


for i in range(n - k + 1):
    diff = h[i + k - 1] - h[i]
    ans = min(ans, diff)

print(ans)


# Optimized solution
n, k = list(map(int, input().split()))
h = []
d = [0 for _ in range(n)]
ans = 1e9

for _ in range(n):
    h.append(int(eval(input())))

h = sorted(h)

for i in range(n - k + 1):
    diff = h[i + k - 1] - h[i]
    ans = min(ans, diff)

print(ans)
# Original solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in [0]*N]

h = sorted(h)
ans = []

for i in range(len(h)-K+1):
  a = h[i:i+K]
  ans.append(max(a)-min(a))
  if ans[-1] == 0:
    print((0))
    exit()

print((min(ans)))

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in [0]*N]

h = sorted(h)
ans = []

for i in range(len(h)-K+1):
  ans.append(h[i+K-1]-h[i])
  if ans[-1] == 0:
    print((0))
    exit()

print((min(ans)))
# Original solution
N, K = list(map(int, input().split()))

h = []
ans = 10**9

for i in range(N):
    h.append(int(eval(input())))

h.sort()

for i in range(N-K+1):
    ans = min(max(h[i:i+K]) - min(h[i:i+K]), ans)


print(ans)

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

h = []
ans = 10**9

for i in range(N):
    h.append(int(eval(input())))

h.sort()

for i in range(N-K+1):
    ans = min(h[i+K-1]-h[i], ans)


print(ans)
# Original solution
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()


# Optimized solution
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()
# Original solution
N,K = input().split()
N = int(N)
K = int(K)

trees = []
for i in range(N) :
	trees.append(int(eval(input())))

trees.sort()
min_diff = 99999999999
for i in range(N-K+1) :
	arr = trees[i:i+K]
	max_v = max(arr)
	min_v = min(arr)
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff == 0) :
		break


print(min_diff)

# Optimized solution
N,K = input().split()
N = int(N)
K = int(K)

trees = []
for i in range(N) :
	trees.append(int(eval(input())))

trees.sort()
min_diff = 99999999999
for i in range(N-K+1) :
	max_v = trees[i+K-1]
	min_v = trees[i]
	diff = max_v - min_v
	if (min_diff > diff) :
		min_diff = diff
	if (min_diff == 0) :
		break



print(min_diff)
# Original solution
N, K = list(map(int, input().split()))
H = list(int(eval(input())) for _ in range(N))
H.sort()
ans = 10 ** 18
for i in range(N - K + 1):
    tmp = H[i + K - 1] - H[i]
    ans = min(ans, tmp)
print(ans)


# Optimized solution
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]
H.sort()
ans = 10 ** 18
for i in range(N - K + 1):
    h1 = H[i]
    h2 = H[i + K - 1]
    ans = min(ans, h2 - h1)
print(ans)
# Original solution
n,k = list(map(int,input().split()))
h = []
ans = 10**10

for i in range(n):
    h.append(int(eval(input())))
h.sort()

for i in range(n-k+1):
    tmp = h[i:i+k]
    buf = tmp[-1] - tmp[0]
    if buf < ans:
        ans = buf
print(ans)


# Optimized solution
n,k = list(map(int,input().split()))
h = []
ans = 10**10

for i in range(n):
    h.append(int(eval(input())))
h.sort()

for i in range(n-k+1):
    buf = h[i+k-1] - h[i]
    if buf < ans:
        ans = buf
print(ans)