#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 10;
int n, m;
int a[N], b[N];
vector<int> v[N];
long long ans, ret, tmp;
void debug() {
  for (int i = 1; i <= int(n); i++) {
    printf("%d:", i);
    for (int j = 0; j < int(v[i].size()); j++) printf("%d ", v[i][j]);
    puts("");
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= int(m); i++) scanf("%d", a + i);
  for (int i = 1; i <= int(n); i++) v[i].clear();
  for (int i = 1; i <= int(m - 1); i++)
    if (a[i] != a[i + 1]) {
      v[a[i]].push_back(a[i + 1]);
      v[a[i + 1]].push_back(a[i]);
    }
  ret = 0;
  for (int i = 1; i <= int(m - 1); i++) {
    ret += abs(a[i] - a[i + 1]);
  }
  ans = ret;
  for (int i = 1; i <= int(n); i++) {
    vector<int> vec = v[i];
    if (vec.empty()) continue;
    sort(vec.begin(), vec.end());
    int x = vec[vec.size() / 2];
    tmp = ret;
    for (int j = 0; j < int(vec.size()); j++) {
      tmp += abs(x - vec[j]) - abs(i - vec[j]);
    }
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
