import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;

void main() {
    const tmp = readln.split.to!(long[]);
    const N = tmp[0], K = tmp[1];
    const hs = N.iota.map!(_ => readln.chomp.to!long).array.sort!"a<b".array;
    long ans = long.max;
    foreach (i; 0..N-K+1) {
        ans = min(ans, hs[i+K-1] - hs[i]);
    }
    writeln(ans);
}
