#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int m, n;
int a[maxn];
long long b[maxn];
vector<int> G[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
  long long tot = 0, ans;
  for (int i = 2; i <= m; i++) tot += (long long)abs(a[i] - a[i - 1]);
  ans = tot;
  for (int i = 1; i <= m; i++) {
    if (i > 1 && a[i - 1] != a[i]) {
      G[a[i]].push_back(a[i - 1]);
      b[a[i]] += (long long)abs(a[i] - a[i - 1]);
    }
    if (i < m && a[i + 1] != a[i]) {
      G[a[i]].push_back(a[i + 1]);
      b[a[i]] += (long long)abs(a[i] - a[i + 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!G[i].size()) continue;
    sort(G[i].begin(), G[i].end());
    long long d = G[i][G[i].size() / 2], tmp = 0;
    for (int j = 0; j < (int)G[i].size(); j++) {
      tmp += (long long)abs(d - G[i][j]);
    }
    ans = min(ans, tot - (b[i] - tmp));
  }
  printf("%lld", ans);
  return 0;
}
