#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
int n, m, a[MAXN];
vector<int> e[MAXN];
long long Tohka, s[MAXN], Mashiro;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> a[i];
    if (i == 1) continue;
    if (a[i] != a[i - 1])
      e[a[i - 1]].push_back(a[i]), e[a[i]].push_back(a[i - 1]);
    Tohka += abs(a[i] - a[i - 1]);
    s[a[i]] += abs(a[i] - a[i - 1]);
    s[a[i - 1]] += abs(a[i] - a[i - 1]);
  }
  Mashiro = Tohka;
  for (int i = 1; i <= n; ++i) {
    vector<int> &v = e[i];
    sort(v.begin(), v.end());
    if (!v.size()) continue;
    int p = (v.size()) / 2;
    long long t = 0;
    for (int j = 0; j < (int)v.size(); ++j) t += abs(v[j] - v[p]);
    Tohka = min(Tohka, Mashiro - s[i] + t);
  }
  cout << Tohka << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
