#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 500000;
int want, n;
int t[MAXN];
int l[MAXN], r[MAXN], c[MAXN];
priority_queue<pair<int, pair<int, int> > > pq;
void run() {
  scanf("%d%d", &want, &n);
  for (int i = (0); i < (n); ++i) scanf("%d", &t[i]);
  sort(t, t + n);
  for (int i = (0); i < (n); ++i) l[i] = i == 0 ? -1 : i - 1;
  for (int i = (0); i < (n); ++i) r[i] = i == n - 1 ? -1 : i + 1;
  pq = priority_queue<pair<int, pair<int, int> > >();
  for (int i = (0); i < (n); ++i)
    if (r[i] != -1)
      c[i] = t[r[i]] - t[i], pq.push(make_pair(-c[i], make_pair(i, r[i])));
  int ret = 0;
  for (int k = (0); k < (want); ++k) {
    while (!pq.empty() && r[pq.top().second.first] != pq.top().second.second)
      pq.pop();
    assert(!pq.empty());
    int cur = -pq.top().first, a = pq.top().second.first,
        b = pq.top().second.second;
    pq.pop();
    ret += cur;
    int aa = l[a], bb = r[b];
    if (aa != -1) l[a] = r[aa] = -1;
    if (bb != -1) r[b] = l[bb] = -1;
    if (aa == -1 || bb == -1) continue;
    int cc = c[aa] + c[b] - cur;
    r[aa] = bb, l[bb] = aa, c[aa] = cc;
    pq.push(make_pair(-cc, make_pair(aa, bb)));
  }
  printf("%d\n", ret);
}
int main() {
  run();
  return 0;
}
