#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
int L[MAXN], R[MAXN];
long long jump[MAXN], S[MAXN], A[MAXN];
int root(int a, int *par) {
  if (a == par[a]) return a;
  return par[a] = root(par[a], par);
}
long long getCost(pair<int, int> a) {
  long long ca = jump[a.second - 1];
  if (a.first > 1) ca -= jump[a.first - 1];
  return S[a.second] - S[a.first - 1] - 2 * ca;
}
struct cmp {
  bool operator()(pair<int, int> a, pair<int, int> b) {
    long long ca = getCost(a);
    long long cb = getCost(b);
    if (ca != cb) return ca < cb;
    return a < b;
  }
};
int tim[MAXN];
int main() {
  int K, N;
  scanf("%d%d", &K, &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &tim[i]);
  }
  sort(tim, tim + N);
  --N;
  set<pair<int, int>, cmp> pq;
  for (int i = 1; i <= N; ++i) {
    A[i] = tim[i] - tim[i - 1];
    S[i] = S[i - 1] + A[i];
    jump[i] = (i > 1 ? jump[i - 2] : 0) + A[i];
    R[i] = i;
    L[i] = i;
    pq.insert({i, i});
  }
  R[N + 1] = N + 1;
  long long ans = 0;
  for (int i = 1; i <= K; ++i) {
    auto x = *pq.begin();
    pq.erase(x);
    int l, r;
    tie(l, r) = x;
    ans += getCost({l, r});
    int l2 = root(l - 1, L);
    int r2 = root(r + 1, R);
    L[l] = l - 1;
    R[l - 1] = l;
    R[r] = r + 1;
    L[r + 1] = r;
    pq.erase({l2, l - 1});
    pq.erase({r + 1, r2});
    if (l2 >= 1 && r2 <= N) {
      pq.insert({l2, r2});
    }
  }
  printf("%lld\n", ans);
}
