#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, p;
  cin >> n >> m;
  int arr[m];
  for (int i = 0; i < m; i++) {
    cin >> p;
    arr[i] = p;
  }
  int ans = 0;
  sort(arr, arr + m);
  if (n == m) {
    cout << arr[m - 1] - arr[0];
  } else {
    int min = arr[n - 1] - arr[0];
    for (int j = 1; j < m - n + 1; j++) {
      ans = arr[j + n - 1] - arr[j];
      if (min > ans) {
        min = ans;
      }
    }
    cout << min;
  }
  return 0;
}
