package billgatesproblem;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class DivideCandies {
	public static void main(String[] args) throws NumberFormatException, IOException {
		File file = new File("bill.txt");
		FileReader reader = new FileReader(file);
		BufferedReader buffer = new BufferedReader(reader);
		int k = Integer.parseInt(buffer.readLine());  // First line read k.
		int n = Integer.parseInt(buffer.readLine());  // Second line is the amount of candies.
		int[] candies = new int[n];
		int i = 0;
		String val;
		while ((val = buffer.readLine()) != null) {  // Put candies into the int array.
			candies[i] = Integer.parseInt(val);
			i++;
		}
		Arrays.sort(candies);  // Sort and return the smallest difference of a k window.
		System.out.println(Arrays.toString(candies));
		int minUnfair = candies[candies.length-1];
		for (int j=0; j<=n-k-1; j++) {
			minUnfair = Math.min(candies[j + k - 1] - candies[j], minUnfair);
		}
		System.out.println(Integer.toString(minUnfair));
		buffer.close();
	}
}
