#include <bits/stdc++.h>
using namespace std;
int a[100050], b[100050];
vector<int> v[100050];
long long ans, now, tmp, sum, c[100050];
int n, m, i, j;
void inc(int x, int y) { v[x].push_back(y); }
int main() {
  scanf("%d%d", &m, &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i < n; i++) sum += abs(a[i + 1] - a[i]);
  for (i = 1; i <= n; i++) {
    if (i > 1 && a[i - 1] != a[i])
      inc(a[i], a[i - 1]), c[a[i]] += abs(a[i] - a[i - 1]);
    if (i < n && a[i + 1] != a[i])
      inc(a[i], a[i + 1]), c[a[i]] += abs(a[i] - a[i + 1]);
  }
  ans = sum;
  for (i = 1; i <= m; i++)
    if (v[i].size()) {
      for (j = 0; j < v[i].size(); j++) b[j + 1] = v[i][j];
      sort(b + 1, b + 1 + v[i].size());
      now = sum - c[i];
      tmp = b[(v[i].size() + 1) / 2];
      for (j = 1; j <= v[i].size(); j++) now += abs(tmp - b[j]);
      ans = min(ans, now);
    }
  printf("%I64d", ans);
}
