CodeEff/ECCO huggingface dataset:
# Original solution
# 2019-11-11 18:30:26(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    min_diff = h[-1] - h[0] # default value
    for i in range(n - k + 1):
        heights = h[i:i+k]
        diff = heights[-1] - heights[0]
        min_diff = min(min_diff, diff)
    print(min_diff)



if __name__ == "__main__":
    main()


# Optimized solution
# 2019-11-11 18:30:26(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    min_diff = h[-1] - h[0] # default value
    for i in range(n - k + 1):
        diff = h[i+k-1] - h[i]
        min_diff = min(min_diff, diff)
    print(min_diff)

if __name__ == "__main__":
    main()
# Original solution
# 2019-11-11 18:30:26(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    min_diff = h[-1] - h[0] # default value
    for i in range(n - k + 1):
        diff = h[i+k-1] - h[i]
        min_diff = min(min_diff, diff)
    print(min_diff)

if __name__ == "__main__":
    main()


# Optimized solution
# 2019-11-11 18:30:26(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, k, *h = (int(x) for x in sys.stdin.read().split())
    h.sort()
    diffs = []
    for i in range(n - k + 1):
        diffs.append(h[i+k-1] - h[i])
    print((min(diffs)))

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

h.sort()

DEF_MIN = max(h)-min(h)

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

print(DEF_MIN)

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

h.sort()

DEF_MIN = max(h)-min(h)

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

print(DEF_MIN)
# Original solution
import bisect
N,K = list(map(int,input().split()))
h_list = []
result = []
for i in range(N):
    bisect.insort(h_list,int(eval(input())))
print((min([h_list[i+K-1]-h_list[i] for i in range(N-K+1)])))


# Optimized solution
N,K = list(map(int,input().split()))
h_list = [int(eval(input())) for i in range(N)]
h_list.sort()
result = []
print((min([h_list[i+K-1]-h_list[i] for i in range(N-K+1)])))
# Original solution
N, K = list(map(int, input().split()))
H = [int(eval(input())) for _ in range(N)]
H = sorted(H, reverse=True)
ans = 1e9
for i in range(N-K+1):
    h = H[i:i+K]
    dh = h[0]-h[-1]
    if dh < ans:
        ans = dh
print(ans)

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

ans = float('inf')
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 _ 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
N, K = list(map(int, input().split()))
tree = [int(eval(input())) for i in range(N)]

tree.sort()
m = float("inf")
for i in range(N-K+1):
    d = tree[i : i + K]
    dif = max(d) - min(d)
    m = dif if m > dif else m

print((int(m)))

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

t.sort()
m = 10 ** 9
for i in range(N-K+1):
    m = min(t[i+K-1] - t[i], m)

print((int(m)))
# Original solution
n,k = input().split(' ')
n = int(n)
k = int(k)
h = []
for i in range(n):
    h.append(int(eval(input())))
h.sort()
mindiff = 1000000000
for i in range(n-k+1):
    t = h[i:i+k]
    diff = max(t) - min(t)
    if diff < mindiff:
        mindiff = diff
print(mindiff)


# Optimized solution
n,k = input().split(' ')
n = int(n)
k = int(k)
h = []
for i in range(n):
    h.append(int(eval(input())))
h.sort()
diffs = []
for i in range(n-k+1):
    diffs.append(h[i+k-1] - h[i])
print((min(diffs)))
# Original solution
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[i-1+K]-H[i])

print(ans)

# Optimized solution
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# 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[i+K-1]-H[i])

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

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

h.sort()

iter = len(h) - K

c_hmin = 99999999999999999999999999999999

for i in range(iter+1):
    cell = h[i: K + i]
    hmin = cell[-1] - cell[0]
    if c_hmin > hmin:
        c_hmin = hmin

print(c_hmin)

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

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

h.sort()

iter = len(h) - K

c_hmin = 99999999999999999999999999999999

for i in range(iter+1):

    hmin = h[K+i-1] - h[i]
    if c_hmin > hmin:
        c_hmin = hmin

print(c_hmin)
# Original solution
import sys

r=input().split()
n=int(r[0])
k=int(r[1])
h=sorted([int(x.strip()) for x in sys.stdin.readlines()])

d=[h[x+1]-h[x] for x in range(n-1)]

rui=[0]+[sum(d[:x+1]) for x in range(len(d))]

dd=[rui[x+k-1]-rui[x] for x in range(n-k+1)]
ans=min(dd)
            
print(ans)

# Optimized solution
import sys

r=input().split()
n=int(r[0])
k=int(r[1])
h=sorted([int(x.strip()) for x in sys.stdin.readlines()])

d=[h[x+k-1]-h[x] for x in range(n-k+1)]
ans=min(d)
            
print(ans)
# Original solution
## C - Christmas Eve
import itertools
N, K = list(map(int, input().split()))
rt = [max(x)-min(x) for x in list(itertools.combinations([int(eval(input())) for i in range(N)], K))]
print((min(rt)))

# Optimized solution
## C - Christmas Eve
N, K = list(map(int, input().split()))
H = sorted([int(eval(input())) for i in range(N)])
rt = 10**9
for i in range(0,N-K+1):
    rt = H[K - 1 + i] - H[i] if rt > H[K - 1 + i] - H[i] else rt
print(rt)
# Original solution
import itertools

N, K = (int(i) for i in input().split())  
heights = [int(eval(input())) for i in range(N)] 
heights_lists = list(itertools.combinations(heights, K))

min_diff = max(heights) - min(heights) 
for heights_one in heights_lists:
    if min_diff > (max(heights_one) - min(heights_one) ):
        min_diff = (max(heights_one) - min(heights_one) )

print(min_diff)


# Optimized solution
import itertools

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

heights.sort()
min_diff = max(heights) - min(heights) 
for i in range(N-K+1):
    diff = heights[i+K-1] - heights[i]
    if diff < min_diff:
        min_diff = diff

print(min_diff)
# 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, max(H[i:i+K]) - min(H[i:i+K]))
  
print(ans)


# Optimized 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)
# Original solution
import sys


def main() -> None:
    n, k = [int(s) for s in input().split()]
    h = []
    for i in range(n):
        h += [int(eval(input()))]
    h.sort()

    mn = sys.maxsize
    for i in range(n - k + 1):
        cnt = h[i + k - 1] - h[i]
        mn = min(mn, cnt)

    print(mn)


if __name__ == '__main__':
    main()

# Optimized solution
import sys

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

mn = sys.maxsize
for i in range(n - k + 1):
    cnt = h[i + k - 1] - h[i]
    mn = min(mn, cnt)

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

aa = h_sort[1:]
bb = h_sort[:-1]
diff = [a - b for a, b in zip(aa, bb)]

ans = []
for i in range(len(diff) - (K - 2)):
    tmp = 0
    for j in range(K - 1):
        tmp += diff[i + j]
    ans.append(tmp)
 
print((min(ans)))


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

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

h.sort()
minimum = h[N - 1]- h[0]

for i in range(N - K + 1):
    minimum = min(minimum, h[i + K - 1] - h[i])
print(minimum)
# Original solution
import itertools
N,K=list(map(int,input().split()))
L=[]
n=0
while n<N:
  L.append(int(eval(input())))
  n=n+1
ans=[]
for r in list(itertools.combinations(L,K)):
  ans.append(max(r)-min(r))
print((min(ans)))

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

H = [0] * N
for i in range(N):
    H[i] = int(eval(input()))

H.sort()

d = max(H)
for i in range(N-K+1):
    d = min(max(H[i:i+K]) - min(H[i:i+K]), d)
print(d)

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

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

d = H[K-1] - H[0]
for i in range(1, N-K+1):
    d = min(H[i+K-1] - H[i], d)
print(d)
# Original 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 = H[-1] - H[0]
for i in range(len(H) - 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)])
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.sort()
s=[]
for i in range(N-K+1):
  a=h[i:i+K]
  s.append(a)
cost=[]
for i in range(N-K+1):
  cost.append(max(s[i])-min(s[i]))
print((min(cost)))

# 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
n, k = list(map(int, input().split()))
h = sorted([int(eval(input())) for _ in range(n)])
min_diff = 10**9
for i in range(n - k + 1):
    min_diff = min(min_diff, h[i + k - 1] - h[i])
print(min_diff)

# 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 = []

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

minval = int(1e10)

#print(h)
for i in range(n-k+1):
    if minval >= h[i+(k-1)] - h[i]:
        minval = h[i+(k-1)] - h[i]
print(minval)

# Optimized solution
n, k = list(map(int, input().split()))
h =[]
for i in range(n):
    h.append(int(eval(input())))
h.sort()
ans = int(1e10)
for i in range(n-(k-1)):
    if h[i+(k-1)] - h[i] < ans:
        ans = h[i+(k-1)] - h[i]
#print(h)
print(ans)
# Original solution
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, K, *H = list(map(int, read().split()))
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
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, K, *H = list(map(int, read().split()))
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=list(int(eval(input())) for i in range(n))
h.sort()
mi=float('inf')

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

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

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

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


resolve()

# Optimized solution
def resolve():
    N, K = list(map(int, input().split()))
    h = [int(eval(input())) for _ in range(N)]
    ans = 10 ** 10
    h.sort()

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


resolve()
# Original solution
n,k = list(map(int,input().split()))
hlist = [int(eval(input()))for i in range(n)]
hlist.sort()
dlist = [0]*(n-1)

ans = float('inf')
i = 0
while i+(k-1) <= n-1:
  ans = min(ans, sum((hlist[j+1]-hlist[j]for j in range(i,k-1+i))))
  i += 1

print(ans)


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

ans = 10**9
i = 0

while i+(k-1) <= n-1:
  ans = min(ans, hlist[k+i-1]-hlist[i])
  i += 1

print(ans)
# Original solution
N, K = list(map(int,input().split()))
a = []
for i in range(N):
    a.append(int(eval(input())))
a.sort()
ans = 10**9 + 7
for i in range(N-K+1):
    if ans > a[K-1+i] - a[i]:
        ans = a[K-1+i] - a[i]
print(ans)

# Optimized solution
# 20200612_yorukatsu_c.py
N, K = list(map(int,input().split()))
M = [int(eval(input())) for i in range(N)]
M.sort()
lis = [0]*(N-K+1)
for i in range(N-K+1):
    lis[i] = abs(M[i]-M[i+K-1])
print((min(lis)))
# Original solution
n,k = list(map(int,input().split()))
list1 = [int(eval(input())) for i in range(n) ]
list1.sort()
treemin = 10**9
for i in range(len(list1)-k+1):
	treemin = min(treemin,list1[i+k-1]-list1[i])
print(treemin)

# Optimized solution
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,k = list(map(int,readline().split()))
lst1 = []
for i in range(n):
    lst1.append( int(readline()))
lst1.sort()
ans = 10**9
for i in range(n-k+1):
    ans = min(ans,abs(lst1[i+k-1]-lst1[i]))

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

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

# Optimized solution
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()))
height = [int(eval(input())) for i in range(N)]

height.sort()
h_min = []
for i in range(N - K + 1):
    h_min.append(height[K - 1 + i] - height[i])
    if i > 0:
        if h_min[0] >= h_min[1]:
            del h_min[0]
        else:
            del h_min[1]

print((h_min[0]))


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

h = sorted(h)

nearest = 1000000000
for i in range(N-K+1):
    if nearest > h[i+K-1] - h[i]:
        nearest = h[i+K-1] - h[i]

print(nearest)
# Original 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):
    s = h[i: i + K]
    ans = min(ans, max(s) - min(s))

print(ans)

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

h.sort()

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

height = []
for _ in range(N) :
    height.append(int(eval(input())))

height.sort()
ans = float('inf')

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

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):
    ans = min(ans, H[i + K - 1] - H[i])
print(ans)
# Original solution
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: list(map(int, sys.stdin.readline().split()))
inintl = lambda: list(inintm())
instrm = lambda: list(map(str, sys.stdin.readline().split()))
instrl = lambda: list(instrm())

n, k = inintm()

h = []
ans = 10**10

for _ in range(n):
    h.append(inint())

h.sort()

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

print(ans)

# Optimized solution
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: list(map(int, sys.stdin.readline().split()))
inintl = lambda: list(inintm())
instrm = lambda: list(map(str, sys.stdin.readline().split()))
instrl = lambda: list(instrm())

n, k = inintm()

h = []
ans = 10**10

for _ in range(n):
    h.append(inint())

h.sort()

for i in range(n-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 = [int(eval(input())) for _ in range(n)]
h = sorted(h)
hMin = 10**9
for i in range(n - k + 1):
    a = h[k + i - 1]-h[i]
    if a < hMin:
        hMin = a
print(hMin)


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


# Optimized solution
n, k = list(map(int, input().split()))
h = sorted([int(eval(input())) for _ in range(n)])
hMin = 10**9
print((min(h[k + i - 1]-h[i] for i in range(n - k + 1))))
# Original solution
#!/env/usr/python3
n, k = input().split()
n = int(n)
k = int(k)

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

a.sort()
#print(a)


height_distance = []
for i, h in enumerate(a):
	if i <= n-k:
		chosen = a[i:i+k]
		height_distance.append(max(chosen)-min(chosen))
		#print(height_distance)

print((min(height_distance)))


# Optimized solution
#!/env/usr/python3
n, k = input().split()
n = int(n)
k = int(k)

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

a.sort()
#print(a)


height_distance = []
for i, h in enumerate(a):
	if i <= n-k:
		#chosen = a[i:i+k]
		#height_distance.append(max(chosen)-min(chosen))
		height_distance.append(a[i+k-1]-a[i])
		#print(height_distance)

print((min(height_distance)))
# Original solution
N,K=list(map(int,input().split()))
h=[int(eval(input())) for i in range(N)]
h.sort()
res=[]
for i in range(N):
  if i != 0:
    res.append(h[i]-h[i-1])
m=max(res)
for i in range(N-K+1):
  ans=0
  for j in range(K-1):
    ans+=res[j+i]
  if ans<m:
    m=ans
print(m)

# Optimized solution
N, K = list(map(int, input().split()))
h = [int(eval(input())) for i in range(N)]
h.sort()
res=[]
for i in range(N-K+1):
  res.append(h[K+i-1]-h[i])
print((min(res)))
# Original solution
import sys
from collections import deque
input = sys.stdin.readline

N, K = [int(x) for x in input().strip().split()]
highest = 0
lowest = 10 ** 9
q = deque([])
ans = 10 ** 9
pre = -1
H = sorted([int(eval(input())) for _ in range(N)])
for n, ne in enumerate(H):
    q.append(ne)
    if len(q) > K:
        pre = q.popleft()
    if ne > highest:
        highest = ne
    if ne < lowest:
        lowest = ne
    if pre == highest:
        highest = max(q)
    if pre == lowest:
        lowest = min(q)
    if n >= K - 1:
        ans = min(ans, highest - lowest)

print(ans)

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

N, K = [int(x) for x in input().strip().split()]
H = sorted([int(eval(input())) for _ in range(N)])
ans = 10 ** 9
for n in range(N - (K - 1)):
    ans = min(ans, H[n+K-1] - H[n])

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

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

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

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

# h = [0]*n
# for i in range(n):
#     h[i] = int(input())

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

h.sort()

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

print(ans)
# Original solution
N,K=(int(i) for i in input().split())
h=[int(eval(input())) for i in range(N)]
h.sort()
m = max(h[0:K]) - min(h[0:K])
for i in range(N-K+1):
    d = max(h[i:i+K]) - min(h[i:i+K])
    if m>d:
        m = d
print(m)

# Optimized solution
N,K=(int(i) for i in input().split())
h=[int(eval(input())) for i in range(N)]
h.sort()
m = h[K-1] - h[0] 
for i in range(N-K+1):
    d = h[i+K-1] -h[i]
    if m>d:
        m = d
print(m)
# Original solution
if __name__ == "__main__":
    N, K = list(map(int, input().split()))
    h = list()
    for i in range(N):
        h.append(int(eval(input())))

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K + 1):
        if res > max(h[i:i+K]) - min(h[i:i+K]):
            res = max(h[i:i+K]) - min(h[i:i+K])
    
    print(res)


# Optimized solution
if __name__ == "__main__":
    N, K = list(map(int, input().split()))
    h = list()
    for i in range(N):
        h.append(int(eval(input())))

    h = sorted(h)

    res = 10 ** 9 + 1
    for i in range(N-K+1):
        if res > h[i+K-1] - h[i]:
            res = h[i+K-1] - h[i]
    
    print(res)
# Original solution
#!/usr/bin/env python3

def main():
    *N, = list(map(int, open(0).read().split()))
    h = N[2:]
    K = N[1]
    N = N[0]

    D = [[] for i in range(0, N)]
    for i in range(0, N):
        for j in range(0, N):
            D[i].append(abs(h[i] - h[j]))
        D[i].sort()
    S = []
    for i in range(0, N):
        S.append(sum(D[i][0:K]))

    print((min(S)))


main()

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

def main():
    *N, = list(map(int, open(0).read().split()))
    h = N[2:]
    K = N[1]
    N = N[0]

    h.sort(reverse=True)
    ans = max(h) - min(h)
    for i in range(0, N - K + 1):
        d = h[i] - h[i + K -1]
        if d < ans:
            ans = d
    print(ans)


main()