#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 100;
vector<int> v[maxn];
int arr[maxn];
int main() {
  int n, m;
  long long temp = 0, _min, _beg = 0;
  int _max = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &arr[i]);
    _max = max(arr[i], _max);
    if (i != 0 && arr[i] != arr[i - 1]) {
      v[arr[i - 1]].push_back(arr[i]);
      v[arr[i]].push_back(arr[i - 1]);
      _beg += abs(arr[i] - arr[i - 1]);
    }
  }
  _min = temp = _beg;
  int rev, _size;
  vector<int> k;
  for (int i = 1; i <= _max; i++) {
    temp = _beg;
    _size = v[i].size();
    if (_size == 0) continue;
    k = v[i];
    sort(k.begin(), k.end());
    rev = k[_size / 2];
    for (int j = 0; j < _size; j++) temp += (abs(rev - k[j]) - abs(i - k[j]));
    _min = min(temp, _min);
  }
  cout << _min << endl;
  return 0;
}
