#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
long long ret, sum, ans;
int a[maxn], n, m, b[maxn], cnt, pos;
vector<int> v[maxn];
void close() { exit(0); }
void init() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    v[a[i]].push_back(i);
  }
  for (int i = 1; i <= m - 1; i++) sum += abs(a[i] - a[i + 1]);
  ret = sum;
  for (int i = 1; i <= n; i++) {
    ans = sum;
    cnt = 0;
    for (int j = 0; j < (int)v[i].size(); j++) {
      pos = v[i][j];
      if (pos != 1 && a[pos - 1] != a[pos]) {
        ans -= abs(a[pos - 1] - a[pos]);
        b[++cnt] = a[pos - 1];
      }
      if (pos != m && a[pos + 1] != a[pos]) {
        ans -= abs(a[pos + 1] - a[pos]);
        b[++cnt] = a[pos + 1];
      }
    }
    sort(b + 1, b + cnt + 1);
    for (int j = 1; j <= cnt; j++) ans += abs(b[j] - b[(cnt + 1) / 2]);
    ret = min(ret, ans);
  }
  printf("%I64d\n", ret);
}
int main() {
  init();
  close();
  return 0;
}
