#include <bits/stdc++.h>
using namespace std;
struct _ {
  ios_base::Init i;
  _() {
    cin.sync_with_stdio(0);
    cin.tie(0);
  }
} _;
int main() {
  int X, N;
  cin >> X >> N;
  vector<long long> A(N);
  long long res = 0LL;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i >= 1) res += abs(A[i] - A[i - 1]);
  }
  map<long long, vector<long long> > M;
  for (int i = 0; i < N; i++) {
    if (i - 1 >= 0 && A[i] != A[i - 1]) M[A[i]].push_back(A[i - 1]);
    if (i + 1 < N && A[i] != A[i + 1]) M[A[i]].push_back(A[i + 1]);
  }
  long long ans = 0LL;
  for (__typeof(M.begin()) i = M.begin(); i != M.end(); i++) {
    sort((i->second).begin(), (i->second).end());
    long long new_i;
    if (((int)(i->second).size()) % 2 == 1) {
      long long new_i = i->second[((int)(i->second).size()) / 2];
      long long my_ans = 0LL;
      for (int j = 0; j < ((int)(i->second).size()); j++) {
        long long prev = abs(i->second[j] - i->first);
        long long next = abs(i->second[j] - new_i);
        my_ans += (next - prev);
      }
      ans = min(ans, my_ans);
    } else {
      long long new_j = i->second[((int)(i->second).size()) / 2] +
                        i->second[((int)(i->second).size()) / 2 - 1];
      long long new_i = new_j / 2;
      long long my_ans = 0LL;
      for (int j = 0; j < ((int)(i->second).size()); j++) {
        long long prev = abs(i->second[j] - i->first);
        long long next = abs(i->second[j] - new_i);
        my_ans += (next - prev);
      }
      ans = min(ans, my_ans);
      if (new_j % 2 == 1) {
        new_i = new_j / 2 + 1;
        my_ans = 0;
        for (int j = 0; j < ((int)(i->second).size()); j++) {
          long long prev = abs(i->second[j] - i->first);
          long long next = abs(i->second[j] - new_i);
          my_ans += (next - prev);
        }
        ans = min(ans, my_ans);
      }
    }
  }
  cout << res + ans << endl;
  return 0;
}
