#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9;
const double eps = 1e-9;
int main() {
  int n, m, i, j, k, minn = inf;
  cin >> n >> m;
  int a[m];
  for (i = 1; i <= m; i++) cin >> a[i];
  sort(a + 1, a + 1 + m);
  for (i = 1; i <= m - n + 1; i++) {
    minn = min(minn, a[i + n - 1] - a[i]);
  }
  cout << minn;
}
