#include <bits/stdc++.h>
using namespace std;
vector<int> vec[100002];
int main() {
  int n, m;
  scanf("%d", &n), scanf("%d", &m);
  int arr[m];
  for (int i = (0); i < (m); ++i) scanf("%d", &arr[i]);
  for (int i = (0); i < (m); ++i) {
    if (i > 0 && arr[i - 1] != arr[i]) vec[arr[i - 1]].push_back(arr[i]);
    if (i < m - 1 && arr[i + 1] != arr[i]) vec[arr[i + 1]].push_back(arr[i]);
  }
  long long int ans = 0, sum = 0;
  for (int i = (0); i < (100002); ++i) {
    if (!vec[i].size()) continue;
    sort(vec[i].begin(), vec[i].end());
    int y = vec[i][vec[i].size() >> 1];
    long long int before = 0, after = 0;
    for (int j = (0); j < (vec[i].size()); ++j) {
      before += abs(i - vec[i][j]);
      after += abs(y - vec[i][j]);
    }
    ans = max(ans, before - after);
    sum += before;
  }
  sum /= 2;
  printf("%lld\n", sum - ans);
  return 0;
}
