#include <bits/stdc++.h>
using namespace std;
vector<int> v[100010];
int a[100010];
long long s[400010];
inline int abs(int x) { return x > 0 ? x : -x; }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  long long S = 0;
  vector<int> w;
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
    if (i) {
      S += abs(a[i] - a[i - 1]);
    }
  }
  for (int i = 0; i < m; i++) {
    if (i > 0 && a[i - 1] != a[i]) {
      v[a[i - 1]].push_back(a[i]);
    }
    if (i < m - 1 && a[i + 1] != a[i]) {
      v[a[i + 1]].push_back(a[i]);
    }
  }
  long long ans = S;
  for (int i = 1; i <= n; i++) {
    if (!v[i].size()) continue;
    vector<int> &w = v[i];
    sort(w.begin(), w.end());
    long long b = 0;
    s[0] = 0;
    for (int j = 0; j < w.size(); j++) {
      s[j + 1] = s[j] + w[j];
      b += abs(w[j] - i);
    }
    for (int j = 0; j < w.size(); j++) {
      long long t = (long long)w[j] * j - s[j] + (s[w.size()] - s[j]) -
                    (long long)w[j] * (w.size() - j);
      ans = min(ans, S - b + t);
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
