#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)2e5 + 7;
const int INF = (int)1e9 + 7;
int n, m;
int a[MAXN];
vector<int> adj[MAXN];
int main() {
  long long ans = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
    if (i) {
      ans += abs(a[i] - a[i - 1]);
      if (a[i] != a[i - 1]) {
        adj[a[i]].push_back(a[i - 1]);
        adj[a[i - 1]].push_back(a[i]);
      }
    }
  }
  long long res = ans;
  for (int i = 1; i <= n; i++) {
    if (adj[i].empty()) continue;
    sort(adj[i].begin(), adj[i].end());
    int mid = adj[i][(adj[i].size() - 1) / 2];
    long long cur = ans;
    for (auto pos : adj[i]) {
      cur -= abs(i - pos);
      cur += abs(mid - pos);
    }
    res = min(res, cur);
  }
  cout << res;
  return 0;
}
