#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long arr[m];
  vector<long long> v[n + 1];
  vector<long long> sum(n + 1, 0);
  long long cnt = 0;
  for (int i = 0; i < m; i++) cin >> arr[i];
  for (int i = 0; i < m; i++) {
    if (i < m - 1) cnt += abs(arr[i] - arr[i + 1]);
    if (i > 0 and arr[i - 1] != arr[i]) {
      v[arr[i]].push_back(arr[i - 1]);
      sum[arr[i]] += abs(arr[i] - arr[i - 1]);
    }
    if (i + 1 < m and arr[i + 1] != arr[i]) {
      v[arr[i]].push_back(arr[i + 1]);
      sum[arr[i]] += abs(arr[i] - arr[i + 1]);
    }
  }
  long long ans = LLONG_MAX;
  for (int i = 1; i <= n; i++) {
    if (((int)(v[i]).size())) {
      sort(v[i].begin(), v[i].end());
      long long temp = v[i][(((int)(v[i]).size()) - 1) / 2];
      long long x = 0;
      for (auto j : v[i]) {
        x += abs(temp - j);
      }
      ans = min(ans, cnt - sum[i] + x);
    }
  }
  cout << (ans == LLONG_MAX ? 0 : ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  solve();
}
