#include <bits/stdc++.h>
using namespace std;
int n, m;
long long int arr[100050];
vector<vector<long long int>> v(100050);
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) cin >> arr[i];
  for (int i = 1; i <= m; i++) {
    if (i > 1 && arr[i - 1] != arr[i]) v[arr[i]].push_back(arr[i - 1]);
    if (i < m && arr[i] != arr[i + 1]) v[arr[i]].push_back(arr[i + 1]);
  }
  long long int sum = 0, ans;
  for (int i = 1; i < m; i++) sum += abs(arr[i] - arr[i + 1]);
  ans = sum;
  for (int i = 1; i <= n; i++) {
    if (v[i].size() == 0) continue;
    sort(v[i].begin(), v[i].end());
    long long int y = v[i][v[i].size() / 2];
    long long int before = 0, after = 0;
    for (int x : v[i]) {
      before += abs(x - i);
      after += abs(y - x);
    }
    ans = min(ans, sum - before + after);
  }
  cout << ans;
  return 0;
}
