#include <bits/stdc++.h>
using namespace std;
long long mod = int(1e9 + 7);
vector<int> v;
int main() {
  int n, m;
  cin >> n >> m;
  int a[m + 1], num[n + 1];
  long long q[n + 1];
  memset(q, 0, sizeof(q));
  long long sum = 0, maxneg = 0;
  vector<int> v[n + 1];
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    if (i > 0) sum += abs(a[i] - a[i - 1]);
  }
  for (int i = 0; i < m; i++) {
    if (i > 0) {
      if (a[i] != a[i - 1]) {
        v[a[i]].push_back(a[i - 1]);
        q[a[i]] += abs(a[i] - a[i - 1]);
      }
    }
    if (i + 1 < m) {
      if (a[i] == a[i + 1]) continue;
      v[a[i]].push_back(a[i + 1]);
      q[a[i]] += abs(a[i] - a[i + 1]);
    }
  }
  long long tp, neg;
  for (int i = 1; i <= n; i++) {
    neg = 0;
    if (v[i].size() == 0) continue;
    sort(v[i].begin(), v[i].end());
    tp = v[i][v[i].size() / 2];
    for (int j = 0; j < v[i].size(); j++) {
      neg = neg + abs(v[i][j] - tp);
    }
    neg = max(0ll, q[i] - neg);
    maxneg = max(maxneg, neg);
  }
  cout << sum - maxneg << endl;
}
