#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 100000000;
inline int fastMin(int x, int y) { return x > y ? y : x; }
inline int fastMax(int x, int y) {
  return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y;
}
inline int fastAbs(int x) { return ((x >> (32 - 1)) & (x ^ (-x))) ^ x; }
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
int n, m;
int a[((int)1e5)];
vector<int> v[((int)1e5) + 1];
int main() {
  cin >> n >> m;
  for (int i = 0; i < (m); i++) cin >> a[i];
  long long base = 0;
  for (int i = 0; i < (m - 1); i++) base += abs(a[i + 1] - a[i]);
  for (int i = 0; i < (m - 1); i++) {
    if (a[i] == a[i + 1]) continue;
    v[a[i]].push_back(a[i + 1]);
    v[a[i + 1]].push_back(a[i]);
  }
  long long max_diff = 0;
  for (int i = 1; i <= n; i++) {
    if (((int)(v[i]).size()) == 0) continue;
    sort(v[i].begin(), v[i].end());
    int val = v[i][((int)(v[i]).size()) / 2];
    int j = 0;
    long long diff = 0;
    for (int j = 0; j < ((int)(v[i]).size()); j++) {
      int nw_d = abs(v[i][j] - val);
      int pre_d = abs(v[i][j] - i);
      diff += pre_d - nw_d;
    }
    max_diff = max(max_diff, diff);
  }
  cout << base - max_diff << endl;
}
