#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
vector<long long> g[N];
int n, m;
int a[N];
long long d[N];
void inline add_edge(int u, int v) {
  g[u].push_back(v);
  d[u] += abs(u - v);
  g[v].push_back(u);
  d[v] += abs(u - v);
}
int main() {
  cin >> n >> m;
  long long s = 0;
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
    if (i > 1 && a[i] != a[i - 1]) {
      add_edge(a[i], a[i - 1]);
      s += abs(a[i] - a[i - 1]);
    }
  }
  long long ans = s;
  for (int i = 1; i <= n; i++) {
    sort(g[i].begin(), g[i].end());
    int sz = g[i].size();
    if (sz == 0) continue;
    long long mid = g[i][sz / 2];
    long long t = 0;
    for (int j = 0; j < sz; j++) t += abs(mid - g[i][j]);
    ans = min(ans, (s - d[i] + t));
  }
  cout << ans << endl;
}
