#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> v1(m);
  for (int i = 0; i < m; i++) {
    cin >> v1[i];
  }
  int x = 1000000;
  sort((v1).begin(), (v1).end());
  for (int i = 0; i < m - n + 1; i++) {
    x = min(x, v1[i + n - 1] - v1[i]);
  }
  cout << x << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  solve();
}
