#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long a[100005];
long long sum = 0;
long long tam[100005];
long long add[100005];
long long ans = 0;
long long mx = 0;
vector<long long> res[100005];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> a[i];
    mx = max(mx, a[i]);
    if (i > 1) {
      sum += abs(a[i] - a[i - 1]);
      if (a[i] == a[i - 1]) continue;
      res[a[i]].push_back(a[i - 1]);
      res[a[i - 1]].push_back(a[i]);
      tam[a[i]] += abs(a[i] - a[i - 1]);
      tam[a[i - 1]] += abs(a[i] - a[i - 1]);
    }
  }
  ans = sum;
  for (long long i = 1; i <= mx; i++) {
    if (res[i].size() == 0) continue;
    sort(res[i].begin(), res[i].end());
    long long mid = res[i].size() / 2;
    for (long long j = 0; j <= res[i].size() - 1; j++) {
      add[i] += abs(res[i][j] - res[i][mid]);
    }
    ans = min(ans, sum - tam[i] + add[i]);
  }
  cout << ans;
  return 0;
}
