#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int n, m;
long long a[maxn];
vector<long long> adj[maxn];
long long myabs(long long x) { return x < 0 ? -x : x; }
int main() {
  scanf("%d%d", &n, &m);
  long long org = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%I64d", &a[i]);
    if (i > 1 && a[i] != a[i - 1])
      adj[a[i]].push_back(a[i - 1]), adj[a[i - 1]].push_back(a[i]);
    if (i > 1) org += myabs(a[i] - a[i - 1]);
  }
  long long ans = org;
  for (int i = 1; i <= n; i++)
    if (adj[i].size() > 0) {
      sort(adj[i].begin(), adj[i].end());
      int md = adj[i][(int)adj[i].size() / 2];
      long long cur = 0, tmp = 0;
      for (int j = 0; j < adj[i].size(); j++)
        cur += myabs(i - adj[i][j]), tmp += myabs(md - adj[i][j]);
      ans = min(ans, org + tmp - cur);
    }
  cout << ans << "\n";
  return 0;
}
