#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
int a[100100];
int main() {
  int N, M;
  long long ans = 0;
  cin >> N >> M;
  adj.resize(N + 1);
  for (int i = (0); i < (M); ++i) {
    scanf("%d", a + i);
    if (i && a[i - 1] != a[i]) {
      ans += abs(a[i - 1] - a[i]);
      adj[a[i]].push_back(a[i - 1]);
      adj[a[i - 1]].push_back(a[i]);
    }
  }
  long long d = 0;
  for (int i = (1); i < (N + 1); ++i) {
    sort(adj[i].begin(), adj[i].end());
    int mid = adj[i].size() / 2;
    long long tmp = 0;
    for (int j = (0); j < (adj[i].size()); ++j) {
      tmp += (abs(adj[i][j] - adj[i][mid]) - abs(adj[i][j] - i));
    }
    d = min(d, tmp);
  }
  cout << ans + d << endl;
  return 0;
}
