#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000100];
bool vis[100100];
long long old[100100];
vector<int> e[100100];
long long ans, sum;
void build() {
  for (int i = 1; i <= n; i++) e[i].clear();
  memset(old, 0, sizeof(old));
  for (int i = 1; i <= m; i++) {
    if (a[i - 1] != a[i]) e[a[i]].push_back(a[i - 1]);
    if (a[i + 1] != a[i]) e[a[i]].push_back(a[i + 1]);
    old[a[i]] += abs(a[i] - a[i + 1]);
    old[a[i]] += abs(a[i] - a[i - 1]);
  }
}
void work() {
  for (int i = 1; i <= n; i++) {
    if (old[i] == 0) continue;
    sort(e[i].begin(), e[i].end());
    int mid = (e[i].size() + 1) / 2 - 1;
    mid = e[i][mid];
    vector<int>::iterator it;
    long long tp = 0;
    for (it = e[i].begin(); it != e[i].end(); it++) {
      tp += abs(mid - *it);
    }
    if (tp < old[i]) ans = min(ans, sum - old[i] + tp);
  }
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
    sum = 0;
    a[0] = a[1];
    a[m + 1] = a[m];
    for (int i = 1; i < m; i++) sum += abs(a[i + 1] - a[i]);
    build();
    ans = sum;
    work();
    cout << ans << endl;
  }
  return 0;
}
