#include <bits/stdc++.h>
using namespace std;
const int maxn = 101010;
vector<int> g[maxn];
long long c[maxn], ans, d[maxn], e[maxn], cur, tot;
int n, m, i, j, k, l, x, y, a[maxn];
int abc(int x) {
  if (x < 0) return -x;
  return x;
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= m; i++) {
    if (i > 1 && a[i] != a[i - 1]) {
      g[a[i]].push_back(a[i - 1]);
      c[a[i]] += abc(a[i] - a[i - 1]);
    }
    if (i < m && a[i] != a[i + 1]) {
      g[a[i]].push_back(a[i + 1]);
      c[a[i]] += abc(a[i] - a[i + 1]);
    }
    if (i > 1) ans += abc(a[i] - a[i - 1]);
  }
  tot = ans;
  for (i = 1; i <= n; i++) {
    if (!c[i]) continue;
    sort(g[i].begin(), g[i].end());
    d[0] = 0;
    e[(int)g[i].size() - 1] = 0;
    for (j = 1; j < g[i].size(); j++) {
      d[j] = d[j - 1] + (g[i][j] - g[i][j - 1]) * 1ll * j;
    }
    for (j = (int)g[i].size() - 2; j >= 0; j--) {
      e[j] =
          e[j + 1] + (g[i][j + 1] - g[i][j]) * 1ll * ((int)g[i].size() - j - 1);
    }
    for (j = 0; j < g[i].size(); j++) {
      ans = min(ans, tot - c[i] + d[j] + e[j]);
    }
  }
  cout << ans;
  return 0;
}
