#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k, n, m, id, mem1, mem2, sz;
  long long sum = 0, mx = 0, cnt1, cnt2;
  cin >> n >> m;
  int a[m];
  for (i = 0; i < m; i++) cin >> a[i];
  if (m == 1) {
    cout << 0;
    return 0;
  }
  for (i = 0; i < m - 1; i++) sum += abs(a[i] - a[i + 1]);
  vector<int> v[n + 1];
  for (i = 0; i < m - 1; i++) {
    if (a[i] != a[i + 1]) {
      v[a[i]].push_back(a[i + 1]);
      v[a[i + 1]].push_back(a[i]);
    }
  }
  for (i = 1; i <= n; i++) {
    sz = v[i].size();
    if (sz == 0) continue;
    sz /= 2;
    sort(v[i].begin(), v[i].end());
    mem1 = i;
    mem2 = v[i][sz];
    cnt1 = 0;
    cnt2 = 0;
    for (j = 0; j < v[i].size(); j++) {
      cnt1 += abs(v[i][j] - mem1);
      cnt2 += abs(v[i][j] - mem2);
    }
    mx = max(mx, cnt1 - cnt2);
  }
  sum += -mx;
  cout << sum;
}
