#include <bits/stdc++.h>
#pragma comment(linker, "/stack:256000000")
using namespace std;
const int N = 100100;
int a[N];
vector<int> v[N];
long long sum[N];
int main() {
  int n, m;
  cin >> n >> m;
  for (int(i) = 0; (i) < (m); (i)++) scanf("%d", &a[i]);
  long long total = 0;
  for (int(i) = 0; (i) < (m - 1); (i)++) total += abs(a[i + 1] - a[i]);
  for (int(i) = 0; (i) < (m); (i)++) {
    int x = a[i];
    if (i > 0 && a[i - 1] != x) {
      v[x].push_back(a[i - 1]);
      sum[x] += abs(x - a[i - 1]);
    }
    if (i < m - 1 && a[i + 1] != x) {
      v[x].push_back(a[i + 1]);
      sum[x] += abs(x - a[i + 1]);
    }
  }
  long long best = total;
  for (int i = 1; i <= n; i++) {
    if (!v[i].empty()) {
      sort((v[i]).begin(), (v[i]).end());
      int t = v[i][(int)(v[i]).size() / 2];
      long long cur = total - sum[i];
      for (int(j) = 0; (j) < ((int)(v[i]).size()); (j)++) {
        cur += abs(v[i][j] - t);
      }
      best = min(best, cur);
    }
  }
  cout << best << endl;
  return 0;
}
