#include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159265358979323846264338327950;
const double M = pow(2, 32);
const long long int modinv = 5 * (pow(10, 8)) + 4;
struct Compare {
  constexpr bool operator()(pair<int, int> const& a,
                            pair<int, int> const& b) const noexcept {
    return (a.first > b.first) || (a.first == b.first && a.second > b.second);
  }
};
int main() {
  long long int n, m, i;
  cin >> n >> m;
  vector<long long int> arr(m);
  for (long long int i = 0; i < m; i++) cin >> arr[i];
  vector<vector<long long int>> adj(n + 1);
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (i = 0; i < m; i++) {
    if (i == 0) {
      if (arr[i + 1] != arr[i]) adj[arr[i]].push_back(arr[i + 1]);
    } else if (i == m - 1) {
      if (arr[i - 1] != arr[i]) adj[arr[i]].push_back(arr[i - 1]);
    } else {
      if (arr[i - 1] != arr[i]) adj[arr[i]].push_back(arr[i - 1]);
      if (arr[i + 1] != arr[i]) adj[arr[i]].push_back(arr[i + 1]);
    }
  }
  long long int sum = 0, ans = 0;
  for (i = 1; i <= n; i++) {
    if (adj[i].size() == 0) continue;
    sort(adj[i].begin(), adj[i].end());
    long long int prev = 0, now = 0;
    long long int j = adj[i][(adj[i].size()) / 2];
    for (long long int k = 0; k < adj[i].size(); k++) {
      prev += abs(i - adj[i][k]);
      now += abs(j - adj[i][k]);
    }
    ans = max(ans, prev - now);
    sum += prev;
  }
  cout << sum / 2 - ans << endl;
}
