#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, a[N], tt;
long long ans, tot;
vector<int> V[N];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
    ans += abs(a[i] - a[i - 1]);
    if (i != 1 && a[i] != a[i - 1]) {
      V[a[i]].push_back(a[i - 1]);
      V[a[i - 1]].push_back(a[i]);
    }
  }
  ans -= a[1];
  tot = ans;
  for (int i = 1; i <= n; i++) {
    int tt = V[i].size();
    if (!tt) continue;
    sort(V[i].begin(), V[i].end());
    int tmp = V[i][tt / 2];
    long long now = tot;
    for (int j = 0; j < tt; j++) now += abs(tmp - V[i][j]) - abs(i - V[i][j]);
    ans = min(ans, now);
  }
  cout << ans << endl;
}
