#include <bits/stdc++.h>
using namespace std;
void test() {
  int n, m;
  cin >> n >> m;
  int a[m];
  for (int i = 0; i < m; i++) cin >> a[i];
  sort(a, a + m);
  int ans = a[n - 1] - a[0];
  for (int i = n; i < m; i++) {
    ans = min(ans, a[i] - a[i - n + 1]);
  }
  cout << ans << "\n";
}
int main() {
  int tests = 1;
  while (tests--) {
    test();
  }
}
