#include <bits/stdc++.h>
using namespace std;
const long double Eps = 1e-8;
inline bool is_zero(long double x) { return (x >= -Eps) && (x <= Eps); }
const int MaxN = 100005, MaxM = 100005;
int tab[MaxM], N, M;
vector<int> occur[MaxN];
void input() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d", &tab[i]);
    occur[tab[i]].push_back(i);
  }
}
long long sum = 0, best = 0;
int main() {
  input();
  for (int i = 0; i < M - 1; i++) sum += abs(tab[i] - tab[i + 1]);
  best = sum;
  for (int i = 1; i <= N; i++) {
    vector<int> poss;
    for (int v : occur[i]) {
      if (v && tab[v - 1] != tab[v]) poss.push_back(tab[v - 1]);
      if (v != M - 1 && tab[v + 1] != tab[v]) poss.push_back(tab[v + 1]);
    }
    long long sumDecr = 0, sumZero = 0;
    int deg = -poss.size(), last = 0;
    for (int v : occur[i]) {
      if (v && tab[v - 1] != tab[v]) {
        sumDecr += abs(tab[v] - tab[v - 1]);
        sumZero += tab[v - 1];
      }
      if (v != M - 1 && tab[v + 1] != tab[v]) {
        sumDecr += abs(tab[v] - tab[v + 1]);
        sumZero += tab[v + 1];
      }
    }
    sort(poss.begin(), poss.end());
    long long sumPrim = sum - sumDecr + sumZero;
    for (int v : poss) {
      int diff = v - last;
      sumPrim += (long long)deg * diff;
      best = min(best, sumPrim);
      last = v;
      deg += 2;
    }
  }
  printf("%I64d\n", best);
  return 0;
}
