CodeEff/ECCO huggingface dataset:
# Original solution
N, K = list(map(int, input().split()))
h = [None] * N
for i in range(N):
    h[i] = int(eval(input()))

h = sorted(h)

sub_min = h[N-1] - h[0]
for i in range(N-K+1):
    if h[i+K-1] - h[i] < sub_min:
        sub_min = h[i+K-1] - h[i]

print(sub_min)

# Optimized solution
# coding: utf-8

# https://atcoder.jp/contests/abc115


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

    h = sorted(h)

    diff = 10**10
    for i in range(N-K+1):
        newdiff = h[i+K-1]-h[i]
        if newdiff < diff:
            diff = newdiff

    return diff


print((main()))
# Original solution
import sys, copy, math, collections, itertools

n, k = (int(i) for i in input().split())
hs = [int(eval(input())) for i in range(n)]
hs.sort()
i=0
m=-1
l=len(hs)
while True:
    if l < (i+k):
        break
    tmp = hs[i:i+k]
    cnd=tmp[-1] - tmp[0]
    if m==-1:
        m=cnd
    else:
        if cnd < m:
            m=cnd
    i+=1
print((int(m)))


# Optimized solution
import sys, copy, math, collections, itertools

n, k = (int(i) for i in input().split())
hs = [int(eval(input())) for i in range(n)]
hs.sort()
i=0
m=-1
l=len(hs)
while True:
    if l < (i+k):
        break
    # tmp = hs[i:i+k]
    cnd=hs[i+k-1] - hs[i]
    if m==-1:
        m=cnd
    else:
        if cnd < m:
            m=cnd
    i+=1
print((int(m)))
# Original solution
import itertools,math
n=input().split()
n=[int(i) for i in n]
h=[]
for i in range(n[0]):
    h.append(int(eval(input())))
q=-1
 
for j in itertools.combinations(h,n[1]):
    if q>max(j)-min(j) or q==-1:
        q=max(j)-min(j)
    if q==0:
        break
 
print(q)

# Optimized solution
n=input().split()
n=[int(i) for i in n]
h=[0]*n[0]
for i in range(n[0]):
    h[i]=int(eval(input()))
h.sort()
q=-1
for i in range(0,len(h)-n[1]+1):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
print(q)
# Original solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h.sort()
ans=1000000000
for j in range(len(h)-2):
    c=h[j:j+k]
    ans=min(ans,c[-1]-c[0])
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
h=[int(eval(input())) for i in range(n)]
h.sort()
print((min([x-y for (x,y) in zip(h[k-1:],h[:-k+1])])))
# Original solution
n,k = list(map(int, input().split()))
h = [0] * n
ans = 0
for i in range(n):
  h[i] = int(eval(input()))
h.sort()
for i in range(n-k+1):
  temp = max(h[i:i+k]) - min(h[i:i+k])
  if temp == 0:
    ans = temp
    break
  elif i == 0:
    ans = temp
  elif ans > temp:
    ans = temp
print(ans)

# Optimized solution
n,k = list(map(int, input().split()))
h = [0] * n
ans = []
for i in range(n):
  h[i] = int(eval(input()))
h.sort()
for i in range(n-k+1):
  ans.append(h[i+k-1] - h[i])
print((min(ans)))
# Original solution
# ABC115C - Christmas Eve
n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)

# Optimized solution
# ABC115C - Christmas Eve
import sys
input = sys.stdin.readline

n, k = list(map(int, input().rstrip().split()))
lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
ans = lst[k - 1] - lst[0]
for i, j in zip(lst, lst[k - 1 :]):
    ans = min(ans, j - i)
print(ans)
# Original solution
# ABC115C - Christmas Eve
def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - i)
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ABC115C - Christmas Eve
import sys
input = sys.stdin.readline

def main():
    n, k = list(map(int, input().rstrip().split()))
    lst = sorted(list(map(int, [input().rstrip() for _ in range(n)])))
    ans = lst[k - 1] - lst[0]
    for i, j in zip(lst, lst[k - 1 :]):
        ans = min(ans, j - i)
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
def main():
    N,K=list(map(int,input().split()))
    h=[]
    for i in range(N):
        a=int(eval(input()))
        h.append(a)
    
    h.sort()
    result=10**10
    count=0
    for i in range(N-K+1):
        count=h[i+K-1]-h[i]
        result=min(result,count)
    print(result)
if __name__=="__main__":
    main()

# Optimized solution
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n,k=list(map(int,readline().split()))
    h=[0]*n
    for i in range(n):
        h[i]=int(readline())
    h.sort()

    ans=float("inf")
    for i in range(k-1,n):
        s=h[i]-h[i-k+1]
        ans=min(s,ans)

    print(ans)
    
if __name__=="__main__":
    main()
# Original solution
n, k = list(map(int, input().split()))
hl = [int(eval(input())) for _ in range(n)]

hl_s = sorted(hl, reverse=True)
delta = 10*10**9

if n > 1000:
    while n < 1000:
        if hl_s[n//2] < (hl_s[0] + hl_s[n-1])//2:
            hl_s = hl_s[:n//2]
        else:
            hl_s = hl_s[n//2:]
        n = len(hl_s)

for i in range(len(hl_s)-k+1):
    h_max = max(hl_s[i:i+k])
    h_min = min(hl_s[i:i+k])
    if delta > (h_max - h_min):
        delta = h_max - h_min
    if delta == 0:
        break

print(delta)

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

h_min = min(hl[i+k-1] - hl[i] for i in range(n-k+1))
print(h_min)
# Original solution
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def S_MAP(): return list(map(str, input().split()))
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
H = [INT() for i in range(N)]
H.sort()
ans = INF
for i in range(N-K+1):
    mi = H[i:i+K][0]
    ma = H[i:i+K][-1]
    tmp = ma - mi
    ans = min(tmp, ans)
print(ans)


# Optimized solution
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def S_MAP(): return list(map(str, input().split()))
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
H = [INT() for i in range(N)]
H.sort()
ans = INF
for i in range(N-K+1):
    mi = H[i]
    ma = H[i+K-1]
    tmp = ma - mi
    ans = min(tmp, ans)
print(ans)
# Original solution
N,K=list(map(int,input().split()))
h=[int(eval(input())) for i in range(N)]
h.sort()
mindif=h[K-1]-h[0]
for i in range(N-K+1):
    tmp=h[i:i+K]
    dif=tmp[-1]-tmp[0]
    if mindif>dif:
        mindif=dif
print(mindif)


# Optimized solution
N,K=list(map(int,input().split()))
h=[int(eval(input())) for i in range(N)]
h.sort()
mindif=h[K-1]-h[0]
for i in range(N-K+1):
    dif=h[i+K-1]-h[i]
    if mindif>dif:
        mindif=dif
print(mindif)
# Original solution
N, K = list(map(int, input().split()))
h = sorted([int(eval(input())) for i in range(N)])
res = []
for i in range(N-K+1):
    tmp = h[i:i+K]
    res.append(tmp[-1] - tmp[0])
print((min(res)))

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

ans=10**9
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=[0]*n
for i in range(n):
    h[i]=int(eval(input()))
h.sort()
ans=10**9
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 = int(eval(input()))
	H.append(h)
ansl = []
H.sort()
for i in range(N - K + 1):
	HK = H[i:i+K]
	ans = max(HK)-min(HK)
	ansl.append(ans)
print((min(ansl)))


# Optimized solution
N, K = list(map(int,input().split( )))
H =[]
for i in range(N):
	h = int(eval(input()))
	H.append(h)
ansl = []
H.sort()

for i in range(N - K + 1):
	#HK = H[i:i+K]
	#ans = max(HK)-min(HK)
	ans = H[i + K - 1] - H[i]
	ansl.append(ans)

print((min(ansl)))
# Original solution
import itertools

N, K = (int(i) for i in input().split(' '))
h=[]
for i in range(N):
    h.append(int(eval(input())))

m = -1
for l in itertools.combinations(h,K):
    d = max(l) - min(l)
    if d < m or m == -1:
        m = d
print(m)

# Optimized solution
N, K = (int(i) for i in input().split(' '))
h=[]
for i in range(N):
    h.append(int(eval(input())))

h.sort()

m = -1
for i in range(N-K+1):
    d = h[i+K-1] - h[i]
    if d < m or m == -1:
        m = d
        
print(m)
# Original solution
n,k = list(map(int,input().split()))
h = sorted([int(eval(input())) for _ in range(n)])
ans = h[k-1]-h[0]
for i in range(n-k+1):
    m = h[i+k-1]-h[i]
    ans = min(m,ans)
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
h = sorted([int(eval(input())) for _ in range(n)])
print((min(h[j+k-1]-h[j] for j in range(n-k+1))))
# Original solution
from itertools import combinations
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]

ans = 10**9
for v in combinations(H, K):
    diff = max(v) - min(v)
    if diff < ans:
        ans = diff
    
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
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n, k = Input()
    data = sorted(int(eval(input())) for _ in range(n))
    ans = 1e9
    for i in range(n-k+1):
        x = data[i:i+k]
        ans = min(ans, max(x) - min(x))
    print(ans)
main()

# Optimized solution
def main():
    n, k = list(map(int, input().split(" ")))
    data = sorted(int(eval(input())) for _ in range(n))
    print((min([data[i+k-1] - data[i] for i in range(n-k+1)])))
main()
# Original solution
import heapq

mINF=-10**10

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

arr=[mINF]*N
for z in range(N):
  heapq.heapreplace(arr,int(eval(input())))

arr2=[mINF]*K
for z in range(K):
  heapq.heapreplace(arr2,heapq.heappop(arr))

r=max(arr2)-min(arr2)

try:
  while True:
    heapq.heapreplace(arr2,heapq.heappop(arr))
    rb=max(arr2)-min(arr2)
    if rb<r:
      r=rb
except Exception:
  pass

print(r)


# Optimized solution
import heapq

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

arr=[0]*N
for z in range(N):
  heapq.heapreplace(arr,int(eval(input())))

arr2=[0]*K
for z in range(K):
  arr2[z]=heapq.heappop(arr)

r=arr2[-1]-arr2[0]

z=0
try:
  while True:
    arr2[z]=heapq.heappop(arr)
    if z==K-1:
      zz=0
    else:
      zz=z+1

    rb=arr2[z]-arr2[zz]
    if rb<r:
      r=rb

    z+=1
    if z==K:
      z=0

except Exception:
  pass

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

import itertools
result = 10**9
h_sorted = sorted(h)
for i in range(n-k+1):
  k_arr = h_sorted[i:i+k]
  k_arr = list(itertools.chain.from_iterable(k_arr))
  result = min(result, k_arr[k-1]-k_arr[0])

print(result)

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

h.sort()
result = 10**9
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 = [int(eval(input())) for i in range(N)]
h.sort()
h_new = [h[i + (K - 1)] - h[i] for i in range(0, N - (K - 1))]
print((min(h_new)))

# Optimized solution
N, K = list(map(int, input().split()))
h = sorted([int(eval(input())) for i in range(N)])
h_new = [h[i + (K - 1)] - h[i] for i in range(0, N - (K - 1))]
print((min(h_new)))
# Original solution
import heapq
 
N, k = [int(s) for s in input().split(' ')]
 
h = []
while len(h) != N:
    heapq.heappush(h, int(eval(input())))
  
min_height = 1000000000
h = heapq.nsmallest(N, h)
for start in range(0, N - k + 1):
    end = start + k - 1
    diff = h[end] - h[start]
    if diff < min_height:
        min_height = diff
print(min_height)


# Optimized solution
N, k = [int(s) for s in input().split(' ')]
 
h = []
while len(h) != N:
    h.append(int(eval(input())))
  
min_height = 1000000000
h.sort()
for start in range(0, N - k + 1):
    end = start + k - 1
    diff = h[end] - h[start]
    if diff < min_height:
        min_height = diff
print(min_height)
# Original solution
N, K =list(map(int, input().split()))
lst = []
for _ in range(N):
    lst.append(int(eval(input())))
sor = sorted(lst)

mindiff = float('inf')
for i in range(N-K+1):
    selected = sor[i:i+K] 
    diff = selected[-1] - selected[0]
    if diff <= mindiff:
        mindiff = diff
print(mindiff) 

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

mindiff = float('inf')
for i in range(N-K+1):
    diff = sor[i+K-1] - sor[i] 
    if diff <= mindiff:
        mindiff = diff
print(mindiff)
# Original solution
N, K = list(map(int, input().split()))
h = []
for i in range(N):
    th = int(eval(input()))
    h.append(th)
h.sort()
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 = 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
import sys
input = sys.stdin.readline
def main():
    N, K = list(map(int, input().split()))
    h = [int(eval(input())) for i in range(N)]
    H = sorted(h)
    ans = 10**9 +1
    for i in range(N-K+1):
        high = max(H[i:i+K]) - min(H[i:i+K])
        ans = min(ans, high)
    print(ans)

if __name__ == '__main__':
    main()

# Optimized solution
import sys
input = sys.stdin.readline
def main():
    N, K = list(map(int, input().split()))
    h = [int(eval(input())) for i in range(N)]
    H = sorted(h)

    ans = 10**9 +1
    for i in range(N-K+1):
        high = H[i+K-1] - H[i] 
        ans = min(ans, high)
    print(ans)

if __name__ == '__main__':
    main()
# 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
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()))
T = [int(eval(input())) for i in range(N)]

T.sort()
P = []
for i in range(N-K+1):
    P.append(T[i+K-1] - T[i])

print((min(P)))
# Original solution
n,k = list(map(int,input().split()))
c = [int(eval(input())) for _ in range(n)]

c.sort()

ans = []
for i in range(n-k+1):
    ans.append(max(c[i:k+i])-min(c[i:k+i]))

print((min(ans)))

# Optimized solution
n,k = list(map(int,input().split()))
c = [int(eval(input())) for _ in range(n)]
c.sort()
ans = []
for i in range(n-k+1):
    ans.append(c[i+k-1]-c[i])
print((min(ans)))
# Original solution
def solve():
    n, k = list(map(int, input().split()))
    h = sorted(list(int(eval(input())) for _ in range(n)))
    min_diff = 10 ** 9
    for i in range(n - k + 1):
        part = h[i:i + k]
        diff = part[-1] - part[0]
        if diff < min_diff:
            min_diff = diff
    return min_diff


if __name__ == "__main__":
    print((solve()))


# Optimized solution
def solve():
    n, k = list(map(int, input().split()))
    h = sorted(list(int(eval(input())) for _ in range(n)))
    return min(h[i + k - 1] - h[i] for i in range(n - k + 1))


if __name__ == "__main__":
    print((solve()))
# Original solution
N,K=list(map(int,input().split()))
h=[]
for i in range(N):
    h.append(int(eval(input())))
h.sort()

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


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

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

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

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

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

s = 0
t = float("inf")
for i in range(n-k+1):
  p = sum(l[i:i+k-1])
  if p < t:
    t = p
    s = i
    
print((h[s+k-1] - h[s]))

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

ans = 0
t = float("inf")
for i in range(n-k+1):
  if abs(h[i+k-1] - h[i]) < t:
    t = abs(h[i+k-1] - h[i])
    ans = i

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

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

t = [[] for i in range(n-1)]
t[0] = diff[0]
for i in range(1,n-1):
    t[i] = t[i-1]+diff[i]
t = [0] + t

# print(h)
# print(diff)
# print(t)

ans = float("inf")
for i in range(n-k+1):
    a = t[i+k-1]-t[i]
    ans = min(ans, a)

print(ans)


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

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

tree = []
for _ in range(n):
    tree.append(int(eval(input())))

tree.sort()
ans = 10**9
for i in range(n-k+1):
    ans = min(ans, max(tree[i:i+k])-min(tree[i:i+k]))
print(ans)


# Optimized solution
n, k = list(map(int, input().split()))

tree = []
for _ in range(n):
    tree.append(int(eval(input())))

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

tree = []
for _ in range(n):
    tree.append(int(eval(input())))

tree.sort()
ans = 10**9
for i in range(n-k+1):
    ans = min(ans, tree[i+k-1]-tree[i])
print(ans)


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 6)


def input():
    return sys.stdin.readline()[:-1]


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

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
n, k = list(map(int,input().split()))
h = [int(eval(input())) for i in range(n)]
ans = max(h) - min(h)
h.sort(reverse=True)

h_n = []
for i in range(0,n):
    l = h[i:i+k]
    if len(l) == k:
        h_n.append(l)

seed = max(h)
for i in range(0,len(h_n)):
    high = max(h_n[i]) - min(h_n[i])
    if seed > high:
        seed = high

print(seed)

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

seed = max(h)
for i in range(0,n-k+1):
    high = max(h[i:i+k]) - min(h[i:i+k])
    if seed > high:
        seed = high

print(seed)
# Original solution
n, k = list(map(int, input().split()))
a = [int(eval(input())) for _ in range(n)]
a.sort()

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

print((min(ans)))

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

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

print((min(c)))
# Original solution
n, k = list(map(int, input().split()))
h = []
ans = 1000000001
for i in range(n):
    h.append(int(eval(input())))
h.sort()
for j in range(n-(k-1)):
    x = []
    if ans == 0:
        break
    for l in range(j,j+k):
        x.append(h[l])
        if ans == 0:
            break
        elif len(x) == k:
            y = x[-1]-x[0]
            if ans == 0:
                break
            elif ans > y:
                ans = y
print(ans)

# Optimized solution
n, k = list(map(int, input().split()))
h = []
ans = 1000000001
for i in range(n):
    h.append(int(eval(input())))
h.sort()
for j in range(n-(k-1)):
    if ans == 0:
        break
    elif ans > h[j+k-1]-h[j]:
        ans = h[j+k-1]-h[j]
print(ans)
# Original solution
import itertools
n,k=list(map(int,input().split()))
h=[int(eval(input())) for _ in range(n)]
ans=float('inf')
for i in itertools.permutations(h,k):
    tmp=max(i)-min(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=float('inf')
for i in range(n-k+1):
    tmp=h[i+k-1]-h[i]
    ans=min(tmp,ans)
print(ans)
# Original solution
n, k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
h.sort()
ans = 10 ** 9
for i in range(n-k+1):
    if h[i+k-1] - h[i] < ans:
        ans = h[i+k-1] - h[i]
print(ans)

# Optimized solution
n,k = list(map(int, input().split()))
h = [int(eval(input())) for _ in range(n)]
h.sort()
ans = float("INF")
for i in range(n-k+1):
    if ans > h[i+k-1] - h[i]:
        ans = h[i+k-1] - h[i]
print(ans)
# Original solution
n, k, *a = list(map(int, open(0).read().split()))
a.sort()
mn = 1e18
for i in range(n-k+1):
    tmp = 0
    for j in range(k-1): tmp+=abs(a[i+j]-a[i+j+1])
    mn = min(mn, tmp)
print(mn)

# Optimized solution
n, k, *a = list(map(int, open(0).read().split()))
a.sort()
for i in range(n-1): a[n-i-1] = abs(a[n-i-1]-a[n-i-2])
a[0] = 0
for i in range(n-1): a[i+1] += a[i]

mn = 1e18
for i in range(n-k+1): mn = min(mn, a[i+k-1]-a[i])
print(mn)
# Original solution
n, k, *a = list(map(int, open(0).read().split()))
a.sort()
for i in range(n-1): a[n-i-1] = abs(a[n-i-1]-a[n-i-2])
a[0] = 0
for i in range(n-1): a[i+1] += a[i]

mn = 1e18
for i in range(n-k+1): mn = min(mn, a[i+k-1]-a[i])
print(mn)

# Optimized solution
n, k, *a = list(map(int, open(0).read().split()))
a.sort()

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

li.sort()
num = 0
li2,li3 = [],[]
for i in range(len(li)-k+1):
    li2.append(li[i:i+k])

for i in range(len(li2)):
    li3.append(max(li2[i]) - min(li2[i]))
print((min(li3)))

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

li.sort()
num = 0
li2 = []
for i in range(len(li)-k+1):
    li2.append( max(li[i:i+k]) - min(li[i:i+k]))

print((min(li2)))
# Original solution
n, k = list(map(int,input().split()))
li = list(int(eval(input())) for i in range(n))

li.sort()
num = 0
li2 = []
for i in range(len(li)-k+1):
    li2.append( max(li[i:i+k]) - min(li[i:i+k]))

print((min(li2)))

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

li.sort()
num = 0
li2 = []
for i in range(len(li)-k+1):
    li2.append(li[i+k-1] - li[i])

print((min(li2)))