#include <bits/stdc++.h>
using namespace std;
long long a[100005], c[100005];
vector<pair<long long, long long>> p[100005];
long long check(long long x, long long i) {
  long long v = 0;
  for (auto j : p[i]) {
    long long v1 = j.first, v2 = j.second;
    if (v1 != -1) v += abs(v1 - x);
    if (v2 != -1) v += abs(v2 - x);
  }
  return v;
}
int main() {
  long long n, m, sum = 0;
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    scanf("%lld", &a[i]);
  }
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  a[0] = a[1], a[m + 1] = a[m];
  for (long long i = 1; i <= m; i++) {
    long long x = a[i - 1], y = a[i + 1];
    if (x == a[i]) x = -1;
    if (y == a[i]) y = -1;
    p[a[i]].push_back({min(x, y), max(x, y)});
  }
  for (long long i = 2; i <= m; i++) {
    sum += abs(a[i] - a[i - 1]);
  }
  for (long long i = 1; i <= n; i++) {
    c[i] = check(i, i);
  }
  long long res = 2e18;
  for (long long i = 1; i <= n; i++) {
    if (p[i].size() == 0) continue;
    long long l = 1, r = n, ans = 2e18;
    while (r >= l) {
      long long midl = l + (r - l) / 3, numl = check(midl, i);
      long long midr = r - (r - l) / 3, numr = check(midr, i);
      if (numl <= numr)
        r = midr - 1, ans = min(ans, numl);
      else
        l = midl + 1, ans = min(ans, numr);
    }
    res = min(res, sum - c[i] + ans);
  }
  cout << res << endl;
}
