#include <bits/stdc++.h>
using namespace std;
int a[150000];
vector<int> v[150000];
long long sum[150000];
int main() {
  int n, m, t;
  cin >> n >> m;
  if (m <= 2) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    if (i == 0) {
      if (a[0] != a[1]) v[a[0]].push_back(a[1]);
      sum[a[0]] += abs(a[0] - a[1]);
    } else if (i == m - 1) {
      if (a[i] != a[i - 1]) v[a[i]].push_back(a[i - 1]);
      sum[a[i]] += abs(a[i] - a[i - 1]);
      ans += abs(a[i] - a[i - 1]);
    } else {
      if (a[i] != a[i - 1]) v[a[i]].push_back(a[i - 1]);
      sum[a[i]] += abs(a[i] - a[i - 1]);
      if (a[i] != a[i + 1]) v[a[i]].push_back(a[i + 1]);
      sum[a[i]] += abs(a[i] - a[i + 1]);
      ans += abs(a[i] - a[i - 1]);
    }
  }
  long long maxx = 0;
  for (int i = 1; i <= n; i++) {
    if (v[i].size() > 0) {
      sort(v[i].begin(), v[i].end());
      t = v[i].size() / 2;
      t = v[i][t];
      long long sum1 = 0;
      for (int j = 0; j < v[i].size(); j++) {
        sum1 += abs(v[i][j] - t);
      }
      if (sum[i] - sum1 > maxx) maxx = sum[i] - sum1;
    }
  }
  cout << ans - maxx << endl;
  return 0;
}
