#include <bits/stdc++.h>
using namespace std;
int a[100005];
vector<int> V[100005];
int main() {
  int n, m, i, j;
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(V, 0, sizeof(V));
    long long ans = 0;
    for (i = 1; i <= m; i++) {
      scanf("%d", &a[i]);
      if (i != 1) {
        ans += abs(a[i] - a[i - 1]);
        if (a[i] != a[i - 1]) {
          V[a[i - 1]].push_back(a[i]);
          V[a[i]].push_back(a[i - 1]);
        }
      }
    }
    long long tt = 0;
    for (i = 1; i <= 100000; i++) {
      if (V[i].size()) {
        sort(V[i].begin(), V[i].end());
        int to = V[i][V[i].size() / 2];
        long long sum1 = 0, sum2 = 0;
        for (j = 0; j < V[i].size(); j++) {
          sum1 += abs(i - V[i][j]);
          sum2 += abs(to - V[i][j]);
        }
        tt = max(tt, sum1 - sum2);
      }
    }
    printf("%I64d\n", ans - tt);
  }
  return 0;
}
