#include <bits/stdc++.h>
using namespace std;
int n, m, a[100000 + 1];
vector<int> b[100000 + 1];
int main() {
  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(b[i][j] - i);
      after += abs(b[i][j] - y);
    }
    ans = min(ans, after - before);
    sum += before;
  }
  cout << sum / 2 + ans;
  return 0;
}
