#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[100010];
vector<int> v[100010];
void solve() {
  int i, j;
  long long total, maxval, median, before, after;
  for (i = 0; i < m; i++) {
    if (i < m - 1 && a[i] != a[i + 1]) v[a[i]].push_back(a[i + 1]);
    if (i > 0 && a[i] != a[i - 1]) v[a[i]].push_back(a[i - 1]);
  }
  total = 0;
  maxval = 0;
  for (i = 1; i <= n; i++)
    if (v[i].size()) {
      sort(v[i].begin(), v[i].end());
      median = v[i][v[i].size() / 2];
      before = 0;
      after = 0;
      for (j = 0; j < v[i].size(); j++) {
        before += abs(i - v[i][j]);
        after += abs(median - v[i][j]);
      }
      maxval = max(maxval, before - after);
      total += before;
    }
  cout << total / 2 - maxval << endl;
}
void input() {
  int i;
  scanf("%d %d", &n, &m);
  for (i = 0; i < m; i++) scanf("%d", &a[i]);
  solve();
}
int main() {
  input();
  fclose(stdin);
  fclose(stdout);
  return 0;
}
