#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int f[m];
  for (int i = 0; i < m; i++) cin >> f[i];
  sort(f, f + m);
  int ans = f[n - 1] - f[0];
  for (int i = 1; i <= m - n; i++) {
    ans = min(ans, f[i + (n - 1)] - f[i]);
  }
  cout << ans;
}
