#include <bits/stdc++.h>
using namespace std;
int i, j, k, N, M, K;
vector<int> b[100005];
int a[100005], c[100005];
long long sum = 0;
int main() {
  memset(b, 0, sizeof(b));
  sum = 0;
  scanf("%d%d", &M, &N);
  for (i = 1; i <= N; i++) {
    scanf("%d", &a[i]);
    if (i >= 2) sum += abs(a[i - 1] - a[i]);
  }
  for (i = 2; i <= N; i++) {
    b[a[i]].push_back(a[i - 1]);
    b[a[i - 1]].push_back(a[i]);
  }
  long long ret = sum;
  for (i = 1; i <= 100000; i++) {
    int len = 0;
    if (b[i].size() == 0) continue;
    for (j = 0; j <= b[i].size() - 1; j++) {
      if (b[i][j] != i) {
        c[++len] = b[i][j];
      }
    }
    sort(c + 1, c + 1 + len);
    long long opt = c[(len + 1) / 2], d = 0;
    for (j = 1; j <= len; j++) {
      d += abs(opt - c[j]) - abs(i - c[j]);
    }
    ret = min(ret, sum + d);
  }
  printf("%I64d\n", ret);
}
