#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
const int inf = 0x3f3f3f3f;
int n, m;
long long a[maxn];
vector<long long> tmp[maxn];
long long c[maxn];
long long p[maxn];
long long num[maxn];
int main() {
  long long sum = 0;
  scanf("%d%d", &n, &m);
  long long MAX = -1;
  for (int i = 1; i <= m; ++i) {
    scanf("%lld", &a[i]);
    MAX = max(MAX, a[i]);
    if (i != 1) sum += abs(a[i] - a[i - 1]);
  }
  if (n == 1) {
    printf("%lld\n", sum);
    return 0;
  }
  for (int i = 1; i <= m; ++i) {
    if (i > 1) {
      c[a[i]] += abs(a[i - 1] - a[i]);
      if (a[i - 1] != a[i]) tmp[a[i]].push_back(a[i - 1]);
    }
    if (i < m) {
      c[a[i]] += abs(a[i + 1] - a[i]);
      if (a[i + 1] != a[i]) tmp[a[i]].push_back(a[i + 1]);
    }
  }
  for (int i = 1; i <= MAX; ++i) {
    sort(tmp[i].begin(), tmp[i].end());
  }
  for (int i = 1; i <= m; ++i) {
    if (tmp[a[i]].size() == 0) continue;
    int size = tmp[a[i]].size();
    int t = tmp[a[i]][size / 2];
    if (i > 1) {
      p[a[i]] += abs(t - (a[i - 1] == a[i] ? t : a[i - 1]));
    }
    if (i < m) {
      p[a[i]] += abs(t - (a[i + 1] == a[i] ? t : a[i + 1]));
    }
  }
  long long op = sum;
  for (int i = 1; i <= n; ++i) {
    if (c[i]) {
      sum = min(sum, op - c[i] + p[i]);
    }
  }
  printf("%lld\n", sum);
  return 0;
}
