#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<int> v[N];
int n, m;
int a[N];
long long _abs(long long x) { return x > 0ll ? x : -x; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d", &a[i]);
  }
  long long s = 0;
  for (int i = 1; i <= m; ++i) {
    if (i > 1 && a[i] != a[i - 1]) v[a[i]].push_back(a[i - 1]);
    if (i < m && a[i] != a[i + 1]) v[a[i]].push_back(a[i + 1]);
    if (i > 1) s += _abs(a[i] - a[i - 1]);
  }
  long long ans = 1e18;
  for (int i = 1; i <= n; ++i) {
    sort(v[i].begin(), v[i].end());
    long long t0 = 0, t1 = 0;
    for (int j = 0; j < int(v[i].size()); ++j) {
      t0 += _abs(i - v[i][j]), t1 += v[i][j] - v[i][0];
    }
    for (int j = 0; j < int(v[i].size()); ++j) {
      ans = min(ans, s - t0 + t1);
      if (j + 1 < int(v[i].size())) {
        long long d = v[i][j + 1] - v[i][j];
        t1 = t1 + (2ll * j + 2ll - int(v[i].size())) * d;
      }
    }
  }
  if (ans == 1e18) ans = 0;
  cout << ans << endl;
  return 0;
}
