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