#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void mn(T& x, T& y) {
  if (y < x) x = y;
}
template <class T>
inline void mx(T& x, T& y) {
  if (x < y) x = y;
}
template <class T>
inline int sz(const T& x) {
  return x.size();
}
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];
    --a[i];
  }
  vector<vector<int> > to(n);
  for (int i = 0; i + 1 < m; ++i) {
    if (a[i + 1] != a[i]) {
      to[a[i]].push_back(a[i + 1]);
      to[a[i + 1]].push_back(a[i]);
    }
  }
  int bestIndex = 0;
  int bestValue = 0;
  long long bestDelta = 0;
  for (int i = 0; i < n; ++i) {
    if (to[i].empty()) {
      continue;
    }
    long long score1 = 0;
    long long score2 = 0;
    sort(to[i].begin(), to[i].end());
    int mid = to[i][sz(to[i]) / 2];
    for (int x : to[i]) {
      score1 += abs(i - x);
      score2 += abs(mid - x);
    }
    long long delta = score2 - score1;
    if (delta < bestDelta) {
      bestDelta = delta;
      bestIndex = i;
      bestValue = mid;
    }
  }
  for (int& x : a) {
    if (x == bestIndex) {
      x = bestValue;
    }
  }
  long long s = 0;
  for (int i = 0; i + 1 < m; ++i) {
    s += abs(a[i] - a[i + 1]);
  }
  cout << s << endl;
  return 0;
}
