#include <bits/stdc++.h>
using namespace std;
long long a[100500];
vector<long long> pfx[100500], vtx[100500];
long long proc(long long x, long long sum) {
  int n = vtx[x].size();
  int mid = n / 2;
  long long res = sum;
  for (int i = mid, _n = n; i < _n; i++) res += vtx[x][i] - vtx[x][mid];
  for (int i = 0, _n = mid; i < _n; i++) res += vtx[x][mid] - vtx[x][i];
  for (int i = 0, _n = n; i < _n; i++) res -= abs(x - vtx[x][i]);
  return res;
}
int main() {
  int n, m;
  long long sum = 0LL;
  scanf("%d %d", &n, &m);
  for (int i = 0, _n = m; i < _n; i++) scanf("%I64d", a + i);
  for (int i = 1, _n = m; i < _n; i++) {
    if (a[i] == a[i - 1]) continue;
    vtx[a[i]].push_back(a[i - 1]);
    vtx[a[i - 1]].push_back(a[i]);
    sum += abs(a[i] - a[i - 1]);
  }
  for (int i = 1, _n = n + 1; i < _n; i++) sort(vtx[i].begin(), vtx[i].end());
  long long ans = sum;
  for (int i = 1, _n = n + 1; i < _n; i++) ans = min(ans, proc(i, sum));
  printf("%I64d\n", ans);
  return 0;
}
