#include <bits/stdc++.h>
using namespace std;
const long long NMAX = 1000 * 100 + 5;
long long a[NMAX], n, m, out1, maks;
vector<long long> sas[NMAX];
bool bylo[NMAX];
long long wb(long long x) { return x >= 0 ? x : -x; }
long long roznica(vector<long long> v, long long t) {
  long long tmp = 0, mini;
  for (long long i = 0; i < v.size(); ++i) tmp += wb(v[i] - t);
  long long wyn = 0;
  long long odn = v[v.size() / 2];
  for (long long i = 0; i < v.size(); ++i) wyn += wb(v[i] - odn);
  return tmp - wyn;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= m; ++i) {
    cin >> a[i];
    bylo[a[i]] = true;
    if (i > 1) out1 += wb(a[i] - a[i - 1]);
    if (i > 1) {
      if (a[i] != a[i - 1]) {
        sas[a[i - 1]].push_back(a[i]);
        sas[a[i]].push_back(a[i - 1]);
      }
    }
  }
  for (long long i = 1; i <= n; ++i) sort(sas[i].begin(), sas[i].end());
  for (long long i = 1; i <= n; ++i) {
    if (bylo[i]) {
      if (roznica(sas[i], i) > maks) maks = roznica(sas[i], i);
    }
  }
  cout << out1 - maks;
}
