#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 123;
vector<long long> nei[maxn];
long long a[maxn];
long long precost[maxn];
long long n, m;
long long minn;
long long ans;
inline long long qabs(long long a) { return (a > 0) ? (a) : (-a); }
int main() {
  memset(precost, 0, sizeof(precost));
  cin >> n >> m;
  ans = 0;
  for (long long i = 0; i < m; ++i) {
    int ta;
    scanf("%d", &ta);
    a[i] = ta;
    if (i > 0 && a[i] != a[i - 1]) {
      long long t = qabs(a[i] - a[i - 1]);
      precost[a[i]] += t;
      precost[a[i - 1]] += t;
      ans += t;
      nei[a[i]].push_back(a[i - 1]);
      nei[a[i - 1]].push_back(a[i]);
    }
  }
  minn = ans;
  for (long long i = 1; i <= n; ++i)
    if (!nei[i].empty()) {
      long long l = 1, r = n;
      vector<long long>& tv = nei[i];
      while (l + 2 < r) {
        long long mid1 = (l + r) / 2;
        long long mid2 = (mid1 + r) / 2;
        long long res1 = 0, res2 = 0;
        for (int j = 0; j < tv.size(); ++j) {
          res1 += qabs(mid1 - tv[j]);
          res2 += qabs(mid2 - tv[j]);
        }
        if (res1 > res2) {
          l = mid1;
        } else {
          r = mid2;
        }
      }
      long long res = 0;
      for (int j = 0; j < tv.size(); ++j) res += qabs(l - tv[j]);
      if ((ans - (precost[i] - res)) < minn) minn = ans - (precost[i] - res);
      res = 0;
      for (int j = 0; j < tv.size(); ++j) res += qabs(r - tv[j]);
      if ((ans - (precost[i] - res)) < minn) minn = ans - (precost[i] - res);
      long long mid = (l + r) / 2;
      res = 0;
      for (int j = 0; j < tv.size(); ++j) res += qabs(mid - tv[j]);
      if ((ans - (precost[i] - res)) < minn) minn = ans - (precost[i] - res);
    }
  cout << minn << endl;
  return 0;
}
