#include <bits/stdc++.h>
using namespace std;
int a[111111], b[111111];
vector<int> id[111111];
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 1; i <= N; i++) id[i].clear();
  for (int i = 0; i < M; i++) scanf("%d", a + i);
  long long tot = 0;
  for (int i = 1; i < M; i++) {
    if (a[i] == a[i - 1]) continue;
    id[a[i]].push_back(a[i - 1]);
    id[a[i - 1]].push_back(a[i]);
    tot += abs(a[i] - a[i - 1]);
  }
  long long d = 0;
  for (int i = 1; i <= N; i++) {
    sort(id[i].begin(), id[i].end());
    long long s = 0;
    int k = id[i].size() / 2;
    for (int j = 0; j < id[i].size(); j++)
      s += abs(id[i][j] - id[i][k]) - abs(id[i][j] - i);
    if (d > s) d = s;
  }
  cout << tot + d << endl;
}
