#include <bits/stdc++.h>
using namespace std;
bool sortMethod(const pair<long long int, long long int>& a,
                const pair<long long int, long long int>& b) {
  return a.second < b.second;
}
int main() {
  long long int n, m;
  cin >> n >> m;
  long long int arr[m];
  for (long long int i = 0; i < m; i++) cin >> arr[i];
  sort(arr, arr + m);
  long long int ans = INT_MAX;
  for (long long int i = 0; i < m - n + 1; i++)
    ans = min(ans, (arr[i + n - 1] - arr[i]));
  cout << ans << endl;
}
