#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int main() {
  int n, m, f;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d", &f);
    A.push_back(f);
  }
  sort(A.begin(), A.end());
  int ans = 10000;
  for (int i = 0; i < m - n + 1; ++i) {
    ans = min(ans, A[i + n - 1] - A[i]);
  }
  printf("%d\n", ans);
  return 0;
}
