#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 77;
int n, m, a[N];
long long A, B;
vector<int> V[N];
int main() {
  scanf("%d %d", &m, &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), V[a[i]].push_back(i);
  for (int i = 2; i <= n; ++i) A += abs(a[i] - a[i - 1]);
  B = A;
  for (int i = 1; i <= m; ++i) {
    vector<int> C;
    long long now = 0;
    for (int x : V[i]) {
      if (x > 1 && a[x - 1] != a[x])
        now -= abs(a[x] - a[x - 1]), C.push_back(a[x - 1]);
      if (x < n && a[x + 1] != a[x])
        now -= abs(a[x] - a[x + 1]), C.push_back(a[x + 1]);
    }
    sort(C.begin(), C.end());
    int sz = C.size();
    if (C.empty()) continue;
    int best = C[(sz - 1) / 2];
    for (int x : V[i]) {
      if (x > 1 && a[x - 1] != a[x]) now += abs(best - a[x - 1]);
      if (x < n && a[x + 1] != a[x]) now += abs(best - a[x + 1]);
    }
    B = min(B, A + now);
  }
  printf("%lld\n", B);
  return 0;
}
