#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, limit;
  cin >> limit;
  cin >> n;
  int last;
  cin >> last;
  long long sum = 0;
  vector<int> neib[100000 + 1];
  for (; n > 1; n--) {
    int tmp;
    cin >> tmp;
    if (tmp == last) continue;
    sum += abs(tmp - last);
    neib[tmp].push_back(last);
    neib[last].push_back(tmp);
    last = tmp;
  }
  long long maxoff = 0;
  for (int i = 1; i <= limit; i++) {
    if (neib[i].size() == 0) continue;
    sort(neib[i].begin(), neib[i].end());
    int mid = neib[i][neib[i].size() / 2];
    long long off = 0;
    for (int j = 0; j < neib[i].size(); j++)
      off += abs(neib[i][j] - i) - abs(neib[i][j] - mid);
    maxoff = max(maxoff, off);
  }
  cout << sum - maxoff << endl;
  return 0;
}
