#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1;
const long long INF = 1LL << 62;
int n, m;
vector<int> G[maxn];
int a[maxn];
int b[maxn];
long long s[maxn];
int main() {
  scanf("%d%d", &n, &m);
  long long tp = 0;
  for (int i = 0; i < (m); i++) {
    scanf("%d", &a[i]);
    if (i) {
      G[a[i - 1]].push_back(a[i]);
      G[a[i]].push_back(a[i - 1]);
      tp += abs(a[i] - a[i - 1]);
    }
  }
  long long ans = tp;
  for (int i = 1; i <= n; i++) {
    long long tm = tp;
    int tot = 0;
    for (int j = 0; j < (G[i].size()); j++)
      if (G[i][j] != i) {
        b[tot++] = G[i][j];
        tm -= abs(i - G[i][j]);
      }
    if (tot == 0) continue;
    sort(b, b + tot);
    int mid = tot / 2;
    for (int i = 0; i < (tot); i++) tm += abs(b[mid] - b[i]);
    ans = min(ans, tm);
  }
  cout << ans << endl;
}
