#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> a(m);
  vector<vector<int>> b(n + 1);
  for (int i = 0; i < m; i++) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    if (i && a[i - 1] != a[i]) b[a[i - 1]].push_back(a[i]);
    if (i != m - 1 && a[i + 1] != a[i]) b[a[i + 1]].push_back(a[i]);
  }
  long long ans = 0, sum = 0;
  for (int i = 1; i <= n; i++)
    if (!b[i].empty()) {
      long long x = 0;
      sort(b[i].begin(), b[i].end());
      for (int j = 0; j < b[i].size(); j++) {
        sum += abs(i - b[i][j]);
        x += abs(i - b[i][j]) - abs(b[i][b[i].size() / 2] - b[i][j]);
      }
      ans = max(ans, x);
    }
  cout << sum / 2 - ans;
}
