#include <bits/stdc++.h>
using namespace std;
int n, m, a[100010];
vector<vector<int> > adj;
long long ans, v, d;
int main() {
  cin >> n >> m;
  adj.resize(n + 1);
  ans = 0;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    if (i && a[i] != a[i - 1])
      ans += abs(a[i] - a[i - 1]), adj[a[i]].push_back(a[i - 1]),
          adj[a[i - 1]].push_back(a[i]);
  }
  d = 1e8;
  for (int i = 0; i < adj.size(); i++) {
    sort(adj[i].begin(), adj[i].end());
    int mid = adj[i].size() / 2;
    v = 0;
    for (int j = 0; j < adj[i].size(); j++) {
      v = v + abs(adj[i][mid] - adj[i][j]) - abs(i - adj[i][j]);
    }
    d = min(d, v);
  }
  cout << ans + d;
}
