#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
const int MOD = 1e9 + 7;
const int maxn = 110000;
const double PI = acos(-1.0);
long long ipt[maxn];
map<long long, vector<long long> > mp;
map<long long, vector<long long> >::iterator it;
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    mp.clear();
    for (int i = 0; i < (m); ++i) {
      cin >> ipt[i];
    }
    for (int i = 0; i < (m); ++i) {
      if (i > 0 && ipt[i - 1] != ipt[i]) mp[ipt[i]].push_back(ipt[i - 1]);
      if (i < m - 1 && ipt[i + 1] != ipt[i]) mp[ipt[i]].push_back(ipt[i + 1]);
    }
    long long ans = 0;
    for (__typeof((mp).begin()) it = (mp).begin(); it != (mp).end(); it++) {
      vector<long long>& ipt = it->second;
      sort((ipt).begin(), (ipt).end());
      long long t = 0, m = ipt[(long long)ipt.size() / 2];
      for (int i = 0; i < (ipt.size()); ++i) {
        t += abs((it->first) - ipt[i]) - abs(m - ipt[i]);
      }
      ans = max(ans, t);
    }
    ans *= -1;
    for (int i = 0; i < (m - 1); ++i) ans += abs(ipt[i] - ipt[i + 1]);
    cout << ans << endl;
  }
  return 0;
}
