#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> b(n + 5);
  vector<long long> dist(n + 5);
  vector<int> a(m + 5);
  long long now = 0, ans = 1e15;
  for (int i = 1; i <= m; ++i) cin >> a[i];
  for (int i = 1; i < m; ++i) {
    int diff = abs(a[i] - a[i + 1]);
    if (diff) {
      now += diff;
      dist[a[i]] += diff;
      dist[a[i + 1]] += diff;
      b[a[i]].push_back(a[i + 1]);
      b[a[i + 1]].push_back(a[i]);
    }
  }
  for (int i = 1; i <= n; ++i) {
    sort(b[i].begin(), b[i].end());
    long long tmp = now - dist[i];
    int sz = b[i].size();
    for (int j = 0; j < sz / 2; ++j) {
      tmp += b[i][sz - 1 - j] - b[i][j];
    }
    ans = min(ans, tmp);
  }
  cout << ans;
}
