#include <bits/stdc++.h>
using namespace std;
struct node {
  long long end, next;
} g[110000 * 2];
long long ans, mid, n, m, j, tem, i, sum, t, tot, a[110000], b[110000],
    fa[110000];
void add(long long a, long long b) {
  g[++tot].end = b;
  g[tot].next = fa[a];
  fa[a] = tot;
}
int main() {
  scanf("%I64d%I64d", &n, &m);
  for (i = 1; i <= m; i++) scanf("%I64d", &a[i]);
  for (i = 1; i <= m; i++) {
    if (i - 1 > 0)
      if (a[i] != a[i - 1]) add(a[i], a[i - 1]);
    if (i + 1 <= m)
      if (a[i + 1] != a[i]) add(a[i], a[i + 1]);
    if (i != 1) sum += abs(a[i] - a[i - 1]);
  }
  ans = 1ll * 110000 * 110000;
  for (i = 1; i <= n; i++) {
    t = 0;
    tem = sum;
    for (j = fa[i]; j; j = g[j].next) b[++t] = g[j].end;
    sort(b + 1, b + t + 1);
    mid = b[t / 2 + 1];
    for (j = 1; j <= t; j++) tem -= abs(i - b[j]);
    for (j = 1; j <= t; j++) tem += abs(mid - b[j]);
    if (tem < ans) ans = tem;
  }
  printf("%I64d\n", ans);
  return 0;
}
