#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
long long t, n, m, k, a, b, x, y, q, ans, arr[N];
string s;
long long mod = 998244353;
bool vis[N];
vector<long long> vec[N];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  t = 1;
  while (t--) {
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
      cin >> arr[i];
      if (i > 1) ans += llabs(arr[i] - arr[i - 1]);
    }
    for (int i = 1; i <= m; ++i) {
      long long cur = arr[i];
      long long prev = (i != 1) ? arr[i - 1] : cur;
      long long nxt = (i != m) ? arr[i + 1] : cur;
      if (cur != prev) vec[cur].push_back(prev);
      if (cur != nxt) vec[cur].push_back(nxt);
    }
    long long best = ans;
    for (int i = 0; i < N; ++i) {
      vector<long long> x = vec[i];
      if (!x.size()) continue;
      sort(x.begin(), x.end());
      long long median = x[x.size() / 2];
      long long a = 0, b = 0;
      for (auto &y : x) a += llabs(i - y), b += llabs(median - y);
      ans = min(ans, best + b - a);
    }
    cout << ans << endl;
  }
  return 0;
}
