#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const int N = 100007;
int k, n, a[N];
vector<int> G[N];
vector<long long> S[N];
int main() {
  scanf("%d%d", &k, &n);
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    if (i) {
      if (a[i] != a[i - 1]) {
        G[a[i - 1]].push_back(a[i]);
        G[a[i]].push_back(a[i - 1]);
      }
      sum += 2 * abs(a[i] - a[i - 1]);
    }
  }
  for (int i = 1; i <= k; ++i) {
    sort((G[i]).begin(), (G[i]).end());
    S[i].resize((int)(G[i]).size());
    for (int j = 0; j < (int)(G[i]).size(); ++j) {
      S[i][j] = G[i][j];
      if (j) S[i][j] += S[i][j - 1];
    }
  }
  long long best = sum;
  for (int i = 1; i <= k; ++i) {
    int size = (int)(S[i]).size();
    for (int j : G[i]) {
      int pos = lower_bound((G[i]).begin(), (G[i]).end(), i) - G[i].begin();
      long long prv = 0;
      if (pos) prv += -S[i][pos - 1] + pos * (long long)i;
      prv += S[i][size - 1] - (size - pos) * (long long)i;
      if (pos) prv -= S[i][pos - 1];
      pos = lower_bound((G[i]).begin(), (G[i]).end(), j) - G[i].begin();
      long long now = 0;
      if (pos) now += -S[i][pos - 1] + pos * (long long)j;
      now += S[i][size - 1] - (size - pos) * (long long)j;
      if (pos) now -= S[i][pos - 1];
      long long cur = sum - 2 * prv + 2 * now;
      if (cur < best) best = cur;
    }
  }
  printf("%I64d\n", best / 2);
  return 0;
}
