#include <bits/stdc++.h>
using namespace std;
long long int n, m, a[100007], sum[100007];
vector<long long int> adj[100007];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long int i = 1; i <= m; ++i) cin >> a[i];
  for (long long int i = 1; i <= m; ++i) {
    if (i > 1 && a[i] != a[i - 1])
      adj[a[i]].push_back(a[i - 1]), sum[a[i]] += a[i - 1];
    if (i < m && a[i] != a[i + 1])
      adj[a[i]].push_back(a[i + 1]), sum[a[i]] += a[i + 1];
  }
  long long int ans = 0;
  for (long long int i = 1; i <= n; ++i) {
    if (!(long long int)adj[i].size()) continue;
    sort(adj[i].begin(), adj[i].end());
    long long int avg = adj[i][(long long int)adj[i].size() / 2], res = 0;
    avg = min(avg, n);
    avg = max(avg, 1ll);
    for (long long int j : adj[i]) {
      res += abs(j - avg) - abs(i - j);
    }
    ans = min(ans, res);
  }
  long long int cur = 0;
  for (long long int i = 2; i <= m; ++i) {
    cur += abs(a[i] - a[i - 1]);
  }
  cout << cur + ans << '\n';
  return 0;
}
