#include <bits/stdc++.h>
using namespace std;
long long len, n, m, a[100100], d[100100];
vector<long long> b[100100], second[100100];
int upper(long long xx, long long yy) {
  int l = 0, r = b[xx].size() - 1, pos;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (b[xx][mid] <= yy) {
      l = mid + 1;
      pos = mid;
    } else
      r = mid - 1;
  }
  return pos;
}
long long bin_search(long long x, long long y, int sz) {
  int pos = upper(x, y);
  return len - d[x] + (pos + 1) * y - second[x][pos] + second[x][sz - 1] -
         second[x][pos] - (sz - pos - 1) * y;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) cin >> a[i];
  for (int i = 0; i < m; ++i) {
    if (i > 0) {
      if (a[i - 1] != a[i]) b[a[i]].push_back(a[i - 1]);
      len += abs(a[i] - a[i - 1]);
    }
    if (i < m - 1 && a[i + 1] != a[i]) b[a[i]].push_back(a[i + 1]);
  }
  long long mn = len;
  for (int i = 1; i <= n; ++i) sort(b[i].begin(), b[i].end());
  for (int i = 1; i <= n; ++i)
    if (b[i].size() > 0) {
      second[i].push_back(b[i][0]);
      d[i] = abs(i - b[i][0]);
      for (int j = 1; j < b[i].size(); ++j) {
        d[i] += abs(i - b[i][j]);
        second[i].push_back(second[i][second[i].size() - 1] + b[i][j]);
      }
    }
  for (int i = 0; i < m; ++i) {
    if (i > 0 && a[i] != a[i - 1]) {
      mn = min(mn, bin_search(a[i], a[i - 1], b[a[i]].size()));
    }
    if (i < m - 1 && a[i] != a[i + 1]) {
      mn = min(mn, bin_search(a[i], a[i + 1], b[a[i]].size()));
    }
  }
  cout << mn;
  return 0;
}
