#include <bits/stdc++.h>
using namespace std;
int n, m;
long long a[100010];
long long before[100010], dex[100010];
long long b[100010], ans, Min;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%I64d", &a[i]);
    before[i] = dex[a[i]];
    dex[a[i]] = i;
    if (i > 1) ans += abs(a[i] - a[i - 1]);
  }
  for (int i = 1; i <= n; i++) {
    long long sum = 0, x = 0;
    for (int j = dex[i]; j; j = before[j]) {
      if (j > 1 && a[j - 1] != i) {
        b[++x] = a[j - 1];
        sum += abs(b[x] - i);
      }
      if (j < m && a[j + 1] != i) {
        b[++x] = a[j + 1];
        sum += abs(b[x] - i);
      }
    }
    sort(b + 1, b + x + 1);
    long long tmp = b[x / 2 + 1];
    for (int k = 1; k <= x; k++) sum -= abs(b[k] - tmp);
    Min = max(Min, sum);
  }
  printf("%I64d\n", ans - Min);
}
