#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int BIG = 1e9 + 555;
const int maxN = 100 * 1000 + 9;
int n, m;
long long a[maxN], sum;
vector<long long> adj[maxN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    if (i - 1 > 0) {
      if (a[i] != a[i - 1]) {
        adj[a[i]].push_back(i - 1);
      }
    }
    if (i + 1 <= m) {
      if (a[i] != a[i + 1]) {
        adj[a[i]].push_back(i + 1);
      }
    }
  }
  for (int i = 1; i + 1 <= m; i++) {
    sum += abs(a[i] - a[i + 1]);
  }
  long long fRes = sum;
  for (int i = 1; i <= n; i++) {
    if (adj[i].empty()) {
      continue;
    }
    for (int j = 0, L = adj[i].size(); j < L; j++) {
      int idx = adj[i][j];
      adj[i][j] = a[idx];
    }
    sort(adj[i].begin(), adj[i].end());
    int len = adj[i].size() / 2;
    long long med = adj[i][len];
    long long delta = 0;
    for (int j = 0, L = adj[i].size(); j < L; j++) {
      delta -= abs(i - adj[i][j]);
      delta += abs(med - adj[i][j]);
    }
    fRes = min(fRes, sum + delta);
  }
  cout << fRes << '\n';
  return 0;
}
