#include <bits/stdc++.h>
using namespace std;
long long n, m, a[100002], mid[100002];
vector<long long> ch(100002, 0);
map<long long, vector<long long> > adj;
long long sum;
void initialization() {
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    cin >> a[i];
    if (i && a[i] != a[i - 1]) {
      sum += abs(a[i] - a[i - 1]);
      adj[a[i]].push_back(a[i - 1]);
      adj[a[i - 1]].push_back(a[i]);
    }
  }
}
long long mn = 100002;
void solve() {
  for (int i = 1; i <= n; ++i)
    if (adj[i].size()) {
      sort(adj[i].begin(), adj[i].end());
      mid[i] = adj[i][adj[i].size() / 2];
      for (int j = 0; j < adj[i].size(); ++j)
        ch[i] += abs(adj[i][j] - mid[i]) - abs(adj[i][j] - i);
      if (ch[i] < mn) mn = ch[i];
    }
  mn == 100002 ? sum += 0 : sum += mn;
}
void print() { cout << sum; }
int main() {
  ios::sync_with_stdio(0);
  initialization();
  solve();
  print();
  return 0;
}
