#include <bits/stdc++.h>
using namespace std;
const int N = 500000;
const int inf = (int)1e9;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int P = 239;
const long double eps = 1e-6;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  int n, m;
  cin >> n >> m;
  int a[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<int> b[n];
  for (int i = 0; i < m - 1; i++)
    if (a[i] != a[i + 1]) {
      b[a[i]].push_back(a[i + 1]);
      b[a[i + 1]].push_back(a[i]);
    }
  long long ans = 0;
  int ans1, ans2;
  for (int i = 0; i < n; i++) {
    sort(b[i].begin(), b[i].end());
    if ((int)b[i].size() > 0) {
      int x = b[i][(int)b[i].size() / 2];
      long long cnt = 0;
      for (int j : b[i]) cnt += (abs(i - j) - abs(x - j));
      if (cnt > ans) {
        ans = cnt;
        ans1 = i;
        ans2 = x;
      }
    }
  }
  if (ans > 0)
    for (int i = 0; i < m; i++)
      if (a[i] == ans1) a[i] = ans2;
  long long out = 0;
  for (int i = 0; i < m - 1; i++) out += abs(a[i + 1] - a[i]);
  cout << out << endl;
  return 0;
}
