#include <bits/stdc++.h>
using namespace std;
long long LLMAX = 9223372036854775807LL;
const int MOD = 1000000007;
const int maxn = 111111;
int n, m;
int a[maxn];
long long cc[maxn], nc[maxn];
vector<long long> c[maxn];
int med;
int main() {
  scanf("%d %d", &(n), &(m));
  for (int(i) = 0; (i) < (int)(m); ++(i)) scanf("%d", &(a[i]));
  long long s = 0;
  for (int i = 1; i < m; ++i)
    if (a[i] != a[i - 1]) {
      c[a[i]].push_back(a[i - 1]);
      c[a[i - 1]].push_back(a[i]);
      s += (long long)abs(a[i] - a[i - 1]);
      cc[a[i]] += (long long)abs(a[i] - a[i - 1]);
      cc[a[i - 1]] += (long long)abs(a[i] - a[i - 1]);
    }
  for (int i = 1; i <= n; ++i)
    if (c[i].size() > 0) sort(c[i].begin(), c[i].end());
  for (int i = 1; i <= n; ++i)
    if (c[i].size() > 0) {
      med = (c[i][c[i].size() / 2] + c[i][(c[i].size() - 1) / 2]);
      for (int j = 0; j < c[i].size(); ++j)
        nc[i] += (long long)abs(med / 2 - c[i][j]);
      long long tmp = 0;
      if (med % 2 == 1) {
        for (int j = 0; j < c[i].size(); ++j)
          tmp += (long long)abs(med / 2 + 1 - c[i][j]);
        nc[i] = min(nc[i], tmp);
      }
    }
  long long ans = s;
  for (int i = 1; i <= n; ++i)
    if (c[i].size() > 0) {
      ans = min(ans, s - cc[i] + nc[i]);
    }
  cout << (ans) << endl;
}
