#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
int a[maxn];
vector<int> G[maxn];
int main() {
  int n, m;
  cin >> n >> m;
  long long sum = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
    if (i) sum += abs(a[i] - a[i - 1]);
  }
  for (int i = 0; i < m - 1; i++) {
    if (a[i] == a[i + 1]) continue;
    G[a[i]].push_back(a[i + 1]);
    G[a[i + 1]].push_back(a[i]);
  }
  long long mm = 0;
  for (int i = 1; i <= n; i++) {
    if (G[i].size() == 0) continue;
    sort(G[i].begin(), G[i].end());
    long long sum1 = 0, sum2 = 0, cnt = 0;
    int e = G[i][G[i].size() / 2];
    for (int j = 0; j < G[i].size(); j++) {
      sum1 += abs(i - G[i][j]);
      sum2 += abs(e - G[i][j]);
    }
    cnt = sum1 - sum2;
    mm = max(mm, cnt);
  }
  cout << sum - mm << endl;
  return 0;
}
