from sys import stdin
from sys import stdout

N, M = map (int, stdin.readline ().split ())
f = map (int, stdin.readline ().split ())

f.sort ()

B = 0
diff = 1000000

while (B + N <= M):
	A = B + N - 1
		
	diff = min (diff, f [A] - f [B])
	A += 1
	B += 1

print (0 if diff == 1000000 else diff)
153933e991538926e18dbc8510ef62ae1e5c05a5
<mask token>

<mask token>
f.sort()
<mask token>
while B + N <= M:
    A = B + N - 1
    diff = min(diff, f[A] - f[B])
    A += 1
    B += 1
print(0 if diff == 1000000 else diff)

<mask token>
N, M = map(int, stdin.readline().split())
f = map(int, stdin.readline().split())
f.sort()
B = 0
diff = 1000000
while B + N <= M:
    A = B + N - 1
    diff = min(diff, f[A] - f[B])
    A += 1
    B += 1
print(0 if diff == 1000000 else diff)

from sys import stdin
from sys import stdout
N, M = map(int, stdin.readline().split())
f = map(int, stdin.readline().split())
f.sort()
B = 0
diff = 1000000
while B + N <= M:
    A = B + N - 1
    diff = min(diff, f[A] - f[B])
    A += 1
    B += 1
print(0 if diff == 1000000 else diff)

from sys import stdin
from sys import stdout

N, M = map (int, stdin.readline ().split ())
f = map (int, stdin.readline ().split ())

f.sort ()

B = 0
diff = 1000000

while (B + N <= M):
	A = B + N - 1
		
	diff = min (diff, f [A] - f [B])
	A += 1
	B += 1

print (0 if diff == 1000000 else diff)