#include <bits/stdc++.h>
long change, mc, a[100005], n, m, to[100005], aim;
long long ma = 0, del[100005];
struct ed {
  long nu;
  struct ed *next;
};
struct ed *p[100005];
void add(long x, long y) {
  struct ed *q;
  q = (struct ed *)malloc(sizeof(struct ed));
  q->next = p[x];
  q->nu = y;
  p[x] = q;
}
long que[100005];
void swap(long x, long y) {
  long t = que[x];
  que[x] = que[y];
  que[y] = t;
}
void qs(long x, long y) {
  long i = x, j = y, k = que[(x + y) >> 1];
  do {
    while (i <= j && que[i] < k) i++;
    while (i <= j && que[j] > k) j--;
    if (i <= j) swap(i++, j--);
  } while (i <= j);
  if (j > x) qs(x, j);
  if (i < y) qs(i, y);
}
void getdel(long i) {
  struct ed *q;
  long tot = 0, x, y;
  long long now = 0, now2 = 0;
  q = p[i];
  while (q) {
    now += abs(q->nu - i);
    que[++tot] = q->nu;
    q = q->next;
  }
  qs(1, tot);
  x = que[(1 + tot) >> 1];
  for (y = 1; y <= tot; y++) now2 += abs(que[y] - x);
  del[i] = now - now2;
  to[i] = x;
  if (del[i] > ma) {
    ma = del[i];
    aim = i;
  }
}
int main() {
  long i;
  long long ans = 0;
  scanf("%ld %ld", &n, &m);
  if (m == 1) {
    printf("0");
    return 0;
  }
  for (i = 1; i <= m; i++) scanf("%ld", &a[i]);
  for (i = 1; i <= n; i++) p[i] = NULL;
  for (i = 1; i < m; i++)
    if (a[i] != a[i + 1]) {
      add(a[i], a[i + 1]);
      add(a[i + 1], a[i]);
    }
  for (i = 1; i <= n; i++)
    if (p[i]) getdel(i);
  for (i = 1; i <= m; i++)
    if (a[i] == aim) a[i] = to[aim];
  for (i = 1; i < m; i++) ans += abs(a[i] - a[i + 1]);
  printf("%I64d", ans);
  return 0;
}
