#include <bits/stdc++.h>
using namespace std;
const double eps(1e-8);
int n, m;
vector<long long> seq[100010];
long long a[100010];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%I64d", &a[i]);
  }
  long long initial = 0;
  a[0] = a[1];
  a[m + 1] = a[m];
  for (int i = 1; i <= m; i++) {
    initial += abs(a[i] - a[i - 1]);
  }
  for (int i = 1; i <= m; i++) {
    if (i - 1 != 0) {
      if (a[i] != a[i - 1]) seq[a[i]].push_back(a[i - 1]);
    }
    if (i != m) {
      if (a[i] != a[i + 1]) seq[a[i]].push_back(a[i + 1]);
    }
  }
  long long tmp;
  long long less;
  long long ini;
  long long answer = initial;
  for (int i = 1; i <= n; i++) {
    if (!seq[i].empty()) {
      sort(seq[i].begin(), seq[i].end());
      less = 0;
      ini = 0;
      tmp = seq[i][(seq[i].size() + 1) / 2 - 1];
      for (int j = 0; j <= seq[i].size() - 1; j++) {
        ini += abs(i - seq[i][j]);
        less += abs(tmp - seq[i][j]);
      }
      less = initial - ini + less;
      answer = min(answer, less);
    }
  }
  printf("%I64d\n", answer);
  return 0;
}
