#include <bits/stdc++.h>
using namespace std;
int a[100005];
vector<int> g[100005];
vector<pair<long long, pair<int, int> > > ans;
long long cal(int i, int sz, int m) {
  long long res = 0;
  for (int j = 0; j < sz; ++j) res += abs(m - g[i][j]);
  return res;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) scanf("%d", a + i);
  for (int i = 1; i <= m - 1; ++i)
    if (a[i] != a[i + 1])
      g[a[i]].push_back(a[i + 1]), g[a[i + 1]].push_back(a[i]);
  for (int i = 1; i <= n; ++i) {
    int sz = g[i].size();
    if (!sz) continue;
    sort(g[i].begin(), g[i].end());
    long long sum = 0;
    for (int j = 0; j < sz; ++j) sum += abs(i - g[i][j]);
    int l = g[i][0], r = g[i][sz - 1];
    while (l < r) {
      int m1 = (2 * l + r) / 3, m2 = (l + 2 * r + 2) / 3;
      if (cal(i, sz, m1) <= cal(i, sz, m2))
        r = m2 - 1;
      else
        l = m1 + 1;
    }
    ans.push_back(make_pair(sum - cal(i, sz, l), make_pair(i, l)));
  }
  ans.push_back(make_pair(0, make_pair(1, a[1])));
  sort(ans.begin(), ans.end());
  reverse(ans.begin(), ans.end());
  int from = ans[0].second.first, to = ans[0].second.second;
  for (int i = 1; i <= m; ++i)
    if (a[i] == from) a[i] = to;
  long long sum = 0;
  for (int i = 1; i <= m - 1; ++i) sum += abs(a[i] - a[i + 1]);
  printf("%lld\n", sum);
  return 0;
}
