#include <bits/stdc++.h>
using namespace std;
int Max(int a, int b) { return a > b ? a : b; }
int Min(int a, int b) { return a < b ? a : b; }
int Abs(int a) { return a < 0 ? -a : a; }
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
long long lmin(long long a, long long b) { return a < b ? a : b; }
vector<int> nb[100009];
int a[100009];
long long sump[100009];
int main() {
  int lim, n;
  long long sum = 0;
  cin >> lim >> n;
  for (int i = 1; i <= (n); i++) scanf("%d", &a[i]);
  for (int i = 1; i <= (n - 1); i++) sum += Abs(a[i + 1] - a[i]);
  a[0] = a[1];
  a[n + 1] = a[n];
  for (int i = 1; i <= n; i++) {
    if (a[i] != a[i - 1]) nb[a[i]].push_back(a[i - 1]);
    if (a[i] != a[i + 1]) nb[a[i]].push_back(a[i + 1]);
  }
  for (int i = 1; i <= (lim); i++)
    for (int j = 0; j < (nb[i].size()); j++) sump[i] += Abs(i - nb[i][j]);
  long long ans = sum;
  for (int i = 1; i <= lim; i++) {
    if (nb[i].size() == 0) continue;
    sort(nb[i].begin(), nb[i].end());
    int mid = nb[i][nb[i].size() / 2];
    long long cnt = 0;
    for (int j = 0; j < nb[i].size(); j++) {
      cnt += Abs(nb[i][j] - mid);
    }
    ans = lmin(ans, sum - sump[i] + cnt);
  }
  cout << ans << endl;
  int x;
  cin >> x;
}
