#include <bits/stdc++.h>
using namespace std;
void solve(std::istream& in, std::ostream& out) {
  int n, m;
  in >> n >> m;
  vector<int> a;
  int prev = -1, x;
  map<int, vector<int> > all;
  long long res = 0;
  while (m-- > 0) {
    in >> x;
    if (prev != x) {
      all[x].push_back(a.size());
      if (prev > 0) res += abs(prev - x);
      a.push_back(x);
      prev = x;
    }
  }
  m = a.size();
  long long bestDiff = 0;
  for (auto it = all.begin(); it != all.end(); ++it) {
    x = it->first;
    const vector<int>& idx = it->second;
    auto cnt = [&](int y) {
      long long res = 0;
      for (int i : idx) {
        if (i > 0) res += abs(y - a[i - 1]);
        if (i < m - 1) res += abs(y - a[i + 1]);
      }
      return res;
    };
    long long best = cnt(x);
    long long cur = res - best;
    vector<int> neighbors;
    for (int i : idx) {
      if (i > 0) neighbors.push_back(a[i - 1]);
      if (i < m - 1) neighbors.push_back(a[i + 1]);
    }
    if (!neighbors.empty()) {
      std::nth_element(neighbors.begin(),
                       neighbors.begin() + neighbors.size() / 2,
                       neighbors.end());
      x = neighbors[neighbors.size() / 2];
      best = min(best, cnt(x));
    }
    cur += best;
    bestDiff = min(bestDiff, cur - res);
  }
  out << (res + bestDiff) << endl;
}
void solve(std::istream& in, std::ostream& out);
int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  solve(std::cin, std::cout);
  return 0;
}
