#include <bits/stdc++.h>
using namespace std;
void setIO(string NAME) {}
inline long long gI() {
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  long long flag = 1, p = 0;
  if (c == '-') flag = -1, c = getchar();
  while (c >= '0' && c <= '9') p = p * 10 + (c - '0'), c = getchar();
  return p * flag;
}
int gs(char* C) {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  int l = 0;
  while (c != ' ' && c != '\n') C[l++] = c, c = getchar();
  C[l] = 0;
  return l;
}
template <class T>
void debug(const T a, const int& n) {
  for (int i = 0; i < n; ++i) printf("%d%c", a[i], (i == n - 1) ? '\n' : ' ');
}
const int inf = ~0U >> 1, maxn = 100000 + 10;
int n, m;
long long a[maxn];
vector<long long> b[maxn];
int main() {
  setIO("test");
  n = gI(), m = gI();
  long long ans = 0;
  for (int i = 0; i < m; ++i) {
    a[i] = gI();
    if (i) ans += abs(a[i] - a[i - 1]);
  }
  long long MAX = 0;
  for (int i = 0; i < m; ++i) {
    if (i && a[i - 1] != a[i]) b[a[i]].push_back(a[i - 1]);
    if (i < m - 1 && a[i] != a[i + 1]) b[a[i]].push_back(a[i + 1]);
  }
  for (int i = 1; i <= n; ++i)
    if (b[i].size()) {
      sort(b[i].begin(), b[i].end());
      int sz = b[i].size();
      int m = (sz - 1) / 2;
      long long origin = 0, now = 0;
      for (int j = 0; j < sz; ++j) {
        origin += abs((long long)b[i][j] - i);
        now += abs(b[i][j] - b[i][m]);
      }
      MAX = max(MAX, origin - now);
    }
  ans -= MAX;
  cout << ans << endl;
  return 0;
}
