#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:512000000")
using namespace std;
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline string tostr(const T &x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
inline long long parse(const string &s) {
  stringstream ss(s);
  long long x;
  ss >> x;
  return x;
}
const long double EPS = 1e-12;
const int INF = 1000 * 1000 * 1000;
const char CINF = 102;
const long long LINF = INF * 1ll * INF;
const long double DINF = 1e200;
const long double PI = 3.1415926535897932384626433832795l;
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long powmod(long long a, long long p, long long m) {
  long long r = 1;
  while (p) {
    if (p & 1) r = r * a % m;
    p >>= 1;
    a = a * a % m;
  }
  return r;
}
int main() {
  int u, n;
  cin >> u >> n;
  long long add = 0;
  int p = 0;
  map<int, vector<long long> > M;
  for (int i = 0; i < (n); ++i) {
    int a;
    scanf("%d", &a);
    if (i && a != p) {
      M[a].push_back(p - a);
      M[p].push_back(a - p);
      add += abs(a - p);
    }
    p = a;
  }
  long long res = 0;
  for (auto &it : M) {
    vector<long long> &v = it.second;
    sort((v).begin(), (v).end());
    long long r = 0;
    for (int i = 0; i < (((int)(v).size())); ++i) {
      r += abs(v[i]) - abs(v[i] - v[((int)(v).size()) / 2]);
    }
    if (r < 0) exit(123);
    res = max(res, r);
  }
  cout << add - res;
  return 0;
}
