#include <bits/stdc++.h>
using namespace std;
int a[100000];
vector<int> adjNos[100001];
vector<long long int> diff;
int main() {
  int n;
  int m;
  long long int sum = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    if (i < m - 1) {
      if (a[i] != a[i + 1]) {
        adjNos[a[i]].push_back(a[i + 1]);
        adjNos[a[i + 1]].push_back(a[i]);
      }
    }
  }
  long long int beforeSum;
  long long int afterSum;
  int vlen;
  int median;
  for (int i = 1; i <= n; i++) {
    vlen = adjNos[i].size();
    if (vlen == 0) {
      continue;
    }
    sort(adjNos[i].begin(), adjNos[i].end());
    median = adjNos[i][vlen / 2];
    beforeSum = 0;
    afterSum = 0;
    for (int j = 0; j < vlen; j++) {
      beforeSum += abs(i - adjNos[i][j]);
      afterSum += abs(median - adjNos[i][j]);
    }
    diff.push_back(beforeSum - afterSum);
    sum += beforeSum;
  }
  sum /= 2;
  if (diff.size() != 0) {
    sum -= *max_element(diff.begin(), diff.end());
  }
  cout << sum << endl;
  return 0;
}
