#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> p(m, 0);
  for (auto& i : p) cin >> i;
  sort(p.begin(), p.end());
  int ans = 1e9 + 1;
  for (int i = 0; i < m - n + 1; ++i) {
    ans = min(ans, p[i + n - 1] - p[i]);
  }
  cout << ans;
}
