#include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
const int MOD = 1000000007;
const int INF = 1e8;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const long double EPS = 1e-7;
int main() {
  int n, m;
  cin >> n >> m;
  map<long long, vector<long long> > pts;
  long long res = 0, dif = 0;
  vector<long long> v(m), v2;
  for (int i = 0; i < (int)m; i++) cin >> v[i];
  v2.push_back(v[0]);
  for (int i = 1; i < m; i++) {
    if (v[i] != v2[v2.size() - 1]) {
      v2.push_back(v[i]);
    }
  }
  swap(v, v2);
  m = v.size();
  for (int i = 0; i < (int)m - 1; i++) {
    res += abs(v[i] - v[i + 1]);
  }
  for (int i = 0; i < (int)m; i++) {
    if (i - 1 >= 0) {
      pts[v[i]].push_back(v[i - 1]);
    }
    if (i + 1 < m) {
      pts[v[i]].push_back(v[i + 1]);
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (!pts.count(i)) continue;
    int size = pts[i].size();
    long long orig = 0, meio = -1, novo = 0;
    sort(pts[i].begin(), pts[i].end());
    for (long long val : pts[i]) {
      orig += (long long)abs(val - i);
    }
    meio = pts[i][size / 2];
    for (long long val : pts[i]) {
      novo += (long long)abs(val - meio);
    }
    if (novo <= orig) dif = max(dif, orig - novo);
  }
  cout << res - dif << '\n';
  assert(res - dif >= 0);
  return 0;
}
