#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int MX = 500004;
const int INF = 1e9 + 9;
int k, n;
int a[MX];
int l[MX];
int r[MX];
set<pair<int, int> > s;
int pre[MX], pro[MX];
int ja[MX];
void build() {
  for (int i = 1; i < n; i++) {
    pro[i] = pro[i - 1];
    pre[i] = pre[i - 1];
    if (i % 2 == 0) {
      pro[i] += a[i] - a[i - 1];
    } else {
      pre[i] += a[i] - a[i - 1];
    }
  }
  for (int i = 0; i < n - 1; i++) r[i] = i + 1;
  r[n - 1] = n - 1;
  for (int i = 1; i < n; i++) l[i] = i - 1;
  l[0] = 0;
  for (int i = 0; i < n - 1; i++) {
    s.insert({a[i + 1] - a[i], i});
    ja[i] = a[i + 1] - a[i];
  }
}
int main() {
  cin >> k >> n;
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  sort(a, a + n);
  build();
  int sum = 0;
  while (k--) {
    pair<int, int> p = *s.begin();
    s.erase(s.begin());
    if (p.first != ja[p.second]) {
      k++;
      continue;
    }
    int le = p.second, ri = r[p.second];
    sum += p.first;
    ja[ri] = -1;
    if (l[le] == le) {
      l[r[ri]] = r[ri];
      continue;
    }
    if (r[ri] == ri) {
      r[l[le]] = l[le];
      continue;
    }
    int x = l[le], y = r[ri];
    r[x] = y;
    l[y] = x;
    int val = 0;
    if (x % 2 == 0) {
      val += pre[y] - pre[x];
      val -= pro[y] - pro[x];
    } else {
      val += pro[y] - pro[x];
      val -= pre[y] - pre[x];
    }
    ja[x] = val;
    s.insert({val, x});
  }
  cout << sum;
}
