#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100010];
int a[100010];
int main() {
  int n, m;
  long long t = 0, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d", a + i);
  }
  m = unique(a, a + m) - a;
  for (int i = 0; i < m; i++) {
    if (i) {
      adj[a[i]].push_back(a[i - 1]);
      t += abs(a[i] - a[i - 1]);
      adj[a[i - 1]].push_back(a[i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (adj[i].empty()) continue;
    sort(adj[i].begin(), adj[i].end());
    long long tmp = 0;
    for (int j = 0; j < adj[i].size(); j++) {
      tmp += abs(adj[i][j] - i);
    }
    int mid = adj[i][adj[i].size() / 2];
    for (int j = 0; j < adj[i].size(); j++) {
      tmp -= abs(adj[i][j] - mid);
    }
    ans = max(ans, tmp);
  }
  printf("%I64d\n", t - ans);
  return 0;
}
