#include <bits/stdc++.h>
using namespace std;
int N, M, P[100005];
vector<int> V[100005];
int main() {
  cin >> N >> M;
  for (int i = 1; i <= M; i++) cin >> P[i];
  for (int i = 1; i <= M; i++) {
    if (i > 1 && P[i - 1] != P[i]) V[P[i]].push_back(P[i - 1]);
    if (i < M && P[i + 1] != P[i]) V[P[i]].push_back(P[i + 1]);
  }
  long long orig = 0;
  for (int i = 2; i <= M; i++) orig += abs(P[i] - P[i - 1]);
  for (int i = 1; i < 100005; i++) sort(V[i].begin(), V[i].end());
  long long ans = orig;
  vector<long long> pre(100005, 0), suf(100005, 0);
  for (int n = 1; n < 100005; n++) {
    if (V[n].empty()) continue;
    int sz = V[n].size();
    pre[0] = V[n][0];
    suf[sz - 1] = V[n][sz - 1];
    for (int i = 1; i < sz; i++) pre[i] = pre[i - 1] + V[n][i];
    for (int i = sz - 2; i >= 0; i--) suf[i] = suf[i + 1] + V[n][i];
    long long before = 0;
    for (int i = 0; i < sz; i++) before += abs(n - V[n][i]);
    for (long long i = 0; i < sz; i++) {
      long long to = V[n][i];
      long long now = 0;
      if (i > 0) now += i * to - pre[i - 1];
      if (i < sz) now += suf[i] - to * (sz - i);
      ans = min(ans, orig + now - before);
    }
  }
  cout << ans;
}
