#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
long long a[MAXN];
vector<long long> g[MAXN];
int n, m;
long long sum, ans;
long long fabs(long long a) {
  if (a < 0) a = -a;
  return a;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++) g[i].clear();
  sum = 0;
  for (int i = 1; i < m; i++) {
    int x = a[i], y = a[i - 1], c = fabs(x - y);
    if (x != y) {
      g[x].push_back(y);
      g[y].push_back(x);
      sum += c;
    }
  }
  for (int i = 0; i < n; i++) sort(g[i].begin(), g[i].end());
  ans = sum;
  for (int i = 0; i < n; i++) {
    if (g[i].size() == 0) continue;
    int cnt = g[i].size() / 2;
    for (int kk = cnt - 1; kk <= cnt + 1; kk++)
      if (0 <= kk && kk <= g[i].size() - 1) {
        long long k = g[i][kk];
        long long cici = sum;
        for (int j = 0; j < g[i].size(); j++) {
          long long inc = fabs(k - g[i][j]);
          long long dec = fabs(i - g[i][j]);
          cici += inc;
          cici -= dec;
        }
        if (cici < ans) ans = cici;
      }
  }
  cout << ans << endl;
}
