#include <bits/stdc++.h>
using namespace std;
void fun() {}
long long int md = 998244353;
long long int __gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return __gcd(b, a % b);
}
long long int poww(long long int a, long long int b) {
  long long int res = 1;
  while (b) {
    if (b & 1) {
      res *= a;
      res %= md;
    }
    a *= a;
    a %= md;
    b >>= 1;
  }
  return res;
}
void ainp(long long int arr[], long long int n) {
  for (long long int i = 1; i <= n; i++) cin >> arr[i];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fun();
  long long int n, m;
  cin >> n >> m;
  long long int arr[m + 1];
  ainp(arr, m);
  vector<long long int> v[n + 1];
  long long int sum = 0;
  for (long long int i = 1; i <= m; i++) {
    if (i != 1 && arr[i] != arr[i - 1]) v[arr[i]].push_back(arr[i - 1]);
    if (i != m) sum += abs(arr[i + 1] - arr[i]);
    if (i != m && arr[i] != arr[i + 1]) v[arr[i]].push_back(arr[i + 1]);
  }
  long long int minsum2 = LLONG_MAX;
  long long int sum2 = 0;
  for (long long int i = 1; i <= n; i++) {
    long long int sz = v[i].size();
    if (sz == 0) continue;
    sort(v[i].begin(), v[i].end());
    long long int med = 0;
    if (sz & 1) {
      med = v[i][sz / 2];
    } else {
      med = v[i][sz / 2] + v[i][sz / 2 - 1];
      med /= 2;
    }
    sum2 = sum;
    for (long long int j = 0; j < v[i].size(); j++) {
      sum2 += -abs(i - v[i][j]) + abs(med - v[i][j]);
    }
    minsum2 = min(minsum2, sum2);
  }
  if (minsum2 == LLONG_MAX) return cout << "0\n", 0;
  cout << minsum2 << "\n";
  return 0;
}
