#include <bits/stdc++.h>
using namespace std;
vector<int> v[100007];
int a[100007];
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    long long sum = 0;
    scanf("%d", a);
    int M = a[0];
    for (int i = 1; i < m; i++) {
      scanf("%d", a + i);
      if (a[i] == a[i - 1]) continue;
      sum += abs(a[i] - a[i - 1]);
      v[a[i]].push_back(a[i - 1]);
      v[a[i - 1]].push_back(a[i]);
      M = max(M, a[i]);
    }
    long long t1 = 0, t2 = 0, N = 0;
    for (int i = 0; i <= M; i++) {
      t1 = 0;
      t2 = 0;
      int s = v[i].size();
      if (!s) continue;
      sort(v[i].begin(), v[i].end());
      int mid = v[i][s / 2];
      for (int j = 0; j < s; j++) {
        t1 += abs(i - v[i][j]);
        t2 += abs(mid - v[i][j]);
      }
      N = max(t1 - t2, N);
    }
    printf("%I64d\n", sum - N);
  }
  return 0;
}
