#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;
}
int arr[N], cnt;
int quickSelect(int l, int r, int k) {
  int m = arr[rand() % (r - l + 1) + l];
  int i(l), j(r);
  while (true) {
    while (arr[i] < m) ++i;
    while (arr[j] > m) --j;
    if (i >= j) break;
    swap(arr[i++], arr[j--]);
  }
  if (i == j) {
    if (i == k) return *max_element(arr + l, arr + k + 1);
    ++i, --j;
  }
  if (j >= k) return quickSelect(l, j, k);
  return quickSelect(i, r, k);
}
long long calc(int x) {
  cnt = 0;
  long long delta(0);
  for (int p : b[x]) {
    if (p != 1 && a[p - 1] != x)
      arr[++cnt] = a[p - 1], delta += abs(a[p - 1] - x);
    if (p != m && a[p + 1] != x)
      arr[++cnt] = a[p + 1], delta += abs(a[p + 1] - x);
  }
  if (cnt) {
    int y = quickSelect(1, cnt, cnt / 2 + 1);
    for (int i = 1; i <= cnt; ++i) delta -= abs(y - arr[i]);
  }
  return delta;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  srand(time(NULL));
  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)
    if (!b[x].empty()) ma = max(ma, calc(x));
  cout << ans - ma;
}
