#include <bits/stdc++.h>
using namespace std;
long long check(long long y, vector<long long>& x) {
  long long res = 0;
  for (long long i = 0; i < (long long)x.size(); i++) res += abs(x[i] - y);
  return res;
}
signed main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> v(m);
  for (long long i = 0; i < m; i++) cin >> v[i];
  v.erase(unique(v.begin(), v.end()), v.end());
  long long h = v.size();
  long long sum = 0;
  for (long long i = 0; i < h - 1; i++) sum += abs(v[i] - v[i + 1]);
  if (h == 1) {
    cout << 0 << endl;
    return 0;
  }
  map<long long, vector<long long> > mv;
  mv[v[0]].push_back(v[1]);
  for (long long i = 1; i < h - 1; i++) {
    mv[v[i]].push_back(v[i - 1]);
    mv[v[i]].push_back(v[i + 1]);
  }
  mv[v[h - 1]].push_back(v[h - 2]);
  long long ans = sum;
  set<long long> si;
  for (long long i = 0; i < h; i++) {
    if (si.count(v[i])) continue;
    si.insert(v[i]);
    vector<long long> x = mv[v[i]];
    long long sub = check(v[i], x);
    long long res = sub;
    long long l = 0, r = n;
    res = min(res, check(l, x));
    res = min(res, check(r, x));
    while (l + 2 < r) {
      long long m1 = l + (r - l) / 3, m2 = l + (r - l) * 2 / 3;
      long long r1 = check(m1, x), r2 = check(m2, x);
      res = min(res, r1);
      res = min(res, r2);
      res = min(res, check(m1 - 1, x));
      res = min(res, check(m1 + 1, x));
      res = min(res, check(m2 - 1, x));
      res = min(res, check(m2 + 1, x));
      if (r1 < r2)
        r = m2;
      else
        l = m1;
    }
    res = min(res, check(l, x));
    res = min(res, check(l + 1, x));
    res = min(res, check(r - 1, x));
    res = min(res, check(r, x));
    ans = min(ans, sum - sub + res);
  }
  cout << ans << endl;
  return 0;
}
