#import <stdio.h>
#import <stdlib.h>

typedef int Int;

Int compareInt(const void *a, const void *b) {
    return *(Int*)a - *(Int*)b;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Int N, K;
        scanf("%d %d", &N, &K);
        Int *numbers = (int *)malloc(N * sizeof(Int));
        
        Int n;
        Int i = 0;
        while (scanf("%d", &n) != EOF) {
            numbers[i] = n;
            i++;
        }
        
        qsort(numbers, N, sizeof(Int), compareInt);
        
        Int min = numbers[N-1];
        for (Int i=0; i<=N-K; i++) {
            Int diff = numbers[i+K-1] - numbers[i];
            min = (min<diff) ? min : diff;
        }
        printf("%d\n", min);
    }
    return 0;
}
