#include <bits/stdc++.h>
using namespace std;
int a[100009];
vector<int> G[100009];
long long num[100009];
int main() {
  int n, m;
  long long ans = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++) {
    if (i - 1 >= 1 && a[i - 1] != a[i]) G[a[i]].push_back(a[i - 1]);
    if (i + 1 <= m && a[i + 1] != a[i]) G[a[i]].push_back(a[i + 1]);
    if (i + 1 <= m) ans = ans + abs(a[i + 1] - a[i]);
  }
  long long ss = ans;
  for (int i = 1; i <= n; i++) {
    sort(G[i].begin(), G[i].end());
    if (G[i].size() == 0) continue;
    for (int j = 0; j < G[i].size(); j++) num[i] += abs(i - G[i][j]);
    if (G[i].size() % 2 == 1) {
      long long cur = 0;
      int pos = G[i].size() / 2;
      for (int j = 0; j < G[i].size(); j++) cur += abs(G[i][pos] - G[i][j]);
      ans = min(ans, ss - num[i] + cur);
    } else {
      long long cur1 = 0, cur2 = 0;
      int pos = G[i].size() / 2;
      long long tmp = floor(1.0 * (G[i][pos] + G[i][pos - 1]) / 2);
      for (int j = 0; j < G[i].size(); j++) cur1 += abs(tmp - G[i][j]);
      ans = min(ans, ss - num[i] + cur1);
      tmp++;
      for (int j = 0; j < G[i].size(); j++) cur2 += abs(tmp - G[i][j]);
      ans = min(ans, ss - num[i] + cur2);
    }
  }
  cout << ans;
}
