#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
  }
  int j = 1;
  for (int i = 1; i < m; ++i) {
    if (a[i] != a[j - 1]) {
      a[j++] = a[i];
    }
  }
  a.resize(j);
  m = a.size();
  vector<vector<int> > neigbours(n + 1);
  for (int i = 0; i < m; ++i) {
    int pi = i - 1;
    if (pi >= 0) {
      neigbours[a[i]].push_back(a[pi]);
    }
    int ni = i + 1;
    if (ni < m) {
      neigbours[a[i]].push_back(a[ni]);
    }
  }
  for (int i = 0; i <= n; ++i) {
    sort(neigbours[i].begin(), neigbours[i].end());
  }
  vector<long long> sums(n + 1, 0);
  long long total_sum = 0;
  for (int num = 0; num <= n; ++num) {
    for (int j = 0; j < neigbours[num].size(); ++j) {
      sums[num] += abs(num - neigbours[num][j]);
    }
    total_sum += sums[num];
  }
  total_sum /= 2;
  long long best_total_sum = total_sum;
  for (int num = 0; num <= n; ++num) {
    int sz = neigbours[num].size();
    if (sz == 0) {
      continue;
    }
    int mid = sz / 2;
    int nv = neigbours[num][mid];
    long long new_sum = 0;
    for (int i = 0; i < sz; ++i) {
      new_sum += abs(neigbours[num][i] - nv);
    }
    best_total_sum = min(best_total_sum, total_sum - (sums[num] - new_sum));
  }
  cout << best_total_sum << endl;
  return 0;
}
