#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int a[100010], b[100010], flag[100010];
long long sum[100010];
vector<int> adj[100010];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d", a + i);
  m = unique(a + 1, a + m + 1) - a - 1;
  if (m < 3) {
    puts("0");
    return 0;
  }
  for (int i = 2; i < m; i++) {
    adj[a[i]].push_back(a[i - 1]);
    adj[a[i]].push_back(a[i + 1]);
    sum[a[i]] += abs(a[i] - a[i + 1]);
    sum[a[i]] += abs(a[i] - a[i - 1]);
  }
  adj[a[1]].push_back(a[2]);
  adj[a[m]].push_back(a[m - 1]);
  sum[a[1]] += abs(a[2] - a[1]);
  sum[a[m]] += abs(a[m] - a[m - 1]);
  long long ans = 0;
  for (int i = 1; i < m; i++) ans += abs(a[i] - a[i + 1]);
  long long mn = ans;
  for (int i = 1; i <= m; i++) {
    int nn = 0, u = a[i];
    if (flag[u]) continue;
    flag[u] = 1;
    for (int j = adj[u].size(); j--;) {
      int v = adj[u][j];
      b[nn++] = v;
    }
    sort(b, b + nn);
    int first = b[nn / 2];
    long long tmp = ans - sum[u];
    for (int j = 0; j < nn; j++) tmp += abs(b[j] - first);
    smin(mn, tmp);
  }
  printf("%I64d\n", mn);
}
