#include <bits/stdc++.h>
using namespace std;
int n, m, a[100000], hd[100001], el, b[100001], ta[100001];
long long ans;
struct E {
  int v, p;
} e[1000000];
void ins(int i, int d) {
  while (d < 100001) {
    if (b[d] != i) {
      b[d] = i;
      ta[d] = 0;
    }
    ta[d]++;
    d += d & -d;
  }
}
int qry(int i, int d) {
  int c = 0;
  while (d) {
    if (b[d] != i) {
      b[d] = i;
      ta[d] = 0;
    }
    c += ta[d];
    d -= d & -d;
  }
  return c;
}
void adde(int u, int v) {
  e[el].v = v;
  e[el].p = hd[u];
  hd[u] = el++;
}
void init() {
  int i, j, k, l, d;
  int h, mid;
  long long mx, mm;
  memset(hd, -1, sizeof hd);
  memset(b, -1, sizeof b);
  ans = el = 0;
  for (i = 0; i < m; i++) {
    scanf("%d", a + i);
    adde(a[i], i);
  }
  if (m < 3) return;
  for (i = 0; i < m - 1; i++) ans += abs(a[i] - a[i + 1]);
  for (i = 1, mx = 0; i <= n; i++) {
    if (hd[i] == -1) continue;
    for (l = hd[i], h = k = 0; l != -1; l = e[l].p) {
      j = e[l].v;
      if (j > 0) {
        if (a[j - 1] > a[j])
          h++;
        else if (a[j - 1] < a[j])
          k++;
      }
      if (j < m - 1) {
        if (a[j + 1] > a[j])
          h++;
        else if (a[j + 1] < a[j])
          k++;
      }
    }
    if (h == k) continue;
    for (l = hd[i]; l != -1; l = e[l].p) {
      j = e[l].v;
      if (j > 0 && a[j - 1] != a[j]) ins(i, a[j - 1]);
      if (j < m - 1 && a[j + 1] != a[j]) ins(i, a[j + 1]);
    }
    k = qry(i, 100000) + 1 >> 1;
    for (l = 1, h = 100000; l < h;) {
      mid = l + h >> 1;
      if (qry(i, mid) >= k)
        h = mid;
      else
        l = mid + 1;
    }
    d = l;
    for (l = hd[i], mm = 0; l != -1; l = e[l].p) {
      j = e[l].v;
      if (j > 0 && a[j - 1] != a[j])
        mm += abs(a[j] - a[j - 1]) - abs(d - a[j - 1]);
      if (j < m - 1 && a[j + 1] != a[j])
        mm += abs(a[j] - a[j + 1]) - abs(d - a[j + 1]);
    }
    mx = max(mx, mm);
  }
  ans = min(ans, ans - mx);
}
void solve() {
  if (m < 3) {
    puts("0");
    return;
  }
  printf("%lld\n", ans);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    init();
    solve();
  }
  return 0;
}
