#include <bits/stdc++.h>
using namespace std;
int n, m, a[100000 + 1];
vector<int> b[100000 + 1];
int main(int argc, char *argv[]) {
  ios ::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) cin >> a[i];
  for (int i = 1; i <= m; ++i) {
    if (i > 1 && a[i - 1] != a[i]) b[a[i - 1]].push_back(a[i]);
    if (i < m && a[i + 1] != a[i]) b[a[i + 1]].push_back(a[i]);
  }
  long long ans = 0LL, sum = 0LL;
  for (int i = 1; i <= n; ++i) {
    if (!b[i].size()) continue;
    sort(b[i].begin(), b[i].end());
    int y = b[i][b[i].size() >> 1];
    long long before = 0LL, after = 0LL;
    for (int j = 0; j < b[i].size(); ++j) {
      before += abs(i - b[i][j]);
      after += abs(y - b[i][j]);
    }
    ans = max(ans, before - after), sum += before;
  }
  cout << sum / 2 - ans << endl;
  fclose(stdin);
  fclose(stdout);
  return 0;
}
