#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d %d\n", &n, &m);
  vector<int> *b = new vector<int>[n + 1];
  int *a = new int[m + 1];
  long long ans = 0;
  scanf("%d", a);
  for (int i = 1; i < m; i++) {
    scanf("%d", a + i);
    ans += abs(a[i] - a[i - 1]);
    if (a[i] != a[i - 1]) {
      b[a[i]].push_back(a[i - 1]);
      b[a[i - 1]].push_back(a[i]);
    }
  }
  long long h = ans;
  for (int i = 1; i <= n; i++) {
    if (!b[i].size()) continue;
    std::sort(b[i].begin(), b[i].end());
    int median = b[i][b[i].size() / 2];
    long long z = ans;
    for (int k = 0; k < b[i].size(); k++) {
      z += abs(median - b[i][k]) - abs(i - b[i][k]);
    }
    h = min(z, h);
  }
  cout << h;
  return 0;
}
