#include <bits/stdc++.h>
using namespace std;
vector<long long> c[200000];
long long cost[200000];
long long v[200000];
int main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    cin >> v[i];
    v[i]--;
  }
  m = unique(v, v + m) - v;
  long long totalCost = 0;
  for (long long i = 1; i < m; i++) {
    totalCost += abs(v[i] - v[i - 1]);
  }
  for (long long i = 0; i < m; i++) {
    long long cur = v[i];
    if (i - 1 >= 0) {
      cost[cur] += abs(v[i - 1] - cur);
      c[cur].push_back(v[i - 1]);
    }
    if (i + 1 < m) {
      cost[cur] += abs(v[i + 1] - cur);
      c[cur].push_back(v[i + 1]);
    }
  }
  long long bestCost = totalCost;
  for (long long i = 0; i < n; i++) {
    sort(c[i].begin(), c[i].end());
    if (c[i].size() == 0) continue;
    long long median = c[i][c[i].size() / 2];
    long long newCost = 0;
    for (long long j = 0; j < c[i].size(); j++) {
      newCost += abs(c[i][j] - median);
    }
    bestCost = min(bestCost, totalCost - cost[i] + newCost);
  }
  cout << bestCost << endl;
  return 0;
}
