#include <bits/stdc++.h>
using namespace std;
int i, b, a, t, k, g, j, l;
int arr[100001];
int main() {
  cin >> b;
  cin >> a;
  for (i = 0; i < a; i++) {
    cin >> arr[i];
  }
  l = 10001;
  sort(arr, arr + a);
  for (i = 0; i <= a - b; i++) {
    g = 0;
    k = 10001;
    for (j = i; j < i + b; j++) {
      g = max(g, arr[j]);
      k = min(k, arr[j]);
    }
    t = g - k;
    l = min(l, t);
  }
  cout << l << endl;
  return 0;
}
