#include <bits/stdc++.h>
using namespace std;
const int MAX = 100100;
int a[MAX];
vector<int> ne[MAX];
int fabs(int x) {
  if (x < 0) x = -x;
  return x;
}
int main() {
  int n, m;
  int i, j;
  int tmp1, tmp2;
  long long sum1, sum2, maxdis;
  long long ans;
  while (scanf("%d %d", &n, &m) != EOF) {
    for (i = 0; i < m; i++) scanf("%d", &a[i]);
    for (i = 0; i < m - 1; i++) {
      if (a[i] != a[i + 1]) {
        ne[a[i]].push_back(a[i + 1]);
        ne[a[i + 1]].push_back(a[i]);
      }
    }
    maxdis = 0;
    ans = 0;
    for (i = 1; i <= n; i++) {
      if (!ne[i].empty()) {
        sort(ne[i].begin(), ne[i].end());
        tmp2 = ne[i].size() / 2;
        tmp1 = ne[i][tmp2];
        sum1 = 0;
        sum2 = 0;
        for (j = 0; j < ne[i].size(); j++) {
          sum1 += fabs(ne[i][j] - tmp1);
          sum2 += fabs(ne[i][j] - i);
        }
        maxdis = max(maxdis, sum2 - sum1);
        ans += sum2;
      }
    }
    printf("%I64d\n", ans / 2 - maxdis);
    for (i = 1; i <= n; i++) ne[i].clear();
  }
  return 0;
}
