#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  deque<long long> arr(m);
  deque<long long> diff(m);
  map<long long, deque<long long>> pos;
  for (long long i = 0; i < m; i++) {
    cin >> arr[i];
    pos[arr[i]].push_back(i);
  }
  long long sumdiff = 0;
  for (long long i = 0; i < (m - 1); i++) {
    diff[i] = fabs(arr[i + 1] - arr[i]);
    sumdiff += diff[i];
  }
  long long ans = UINT_MAX;
  for (auto it = pos.begin(); it != pos.end(); it++) {
    long long i = (*it).first;
    {
      deque<long long> adj;
      long long tot = sumdiff;
      for (deque<long long>::iterator j = ((*it).second.begin());
           j != ((*it).second.end()); j++) {
        long long x = *j;
        if (x != 0 && arr[x - 1] != i) {
          adj.push_back(arr[x - 1]);
          tot -= diff[x - 1];
        }
        if (x != (m - 1) && arr[x + 1] != i) {
          adj.push_back(arr[x + 1]);
          tot -= diff[x];
        }
      }
      if (adj.size() != 0) {
        sort(adj.begin(), adj.end());
        long long mid = adj[(adj.size() - 1) >> 1];
        if (mid == i) mid = adj[1 + ((adj.size() - 1) >> 1)];
        for (long long j = 0; j < adj.size(); j++) tot += (fabs(adj[j] - mid));
      } else
        tot = 0;
      if (ans > tot) ans = tot;
    }
  }
  cout << ans << endl;
  return 0;
}
