#include <bits/stdc++.h>
using namespace std;
vector<int> a[100100], b[100100];
int n1[100100] = {0};
int main() {
  long long Answer = 0;
  int n, m, num;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> n1[i];
    if (i > 1 && n1[i] != n1[i - 1]) {
      Answer += abs(n1[i] - n1[i - 1]);
      a[n1[i - 1]].push_back(n1[i]);
      a[n1[i]].push_back(n1[i - 1]);
    }
  }
  long long mx = 1e12;
  for (int i = 1; i <= n; i++) {
    sort(a[i].begin(), a[i].end());
    int ave = a[i].size() / 2;
    long long temp = 0;
    for (int j = 0; j < (int)a[i].size(); j++)
      temp += abs(a[i][ave] - a[i][j]) - abs(i - a[i][j]);
    mx = min(mx, temp);
  }
  cout << Answer + mx << endl;
}
