#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n >> n;
  vector<long long> aa(n), a;
  map<long long, map<long long, long long>> mp;
  for (long long i = 0; i < n; ++i) {
    cin >> aa[i];
  }
  for (long long i = 0; i < n; ++i) {
    if (a.empty())
      a.push_back(aa[i]);
    else if (a.back() != aa[i])
      a.push_back(aa[i]);
  }
  n = a.size();
  if (n == 1) {
    cout << "0\n";
    return 0;
  }
  long long sum = 0;
  mp[a[0]][a[1]]++;
  mp[a[n - 1]][a[n - 2]]++;
  sum += abs(a[0] - a[1]);
  sum += abs(a[n - 1] - a[n - 2]);
  for (long long i = 1; i < n - 1; ++i) {
    mp[a[i]][a[i + 1]]++;
    mp[a[i]][a[i - 1]]++;
    sum += abs(a[i] - a[i + 1]);
    sum += abs(a[i] - a[i - 1]);
  }
  sum /= 2;
  long long ans = sum;
  for (auto w : mp) {
    long long x = w.first;
    vector<long long> Yfs, Ys, Yf, pref, pref2;
    for (auto y : w.second)
      Yfs.push_back(y.first * y.second), Ys.push_back(y.second),
          Yf.push_back(y.first);
    partial_sum(Ys.begin(), Ys.end(), back_inserter(pref2));
    partial_sum(Yfs.begin(), Yfs.end(), back_inserter(pref));
    long long sum2 = 0;
    long long Ysum = 0;
    for (auto y : w.second)
      sum2 += abs(x - y.first) * y.second, Ysum += y.second;
    for (long long i = 0; i < Yfs.size(); ++i) {
      long long l = i - 1, r = i + 1;
      long long cntl = (l != -1 ? pref2[l] : 0),
                cntr = (pref2[Yfs.size() - 1] - pref2[r - 1]);
      ans = min(ans, sum - sum2 + (-cntr + cntl) * Yf[i] -
                         (l != -1 ? pref[l] : 0) +
                         (pref[Yfs.size() - 1] - pref[r - 1]));
    }
  }
  cout << ans << "\n";
  return 0;
}
