#include <bits/stdc++.h>
using namespace std;
const int maxn = 100050;
int n, m, A[maxn];
long long ans;
long long sum = 0;
vector<int> q[maxn];
int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) scanf("%d", &A[i]);
  for (i = 1; i < m; i++)
    if (A[i] != A[i + 1]) {
      sum += abs(A[i + 1] - A[i]);
      q[A[i]].push_back(A[i + 1]);
      q[A[i + 1]].push_back(A[i]);
    }
  ans = sum;
  for (i = 1; i <= n; i++)
    if (!q[i].empty()) {
      sort(q[i].begin(), q[i].end());
      int k = q[i][q[i].size() / 2];
      long long ans0 = sum;
      for (j = 0; j < q[i].size(); j++) {
        ans0 -= abs(q[i][j] - i);
        ans0 += abs(q[i][j] - k);
      }
      ans = min(ans, ans0);
    }
  printf("%I64d\n", ans);
  return 0;
}
