#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, m, k, s, t, a[N], b[N], l[N], r[N];
long long ans;
pair<long long, int> u;
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    q;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  sort(a + 1, a + 1 + n);
  for (int i = 1; i < n; ++i) a[i] = a[i + 1] - a[i];
  --n;
  for (int i = 1; i <= n; ++i) l[i] = i - 1, r[i] = i + 1;
  for (int i = 1; i <= n; ++i) q.push({a[i], i});
  while (m) {
    u = q.top();
    q.pop();
    if (b[u.second]) continue;
    k = u.second;
    ans += a[k];
    s = l[k];
    t = r[k];
    if (s != 0 && t != n + 1) {
      b[s] = 1;
      l[k] = l[s];
      r[l[k]] = k;
      b[t] = 1;
      r[k] = r[t];
      l[r[k]] = k;
      a[k] = a[s] + a[t] - a[k];
      q.push({a[k], k});
    } else {
      b[s] = b[t] = 1;
      s = l[s];
      t = r[t];
      l[s] = t;
      r[t] = s;
    }
    --m;
  }
  cout << ans;
}
