#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  long long a[100005];
  vector<long long> adj[100005];
  int s;
  bool nonzero = false;
  long long total = 0, median, turns, medturns, best = LLONG_MAX;
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> a[i];
  for (int i = 0; i < m - 1; i++) {
    if (a[i + 1] != a[i]) adj[a[i]].push_back(a[i + 1]);
    total += llabs(a[i + 1] - a[i]);
  }
  for (int i = 1; i < m; i++) {
    if (a[i - 1] != a[i]) adj[a[i]].push_back(a[i - 1]);
  }
  for (int i = 1; i <= n; i++) {
    sort(adj[i].begin(), adj[i].end());
    s = adj[i].size();
    if (s == 0) continue;
    nonzero = true;
    if (s % 2 == 1)
      median = adj[i][s / 2];
    else
      median = (adj[i][s / 2 - 1] + adj[i][s / 2]) / 2;
    turns = 0;
    medturns = 0;
    for (int j = 0; j < s; j++) {
      turns += llabs(((long long)i) - adj[i][j]);
      medturns += llabs(median - adj[i][j]);
    }
    if (total - turns + medturns < best) best = total - turns + medturns;
  }
  if (nonzero)
    cout << best << endl;
  else
    cout << "0" << endl;
  return 0;
}
