#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int n, m, a[N];
vector<int> b[N];
long long cost(int x, int y) {
  long long ans(0);
  for (int p : b[x]) {
    if (p != 1 && a[p - 1] != x) ans += abs(a[p - 1] - x) - abs(a[p - 1] - y);
    if (p != m && a[p + 1] != x) ans += abs(a[p + 1] - x) - abs(a[p + 1] - y);
  }
  return ans;
}
long long calc(int x) {
  int l(1), r(n), m;
  while (l <= r) {
    m = l + r >> 1;
    if (cost(x, m) > cost(x, m + 1))
      r = m - 1;
    else
      l = m + 1;
  }
  return cost(x, l);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) cin >> a[i], b[a[i]].push_back(i);
  long long ans(0);
  for (int i = 1; i < m; ++i) ans += abs(a[i] - a[i + 1]);
  long long ma(0);
  for (int x = 1; x <= n; ++x) ma = max(ma, calc(x));
  cout << ans - ma;
}
