#include <bits/stdc++.h>
using namespace std;
int a[100000];
long long d[100001];
vector<long long> v[100001];
int n, m;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) scanf("%d", a + i);
  for (int i = 0; i < m; i++) {
    if (i && a[i] != a[i - 1]) {
      d[a[i]] += abs(a[i] - a[i - 1]);
      v[a[i]].push_back(a[i - 1]);
    }
    if (i < m - 1 && a[i] != a[i + 1]) {
      v[a[i]].push_back(a[i + 1]);
      d[a[i]] += abs(a[i] - a[i + 1]);
    }
  }
  long long c = 0;
  for (int i = 1; i <= n; i++)
    if (v[i].size()) {
      sort(v[i].begin(), v[i].end());
      long long temp = v[i][v[i].size() / 2];
      long long ts = 0;
      for (int u = 0; u < v[i].size(); u++) ts += abs(temp - v[i][u]);
      if (d[i] - ts > c) c = d[i] - ts;
    }
  long long ans = 0;
  for (int i = 1; i <= n; i++) ans += d[i];
  ans = ans / 2;
  ans = ans - c;
  printf("%I64d\n", ans);
}
