#include <bits/stdc++.h>
using namespace std;
long long A[100005];
vector<int> v[100005];
int main() {
  int n, m, i, j;
  long long sum = 0, x, y, mini = LLONG_MAX;
  cin >> n >> m;
  for (i = 0; i < m; i++) cin >> A[i];
  for (i = 1; i < m; i++)
    if (A[i - 1] != A[i]) v[A[i]].push_back(A[i - 1]);
  for (i = m - 2; i >= 0; i--)
    if (A[i + 1] != A[i]) v[A[i]].push_back(A[i + 1]);
  for (i = 1; i < m; i++) sum += abs(A[i] - A[i - 1]);
  for (i = 1; i <= n; i++) {
    x = y = 0;
    sort(v[i].begin(), v[i].end());
    for (j = 0; j < v[i].size(); j++) {
      x += abs(v[i][j] - i);
      y += abs(v[i][j] - v[i][v[i].size() / 2]);
    }
    mini = min(mini, sum - x + y);
  }
  cout << min(sum, mini);
  return 0;
}
