#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, m, b[100005] = {};
  long long int z = 0, c[100005] = {}, x = 0;
  vector<int> a[100005] = {};
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> b[i];
  for (int i = 0; i < m - 1; i++) {
    if (b[i] != b[i + 1])
      a[b[i]].push_back(b[i + 1]), a[b[i + 1]].push_back(b[i]);
    z += abs(b[i + 1] - b[i]);
  }
  for (int i = 1; i <= n; i++) sort(a[i].begin(), a[i].end());
  for (int i = 1; i <= n; i++) {
    if (a[i].empty()) continue;
    long long int y = 0;
    int l = a[i][a[i].size() / 2];
    for (int j : a[i]) y += abs(l - j) - abs(i - j);
    x = min(x, y);
  }
  cout << z + x;
}
