#include <bits/stdc++.h>
using namespace std;
vector<int> G[int(1e5 + 7)];
int n, m, a[int(1e5 + 7)];
int main() {
  scanf("%d%d", &n, &m);
  long long res = 0;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
    if (i > 1 && a[i] != a[i - 1]) {
      G[a[i]].push_back(a[i - 1]), G[a[i - 1]].push_back(a[i]);
      res += abs(a[i] - a[i - 1]);
    }
  }
  long long ans = res;
  for (int i = 1; i <= n; i++) {
    long long sz = G[i].size();
    if (sz) {
      long long tmp = res;
      sort(G[i].begin(), G[i].end());
      int mid = G[i][sz / 2];
      for (int j = 0; j < sz; j++) tmp += abs(mid - G[i][j]) - abs(i - G[i][j]);
      ans = min(ans, tmp);
    }
  }
  printf("%lld\n", ans);
}
