#include <bits/stdc++.h>
using namespace std;
const long long LLINF = 1e18;
const int INF = 1e9;
const double EPS = 1e-7;
inline const int &INT() {
  static int x;
  scanf("%d", &x);
  return x;
}
inline const long long &LONG() {
  static long long x;
  scanf("%lld", &x);
  return x;
}
template <class T>
inline void MIN(T &a, const T &b) {
  if (b < a) a = b;
}
template <class T>
inline void MAX(T &a, const T &b) {
  if (b > a) a = b;
}
const int NN = 1e5 + 5;
vector<int> adj[NN];
int N, M;
long long ans = 0, S = 0;
int main() {
  N = INT();
  M = INT();
  int v = 0;
  for (int zz = 0; zz < (M); ++zz) {
    int u = INT();
    if (v && u != v) {
      adj[u].push_back(v);
      adj[v].push_back(u);
      S += abs(u - v);
    }
    v = u;
  }
  ans = S;
  for (int i = (1); i <= (N); ++i)
    if (adj[i].size()) {
      long long x = 0, y = 0;
      for (__typeof((adj[i]).begin()) j = (adj[i]).begin(); j != (adj[i]).end();
           ++j)
        x += abs(*j - i);
      sort(adj[i].begin(), adj[i].end());
      int t = adj[i][adj[i].size() / 2];
      for (__typeof((adj[i]).begin()) j = (adj[i]).begin(); j != (adj[i]).end();
           ++j)
        y += abs(*j - t);
      MIN(ans, S - x + y);
    }
  cout << ans << endl;
  return 0;
}
