#include <bits/stdc++.h>
using namespace std;
const int N = 500050;
long long input() {
  long long f = 1, s = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    s = s * 10 + c - '0';
    c = getchar();
  }
  return s * f;
}
long long n, m, a[N], ans;
int pre[N], nxt[N];
bool p[N];
priority_queue<pair<long long, int> > q;
void work() {
  for (; q.top().first != -a[q.top().second];) q.pop();
  int x = q.top().second, l = pre[x], r = nxt[x];
  ans += a[x];
  q.pop();
  pre[nxt[x] = nxt[r]] = x;
  nxt[pre[x] = pre[l]] = x;
  a[x] = (l && r ? min((long long)(1e18), a[l] + a[r] - a[x]) : (1e18));
  a[l] = a[r] = (1e18);
  q.push(make_pair(-a[x], x));
}
int main() {
  m = input();
  n = input();
  for (int i = 1; i <= n; ++i) a[i] = input();
  sort(a + 1, a + n + 1);
  for (int i = 1; i < n; ++i)
    a[i] = (a[i + 1] - a[i]), pre[i] = i - 1, nxt[i] = i + 1;
  nxt[n - 1] = 0;
  for (int i = 1; i < n; ++i) q.push(make_pair(-a[i], i));
  long long res = 0;
  for (; m--;) {
    work();
  }
  cout << ans << endl;
  return 0;
}
