#include <bits/stdc++.h>
template <typename T>
inline T Max(T a, T b) {
  if (a > b) return a;
  return b;
}
int n, m;
std::vector<int> b[100000 + 10];
bool vis[100000 + 10];
int a[100000 + 10], maxp, cnt, maxn;
long long before, after, sum = 0, delta = 0;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    if (i > 1) sum += abs(a[i] - a[i - 1]);
  }
  for (int i = 1; i <= m; i++) {
    if (i - 1 >= 1 && a[i - 1] != a[i]) b[a[i]].push_back(a[i - 1]);
    if (i + 1 <= m && a[i + 1] != a[i]) b[a[i]].push_back(a[i + 1]);
  }
  for (int i = 1, midn; i <= n; i++)
    if (b[i].size()) {
      std::sort(b[i].begin(), b[i].end());
      midn = b[i][b[i].size() >> 1];
      before = after = 0;
      for (int j = 0; j < b[i].size(); j++) {
        before += abs(i - b[i][j]);
        after += abs(midn - b[i][j]);
      }
      delta = Max(delta, before - after);
    }
  printf("%I64d", sum - delta);
  return 0;
}
