CodeEff/ECCO huggingface dataset:
# Original solution
import sys
import os
import math
import string

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    H = [ii() for _ in range(N)]
    H = sorted(H, reverse=True)
    cnt = len(H)-K+1
    ret = [0]*cnt
    for i in range(cnt):
        ret[i] = max(H[i:i+K]) - min(H[i:i+K])
    print((min(ret)))


if __name__ == '__main__':
    main()


# Optimized solution
import sys
import os
import math
import string

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: x * y / math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    H = [ii() for _ in range(N)]
    H = sorted(H, reverse=True)
    cnt = len(H)-K+1
    ret = [0]*cnt
    for i in range(cnt):
        ret[i] = H[i] - H[i+K-1]
    print((min(ret)))


if __name__ == '__main__':
    main()
# Original solution
N, K= list(map(int, input().split()))

h = list(sorted([int(eval(input())) for i in range(N)]))
ans = float('inf')

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

print(ans)


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

h = list(sorted([int(eval(input())) for i in range(N)]))
h_dif = [h[i+K-1] - h[i] for i in range(N-K+1)]
print((min(h_dif)))
# Original solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for _ in range(n)]
h.sort()
INF=2*10**9
ans=INF
for i in range(n-k+1):
	diff=max(h[i:i+k])-min(h[i:i+k])
	if ans>diff:
		ans=diff
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for _ in range(n)]
h.sort()
INF=2*10**9
ans=INF
for i in range(n-k+1):
	diff=h[i+k-1]-h[i]
	if ans>diff:
		ans=diff
		if ans==0:
			print((0))
			exit()
print(ans)
# Original solution
import itertools

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

hmin =[]

c = itertools.combinations(h,K)

for v in c:
    hmin.append(max(v)-min(v))
print((min(hmin)))

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

height = []
for i in range(N):
  height.append(int(eval(input())))
                
height.sort()
dif = [0]*(N-1)
for i in range(N-1):
  dif[i] = height[i+1]-height[i]

diflist = [dif[0]]*(N-1)
for i in range(1,N-1):
  diflist[i] = diflist[i-1] + dif[i]

diflist.insert(0,0)
ans = 10**10
for i in range(N-K+1):
  pro = diflist[i+K-1]-diflist[i]
  ans = min(ans,pro)
  
print(ans)


# Optimized solution
import sys
input = sys.stdin.readline

def main():
    N,K = list(map(int,input().split()))
    h = [int(eval(input())) for _ in range(N)]
    h.sort()
    
    ans = 10**10
    for i in range(N-K+1):
        ans = min(ans,h[K+i-1]-h[i])
        
    print(ans)
    
if __name__ == "__main__":
    main()
# Original solution
#!/usr/bin/env python3
# Christmas Eve

N = list(map(int, (input().strip().split(' '))))
K = N[1]
N = N[0]
i = 0
h = []
while i < N:
  hi = int(input().strip())
  h.append(hi)
  i += 1
h = sorted(h)

i = 0
min_i = i
min_diff = 10000000000
while i < N - K + 1:
  sub = h[i:i+K]
  diff = sub[K-1] - sub[0]
  if min_diff > diff:
    min_diff = diff
    min_i = i
  i += 1
print(min_diff)

"""
med = sum(h)/len(h)
print(list(map(lambda x:x-med, h)))
"""

# Optimized solution
#!/usr/bin/env python3
# Christmas Eve

N = list(map(int, (input().strip().split(' '))))
K = N[1]
N = N[0]
i = 0
h = []
while i < N:
  hi = int(input().strip())
  h.append(hi)
  i += 1
h = sorted(h)

i = 0
min_diff = 10000000000
while i < N - K + 1:
  diff = h[i+K-1] - h[i]
  if min_diff > diff:
    min_diff = diff
  i += 1
print(min_diff)
# Original solution
# C
N, K = list(map(int, input().split()))
h = [0]*N
for i in range(N):
    h[i] = int(eval(input()))
h.sort()
result = 1e10
for i in range(N-K+1):
    h_list = h[i:i+K]
    result = min(result, max(h_list) - min(h_list))
print(result)

# Optimized solution
# C
N, K = list(map(int, input().split()))
h = [0]*N
for i in range(N):
    h[i] = int(eval(input()))
h.sort()
result = 1e10
for i in range(N-K+1):
    result = min(result, h[i+K-1]-h[i])
print(result)
# Original solution
n, k = list(map(int, input().split()))
h = []
for i in range(n):
    h.append(int(eval(input())))
h.sort()
d = [h[i+1]-h[i] for i in range(0, len(h)-1)]
S = 0
for i in range(k-1):
    S += d[i]
ans = S
for i in range(0, n-k):
    SS = S + d[i+k-1] - d[i]
    if SS <= ans:
        ans = SS
    S = SS
print(ans)


# Optimized solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in  range(n)]
h.sort()
print((min([h[i+k-1]-h[i] for i in range(n-k+1)])))
# Original solution
import itertools
import math
import fractions
import functools
n, k = list(map(int, input().split()))
h = []
for i in range(n):
    h.append(int(eval(input())))

h.sort()
subs = []
for i in range(1,n):
    subs.append(h[i]-h[i-1])

minimum = 10**10
for i in range(len(subs)-1):
    minimum = min(minimum,sum(subs[i:k+i-1]))


print(minimum)


# Optimized solution
import itertools
import math
import fractions
import functools
n, k = list(map(int, input().split()))
h = []
for i in range(n):
    h.append(int(eval(input())))

h.sort()
subs = []
for i in range(1,n):
    subs.append(h[i]-h[i-1])

amount = [0]*(n)
sums = 0
for i in range(0,len(subs)):
    sums += subs[i]
    amount[i+1] = sums


minimum = 10**10
for i in range(k-1,n):
    minimum = min(minimum,amount[i]-amount[i-k+1])

print(minimum)
# Original solution
import itertools
import math
import fractions
import functools
import copy

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

h.sort()
minimum = 10**10
for i in range(n-k+1):
    minimum = min(minimum,h[i+k-1]-h[i])

print(minimum)

# Optimized solution
def main():
    # n = int(input())
    n, m = list(map(int, input().split()))
    # v = list(map(int, input().split()))
    # s = input()
    h = [int(eval(input())) for _ in range(n)]

    h.sort()

    temp = []
    for i in range(n-m+1):
        temp.append(h[i+m-1]-h[i])

    temp.sort()
    print((temp[0]))


if __name__ == '__main__':
    main()
# Original solution
def resolve():
    n, k = list(map(int, input().split()))
    H = []
    for _ in range(n):
        H.append(int(eval(input())))
    H_sort = list(sorted(H, reverse=True))
    ans = 10**9
    for i in range(n-k+1):
        ans = min(ans, H_sort[i]-H_sort[i+k-1])
    print(ans)
resolve()

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

h.sort()
dh = [h[i+1]-h[i] for i in range(0,n-1)]

mindh = dhsum = sum(dh[:k-1])
for i in range(1,n-k+1):
    dhsum = dhsum-dh[i-1]+dh[i+k-2]
    mindh = min(dhsum,mindh)

ans = mindh
print(ans)

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

h.sort()
mindh = 10**9

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

ans = mindh
print(ans)
# Original solution
import itertools

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

com = list(itertools.combinations(h, k))

ans = max(com[0]) - min(com[0])
for c in com[1:]:
    ans = min(ans, max(c) - min(c))
print(ans)

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

h = sorted(h)

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

h = sorted(h)

ans = max(h[0:k]) - min(h[0:k])
for i in range(1, 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 _ in range(n)]
 
h = sorted(h)
 
ans = h[k-1] - h[0]
for i in range(1, 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.sort()
m = 10 ** 10
for i in range(N-K+1):
    tmp = h[i+K-1] - h[i]
    if tmp < m:
        m = tmp
print(m)

# Optimized solution
# input 高速化
import sys
input = sys.stdin.readline

N, K = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(N)]
h.sort()
m = 10 ** 10
for i in range(N-K+1):
    tmp = h[i+K-1] - h[i]
    if tmp < m:
        m = tmp
print(m)
# Original solution
n, k = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]

from itertools import combinations
x = list(combinations(l, k))

y = []
for i in range(len(x)):
    y.append(max(x[i]) - min(x[i]))

print((min(y)))


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

for i in range(n-k+1):
    x.append(max(l[i:i+k]) - min(l[i:i+k]))

print((min(x)))
# Original solution
n, k = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
l = sorted(l)
#print(l)
x = []
for i in range(n-1):
    x.append(l[i+1] - l[i])
x  = sorted(x)
#print(x)

z = []
for j in range(n-k):
    z.append(sum(x[j:j+k-1]))
print((min(z)))


# Optimized solution
n, k = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
l = sorted(l)
#print(l)
x = []
for i in range(n-k+1):
    x.append(l[i+k-1] - l[i])
x  = sorted(x)
#print(x)

print((min(x[:k-1])))
# Original solution

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

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

print(ans)

# Optimized solution


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

H.sort()

ans = 999999999999999999999
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 _ in range(N)])

ans = float("inf")
for i in range(N-K+1):
  t = H[i:i+K]
  ans = min(ans, t[K-1]-t[0])
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):
  ans = min(ans, H[i+K-1]-H[i])
print(ans)
# Original solution
# sys.stdin.readline()
import sys
input = sys.stdin.readline

n, k = list(map(int, input().split()))
h = [0]*n
for i in range(n):
    h[i] = int(eval(input()))
h.sort()
check = [0]*(n-k+1)
for i in range(n-k+1):
    check[i] = h[i+k-1]-h[i]
print((min(check)))

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = list(map(int, readline().split()))
h = [int(readline()) for _ in range(n)]
h.sort()
ans = float('inf')
for bf, af in zip(h, h[k - 1:]):
    ans = min(ans, af - bf)
print(ans)
# Original solution
n, k = list(map(int, input().split()))
h = list(int(eval(input())) for _ in range(n))

h.sort()

mn = 10 ** 9
for i in range(n-k+1):
    d = h[i+k-1] - h[i]
    mn = min(mn, d)

print(mn)

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

mn = 10 ** 9
for i in range(n-k+1):
    mn = min(mn, h[i+k-1] - h[i])

print(mn)
# Original solution
N, K = list(map(int, input().split()))
H = [int(eval(input())) for i in range(N)]

H.sort()
ans = float("inf")
for i in range(len(H)-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 = float("inf")
for i in range(N-K+1):
	ans = min(ans, H[i+K-1]-H[i])

print(ans)
# Original solution
ans = 10**10
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(ans, h[i+K-1] - h[i])
print(ans)

# Optimized solution
import sys

input = sys.stdin.readline

def main():
    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)
    
if __name__ == '__main__':
    main()
# Original solution
import sys
input=sys.stdin.readline
n,k=list(map(int,input().split()))
l=[0]*n
c=0
for i in range(n):
    l[i]=int(eval(input()))
    if l.count(l[i])>=k:
        print((0))
        c=1
if c==0:
    l.sort()
    print((l[k-1]-l[0]))

# Optimized solution
# coding: utf-8
# Your code here!
n,k=list(map(int,input().split()))
h=[]
for i in range(n):
    h.append(int(eval(input())))
ans=10**10
c=0
h.sort()
for i in range(n-k+1):
    c=h[i+k-1]-h[i]
    if c<ans:
        ans=c
print(ans)
# Original solution
N, K = list(map(int, input().split()))
h = []
for _ in range(N):
    h.append(int(eval(input())))

h.sort()

ans = 10 ** 9
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 = []
for _ in range(N):
    h.append(int(eval(input())))

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
# ABC 115 C

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

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

h.sort()

ans = 1.0e+9

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

print(ans)


# Optimized solution
# ABC 115 C

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

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

h.sort()

ans = 1.0e+9

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

print(ans)
# Original solution
# coding:utf-8

import sys
from collections import Counter


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


N, K = inpl()

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

C = Counter(H)

H2 = sorted(H)
S = [0]
for i in range(N - 1):
    S.append(S[-1] + H2[i + 1] - H2[i])

# print(H2)
ans = INF
for i in range(N - K + 1):
    # print(i + K - 1, S[i + K - 1] - S[i])
    ans = min(ans, S[i + K - 1] - S[i])

print(ans)


# Optimized solution
# coding:utf-8

import sys


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


N, K = inpl()
H = [int(eval(input())) for _ in range(N)]

H.sort()
ans = 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 = []
for n in range(N):
    H.append(int(eval(input())))
H.sort()
ans = 10000000000
for n in range(N-K+1):
    ans = min(ans,H[n+K-1]-H[n])
print(ans)


# Optimized solution
N,K = list(map(int,input().split()))
H = [int(eval(input())) for n in range(N)]
H.sort()
ans = 10000000000
for n in range(N-K+1):
    ans = min(ans,H[n+K-1]-H[n])
print(ans)
# Original solution
n,k = list(map(int,input().split()))
h = []
ans = 1<<50
for i in range(n):
    h.append(int(eval(input())))
h = sorted(h)
for i in range(n-k+1):
    temp = h[i:i+k]
    # print(temp)
    ans = min(ans,temp[-1]-temp[0])
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
h = []
ans = 1<<50
for i in range(n):
    h.append(int(eval(input())))
h = sorted(h)
for i in range(n-k+1):
    ans = min(ans,h[i+k-1]-h[i])
print(ans)
# Original solution
N,K,*h=list(map(int,open(0).read().split()))
h.sort()
if K == N:
    print((h[-1] - h[0]))
else:
    dif_h=[]
    for i in range(N-1):
        dif_h.append(h[i+1] - h[i])
    _min = base = sum(dif_h[:K-1])
    for i in range(K-1,N-1):
        base += dif_h[i]
        base -= dif_h[i-K+1]
        _min = min(base,_min)
    print(_min)    
        
        
    

# Optimized solution
N,K,*h=list(map(int,open(0).read().split()))
h.sort()
_min = 10**9
for i in range(N+1-K):
    _min = min(_min, h[i+K-1] - h[i])
print(_min)
# Original solution
from itertools import combinations

N, K = [int(n) for n in input().split()]
h = [int(eval(input())) for _ in range(N)]
ans = 10**9+1
for c in combinations(h, K):
    mc = max(c)-min(c)
    if ans > mc:
      ans = mc
print(ans)


# Optimized solution
N, K = [int(n) for n in input().split()]
h = sorted([int(eval(input())) for _ in range(N)])
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()))
hlist = [int(eval(input())) for _ in range(N)]
hlist.sort(reverse=True)
sa = []

for _ in range(N-K+1):
    num = hlist[_]-hlist[_+(K-1)]
    sa.append(num)
sa.sort()
print((sa[0]))


# Optimized solution
N, K = list(map(int, input().split()))
hlist = sorted([int(eval(input())) for _ in range(N)])
ans = 10**9 + 7
for i in range(N - K + 1):
    ans = min(ans, hlist[i + K - 1] - hlist[i])
print(ans)
# Original solution
N, K = list(map(int, input().split()))
hlist = sorted([int(eval(input())) for _ in range(N)], reverse=True)
ans = 10**9 + 7
for i in range(N - K + 1):
    # print(hlist[i], hlist[i + K])
    ans = min(ans, hlist[i] - hlist[i + K - 1])
print(ans)


# Optimized solution
N, K = list(map(int, input().split()))
hlist = sorted([int(eval(input())) for _ in range(N)])
ans = 10**9
for i in range(N-K+1):
    ans = min(ans, abs(hlist[i] - hlist[i+K-1]))
print(ans)
# Original solution
import itertools

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

ans = float("inf")

for s in itertools.permutations(H, K):
  s = list(s)
  s.sort()
  ans = min(ans, s[K-1] - s[0])
  
print(ans)

# Optimized solution
# ans

N, K = list(map(int, input().split()))
H = [int(eval(input())) for i in range(N)]
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()))
h = sorted([int(eval(input())) for i in range(n)])

h_dif = []
ans = 10 ** 9 + 1

for i in range(1, n):
    h_dif.append(h[i]-h[i-1])
    if i >= k-1:
        ans = min(ans, sum(h_dif[i-k+1:i]))

print(ans)

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

mind = 10 ** 9 + 1

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

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

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

def calculate(n,k,arr):
    arr.sort()
    drr = []
    for i in range(1,n):
        drr.append(arr[i] - arr[i-1])

    i = 0
    sums = []
    while (i + k-1) <= len(drr):
        tmp = sum(drr[i:i + k-1])
        sums.append(tmp)
        i = i + 1
    print((min(sums)))

calculate(n,k,arr)

# Optimized solution
S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(eval(input())))

def calculate(n, k, arr):
    arr.sort()
    drr = []
    for i in range(1, n):
        drr.append(arr[i] - arr[i - 1])

    j = 0
    s = 0
    sums = []
    for i in range(len(drr) - (k - 1) + 1):
        while j < i + (k-1):
            s = s + drr[j]
            j = j + 1
        sums.append(s)
        s = s - drr[i]
    print((min(sums)))



calculate(n, k, arr)
# Original solution
S = input().split(" ")
n = int(S[0])
k = int(S[1])
arr = []
for i in range(n):
    arr.append(int(eval(input())))

def calculate(n, k, arr):
    arr.sort()
    drr = []
    for i in range(1, n):
        drr.append(arr[i] - arr[i - 1])

    j = 0
    s = 0
    sums = []
    for i in range(len(drr) - (k - 1) + 1):
        while j < i + (k-1):
            s = s + drr[j]
            j = j + 1
        sums.append(s)
        s = s - drr[i]
    print((min(sums)))



calculate(n, k, arr)


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

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

def calculate(n, k, arr):
    drr = []
    arr.sort()
    for i in range(1,n):
      drr.append(arr[i] - arr[i-1])

    s = 0
    j = 0
    sums = []
    for i in range(len(drr)- (k-1) + 1):
      while j < i + (k-1):
        s = s + drr[j]
        j = j + 1
      sums.append(s)
      s = s - drr[i]
    print((min(sums)))

calculate(n, k, arr)
# Original solution
S = input().split(" ")
n = int(S[0])
k = int(S[1])

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

def calculate(n, k, arr):
    drr = []
    arr.sort()
    for i in range(1,n):
      drr.append(arr[i] - arr[i-1])

    s = 0
    j = 0
    sums = []
    for i in range(len(drr)- (k-1) + 1):
      while j < i + (k-1):
        s = s + drr[j]
        j = j + 1
      sums.append(s)
      s = s - drr[i]
    print((min(sums)))

calculate(n, k, arr)




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

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



def calculate(n,k,arr):
    brr = []
    arr.sort()
    for i in range(1,n):
        brr.append(arr[i] - arr[i-1])

    j = 0
    s = 0
    sums = []
    for i in range(0,len(brr) - (k-1) + 1):
        while j < i + (k-1):
            s = s + brr[j]
            j = j + 1
        sums.append(s)
        s = s - brr[i]

    print((min(sums)))



calculate(n,k,arr)
# Original solution
n,k =list(map(int,input().split()))
h =[0]*n

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

sort_h = sorted(h)
diff=10**9

for times in range(n-k+1):
  maxh=0
  minh=10**9
  for i in range(times,k+times):
    if sort_h[i] > maxh:
      maxh= sort_h[i]
    if sort_h[i] < minh:
      minh=sort_h[i]

  if diff > maxh-minh:
    diff = maxh-minh

print(diff)

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

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

sort_h = sorted(h)
diffmin=10**9

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

  diff = sort_h[k+times-1]-sort_h[times]
  if diffmin > diff:
    diffmin=diff

print(diffmin)
# Original solution
INF = 1000000007

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

h.sort()

ans = INF

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

print(ans)


# Optimized solution
INF = 1000000007

N, K, *h = list(map(int, open(0).read().split()))

h.sort()

ans = INF

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

print(ans)
# Original solution
INF = 1000000007

N, K, *h = list(map(int, open(0).read().split()))

h.sort()

ans = INF

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

print(ans)


# Optimized 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()