#include <bits/stdc++.h>
using namespace std;
int n, m;
long long a[100050];
vector<long long> vec[100050];
int main() {
  long long ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    scanf("%I64d", a + i);
  }
  for (int i = 1; i <= n; i++) {
    vec[i].clear();
  }
  long long pre = a[1];
  a[0] = a[1];
  for (int i = 1; i <= m; i++) {
    ans += abs(a[i] - a[i - 1]);
    if (a[i] == pre)
      continue;
    else {
      vec[pre].push_back(a[i]);
      vec[a[i]].push_back(pre);
      pre = a[i];
    }
  }
  long long best_ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vec[i].size() == 0) continue;
    sort(vec[i].begin(), vec[i].end());
    int h = vec[i].size();
    long long s0 = 0, s1 = 0, cwj = vec[i][h / 2], xxx = (long long)(i);
    for (int j = 0; j < h; j++) {
      s0 += abs(vec[i][j] - xxx);
      s1 += abs(vec[i][j] - cwj);
    }
    best_ans = max(best_ans, s0 - s1);
  }
  ans = ans - best_ans;
  cout << ans << endl;
  return 0;
}
