#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<vector<int> > fact(n + 1);
  vector<int> num(m);
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d", &num[i]);
    if (i > 0 && num[i] != num[i - 1]) {
      ans += abs(num[i] - num[i - 1]);
      fact[num[i]].push_back(num[i - 1]);
      fact[num[i - 1]].push_back(num[i]);
    }
  }
  long long x = 100000000;
  int size1 = fact.size();
  for (int i = 0; i < size1; i++) {
    sort(fact[i].begin(), fact[i].end());
    long long v = 0;
    int size2 = fact[i].size(), mid = size2 / 2;
    for (int j = 0; j < size2; j++)
      v = v + abs(fact[i][mid] - fact[i][j]) - abs(i - fact[i][j]);
    x = min(x, v);
  }
  cout << ans + x;
}
