#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const double PI = atan(1.0) * 4;
const int inf = 2147483647 / 2;
const int maxn = 100005;
template <class T>
void chmin(T &t, T first) {
  if (first < t) t = first;
}
template <class T>
void chmax(T &t, T first) {
  if (first > t) t = first;
}
template <class T>
int sgn(T first) {
  return (first > eps) - (first < -eps);
}
vector<int> an[maxn];
int num[maxn];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = (0); i <= (int)(n); i++) an[i].clear();
    for (int i = (0); i <= (int)(m - 1); i++) {
      scanf("%d", &num[i]);
      if (i && num[i] != num[i - 1]) {
        an[num[i]].push_back(num[i - 1]);
        an[num[i - 1]].push_back(num[i]);
      }
    }
    long long best = 0, idx = -1;
    for (int i = (1); i <= (int)(n); i++)
      if (((int)(an[i]).size())) {
        sort(an[i].begin(), an[i].end());
        long long tsum = 0;
        for (int first : an[i]) tsum += abs(first - i);
        int tmp = an[i][((int)(an[i]).size()) / 2];
        long long sum2 = 0;
        for (int j = (0); j <= (int)(((int)(an[i]).size()) - 1); j++)
          sum2 += abs(an[i][j] - tmp);
        if (tsum - sum2 > best) idx = i, best = tsum - sum2;
      }
    if (idx != -1) {
      int chag = an[idx][((int)(an[idx]).size()) / 2];
      for (int i = (0); i <= (int)(m - 1); i++)
        if (num[i] == idx) num[i] = chag;
    }
    long long ans = 0;
    for (int i = (0); i <= (int)(m - 2); i++) ans += abs(num[i + 1] - num[i]);
    printf("%I64d\n", ans);
  }
  return 0;
}
