#include <bits/stdc++.h>
using namespace std;
void Merge(int *a, int low, int high, int mid) {
  int i, j, k, temp[high - low + 1];
  i = low;
  k = 0;
  j = mid + 1;
  while (i <= mid && j <= high) {
    if (a[i] < a[j]) {
      temp[k] = a[i];
      k++;
      i++;
    } else {
      temp[k] = a[j];
      k++;
      j++;
    }
  }
  while (i <= mid) {
    temp[k] = a[i];
    k++;
    i++;
  }
  while (j <= high) {
    temp[k] = a[j];
    k++;
    j++;
  }
  for (i = low; i <= high; i++) {
    a[i] = temp[i - low];
  }
}
void MergeSort(int *a, int low, int high) {
  int mid;
  if (low < high) {
    mid = (low + high) / 2;
    MergeSort(a, low, mid);
    MergeSort(a, mid + 1, high);
    Merge(a, low, high, mid);
  }
}
int main() {
  int n, m, i, ans = 1001;
  cin >> n >> m;
  int gif[m];
  for (i = 0; i < m; i++) cin >> gif[i];
  MergeSort(gif, 0, m - 1);
  for (i = 0; i <= m - n; i++) {
    ans = min(ans, gif[i + n - 1] - gif[i]);
  }
  cout << ans;
  return 0;
}
