#include <bits/stdc++.h>
using namespace std;
long long n, m, a[100010];
vector<long long> v[100010];
long long b[100010];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int M;
  cin >> n >> M;
  m = 0;
  long long pre = 1070000000LL;
  for (int i = 0; i < (M); i++) {
    long long A;
    cin >> A;
    if (A != pre) {
      a[m++] = A;
    }
    pre = A;
  }
  if (m == 1) return cout << 0, 0;
  long long s = 0;
  for (int i = 0; i < (m - 1); i++) s += abs(a[i + 1] - a[i]);
  for (int i = 0; i < (m); i++) {
    if (i == 0) {
      v[a[i]].push_back(a[i + 1]);
      b[a[i]] += abs(a[i + 1] - a[i]);
    } else if (i == m - 1) {
      v[a[i]].push_back(a[i - 1]);
      b[a[i]] += abs(a[i] - a[i - 1]);
    } else {
      v[a[i]].push_back(a[i + 1]);
      v[a[i]].push_back(a[i - 1]);
      b[a[i]] += abs(a[i + 1] - a[i]);
      b[a[i]] += abs(a[i] - a[i - 1]);
    }
  }
  long long ma = 0;
  for (int i = 0; i < (100010); i++)
    if (((int)(v[i]).size())) {
      long long res = 0;
      sort(v[i].begin(), v[i].end());
      long long mi = v[i][((int)(v[i]).size()) / 2];
      for (int j = 0; j < (((int)(v[i]).size())); j++) {
        res += abs(v[i][j] - mi);
      }
      ma = max(ma, b[i] - res);
    }
  cout << s - ma << endl;
}
