#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 10;
int a[N];
vector<int> v[N];
long long all;
void update(long long &best, int pos) {
  if ((int)v[pos].size() == 0) return;
  long long temp = all;
  int i, j;
  for (i = 0; i < (int)v[pos].size(); i++) temp -= abs(pos - v[pos][i]);
  int l = (int)v[pos].size();
  int cur = v[pos][l / 2];
  for (i = 0; i < (int)v[pos].size(); i++) temp += abs(cur - v[pos][i]);
  if (temp < best) best = temp;
}
int main() {
  int i, j;
  int n, m;
  cin >> n >> m;
  for (i = 1; i <= m; i++) scanf(" %d", a + i);
  long long best = 0;
  for (i = 1; i < m; i++) best += (long long)abs(a[i + 1] - a[i]);
  for (i = 1; i < m; i++)
    if (a[i] != a[i + 1]) {
      v[a[i + 1]].push_back(a[i]);
      v[a[i]].push_back(a[i + 1]);
    }
  all = best;
  for (i = 1; i <= n; i++) sort(v[i].begin(), v[i].end());
  for (i = 1; i <= n; i++) update(best, i);
  cout << best << endl;
}
