#include <bits/stdc++.h>
long long mpow(long long a, long long n, long long mod) {
  long long ret = 1;
  long long b = a;
  while (n) {
    if (n & 1) ret = (ret * b) % mod;
    b = (b * b) % mod;
    n >>= 1;
  }
  return (long long)ret;
}
using namespace std;
long long a[100003];
vector<int> b[100001];
int main() {
  int i, j, n, m;
  cin >> n >> m;
  for (j = 1; j <= m; j++) {
    cin >> a[j];
  }
  for (j = 1; j <= m; j++) {
    if (j > 1 && a[j] != a[j - 1]) b[a[j - 1]].push_back(a[j]);
    if (j <= m - 1 && a[j] != a[j + 1]) b[a[j + 1]].push_back(a[j]);
  }
  long long ans = 0, sum = 0;
  for (i = 1; i <= n; i++) {
    if (b[i].empty()) continue;
    sort(b[i].begin(), b[i].end());
    long long y = b[i][b[i].size() >> 1];
    long long bef = 0, af = 0;
    for (j = 0; j < b[i].size(); j++) {
      bef += abs(b[i][j] - i);
      af += abs(b[i][j] - y);
    }
    ans = max(ans, bef - af);
    sum += bef;
  }
  cout << sum / 2 - ans;
  return 0;
}
