#include <bits/stdc++.h>
using namespace std;
int m, n, a[110000];
vector<int> data[110000];
long long sum, ans, dp[110000];
int abs(int x) { return x > 0 ? x : -x; }
int main() {
  int i, j, k;
  scanf("%d%d", &m, &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= m; i++) data[i].clear();
  if (a[1] != a[2]) data[a[1]].push_back(a[2]);
  memset(dp, 0, sizeof(dp));
  for (i = 2; i <= n; i++) {
    dp[a[i]] += (long long)abs(a[i] - a[i - 1]);
    dp[a[i - 1]] += (long long)abs(a[i] - a[i - 1]);
    if (a[i] != a[i - 1]) data[a[i]].push_back(a[i - 1]);
    if (i != n && a[i] != a[i + 1]) data[a[i]].push_back(a[i + 1]);
    sum += (long long)abs(a[i] - a[i - 1]);
  }
  ans = sum;
  for (i = 1; i <= m; i++) {
    long long temp = sum - dp[i];
    if (data[i].empty()) continue;
    sort(data[i].begin(), data[i].end());
    int x = data[i].size();
    x /= 2;
    for (j = 0; j < data[i].size(); j++)
      temp += (long long)abs(data[i][j] - data[i][x]);
    ans = ((ans) < (temp) ? (ans) : (temp));
  }
  printf("%I64d\n", ans);
  return 0;
}
