#include <bits/stdc++.h>
using namespace std;
long long arr[100005];
vector<int> v[100005];
int main() {
  long long i, j, n, m, ans = 0, mini, temp, temp1;
  scanf("%lld %lld", &m, &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &arr[i]);
    if (1 <= i - 1) ans += abs(arr[i] - arr[i - 1]);
  }
  for (i = 1; i <= n; i++) {
    if (1 <= i - 1 && arr[i] != arr[i - 1]) v[arr[i]].push_back(arr[i - 1]);
    if (i + 1 <= n && arr[i] != arr[i + 1]) v[arr[i]].push_back(arr[i + 1]);
  }
  mini = ans;
  for (i = 1; i <= m; i++) {
    sort(v[i].begin(), v[i].end());
    temp = 0;
    for (auto el : v[i]) temp += abs(i - el);
    temp1 = 0;
    for (auto el : v[i]) temp1 += abs(v[i][(v[i].size() - 1) / 2] - el);
    mini = min(mini, ans - temp + temp1);
  }
  printf("%lld\n", mini);
  return 0;
}
