#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; ++i) cin >> a[i];
  map<int, vector<int> > b;
  long long sum = 0ll;
  for (int i = 1; i < m; ++i) {
    sum += abs(a[i] - a[i - 1]);
    if (a[i] != a[i - 1]) {
      b[a[i]].push_back(a[i - 1]);
      b[a[i - 1]].push_back(a[i]);
    }
  }
  long long diff = 0;
  for (auto& c : b) {
    long long s = 0;
    int ref = c.first;
    vector<int>& v = c.second;
    for (auto d : v) s += abs(ref - d);
    sort(v.begin(), v.end());
    int n = v.size();
    long long t;
    ref = v[n / 2], t = 0;
    for (auto d : v) t += abs(ref - d);
    diff = max(diff, max(0ll, s - t));
    ref = v[(n - 1) / 2], t = 0;
    for (auto d : v) t += abs(ref - d);
    diff = max(diff, max(0ll, s - t));
  }
  cout << sum - diff << endl;
  return 0;
}
