import std.stdio, std.conv, std.string, std.algorithm.searching, std.algorithm.iteration, std.algorithm.sorting, std.algorithm.comparison;

void main()
{
    auto input = readln().strip.split.to!(int[]);
    auto N = input[0];
    auto K = input[1];

    int[] hList = [];

    foreach(i; 0..N)
    {
        hList ~= readln.strip.to!int;
    }
    hList.sort();
    int minH=int.max;
    for(int i = 0;i < N-K+1;i++)
    {
        minH = min(hList[K-1+i] - hList[i], minH);
    }
    writeln(minH);
}
