#include <bits/stdc++.h>
using namespace std;
long long aDist[100000];
long long aDist2[100000];
long long bestDif;
long long sm;
int main() {
  int N, M;
  vector<vector<int> > aNei;
  cin >> N >> M;
  int i;
  int page;
  vector<int> aPages;
  for (i = 0; i < N; i++) aNei.push_back(aPages);
  cin >> page;
  page--;
  aPages.push_back(page);
  for (i = 1; i < M; i++) {
    cin >> page;
    page--;
    aPages.push_back(page);
    if (aPages[i] != aPages[i - 1]) {
      aNei[aPages[i]].push_back(aPages[i - 1]);
      aNei[aPages[i - 1]].push_back(aPages[i]);
      aDist[aPages[i]] += abs(aPages[i] - aPages[i - 1]);
      aDist[aPages[i - 1]] += abs(aPages[i] - aPages[i - 1]);
    }
  }
  sm = 0;
  bestDif = 0;
  for (i = 0; i < N; i++) {
    sort(aNei[i].begin(), aNei[i].end());
    int sz = aNei[i].size();
    for (int j = 0; j < sz; j++) aDist2[i] += abs(aNei[i][j] - aNei[i][sz / 2]);
    sm += aDist[i];
    if (bestDif < (aDist[i] - aDist2[i])) bestDif = aDist[i] - aDist2[i];
  }
  cout << sm / 2 - bestDif << endl;
}
