#include <bits/stdc++.h>
using namespace std;
void read_file(bool outToFile = true) {}
int n, m;
int A[100000 + 9];
map<int, map<int, int> > G;
long long tot;
long long f(int nw, int i) {
  long long ret = tot;
  for (map<int, int>::iterator it = G[i].begin(); it != G[i].end(); it++) {
    int bj = it->second;
    int aj = it->first;
    ret += 1LL * bj * (abs(aj - nw) - abs(aj - i));
  }
  return ret;
}
int main() {
  read_file();
  while (scanf("%d", &n) != EOF) {
    scanf("%d", &m);
    for (int i = 0; i < m; i++) scanf("%d", &A[i]);
    G.clear();
    long long ans = 0;
    for (int i = 0; i < m - 1; i++) {
      int u = A[i], v = A[i + 1];
      if (u == v) continue;
      G[u][v]++;
      G[v][u]++;
      ans += abs(u - v);
    }
    tot = ans;
    for (int i = 1; i <= n; i++) {
      int lo = 1, hi = n;
      while (lo != hi) {
        int md = (lo + hi) / 2;
        if (f(md, i) >= f(md + 1, i))
          lo = md + 1;
        else
          hi = md;
      }
      ans = min(ans, f(lo, i));
    }
    printf("%lld\n", ans);
  }
}
