#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105000;
int n, m;
int mx;
int a[MAXN], b[MAXN];
long long ans, ansi;
vector<pair<int, int> > g[MAXN];
vector<long long> st[MAXN], en[MAXN];
map<int, long long> cost;
int main() {
  scanf("%d %d", &n, &m);
  mx = n;
  n = m;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &a[i]);
  }
  int pos = 1;
  for (int i = 1; i <= m; i++) {
    if (i == 1) {
      b[pos] = a[i];
      pos++;
    } else {
      if (a[i] == b[pos - 1]) continue;
      b[pos] = a[i];
      pos++;
    }
  }
  m = pos - 1;
  for (int i = 1; i <= m; i++) a[i] = b[i];
  n = m;
  for (int i = 1; i < m; i++) ans += abs(a[i + 1] - a[i]);
  ansi = ans;
  for (int i = 1; i <= m; i++) {
    int cur = a[i];
    int pr = i - 1, nx = i + 1;
    if (pr >= 1) {
      cost[cur] += abs(cur - a[pr]);
      g[cur].push_back(make_pair(a[pr], 1));
      st[cur].push_back(0);
      en[cur].push_back(0);
    }
    if (nx <= m) {
      cost[cur] += abs(cur - a[nx]);
      g[cur].push_back(make_pair(a[nx], 1));
      st[cur].push_back(0);
      en[cur].push_back(0);
    }
    for (int j = max(1, i - 20); j <= min(m, i + 20); j++) {
      if (j == i) continue;
      g[cur].push_back(make_pair(a[j], 0));
      st[cur].push_back(0);
      en[cur].push_back(0);
    }
  }
  for (int i = 1; i <= mx; i++) {
    if (cost.count(i) == 0) continue;
    sort(g[i].begin(), g[i].end());
    int len = (int)g[i].size();
    int num = 0;
    for (int j = 0; j < len; j++) {
      if (j == 0)
        st[i][j] == 0;
      else {
        st[i][j] =
            st[i][j - 1] + 1ll * (g[i][j].first - g[i][j - 1].first) * num;
      }
      if (g[i][j].second) num++;
    }
    num = 0;
    for (int j = len - 1; j >= 0; j--) {
      if (j == len - 1)
        en[i][j] = 0;
      else {
        en[i][j] =
            en[i][j + 1] + 1ll * (g[i][j + 1].first - g[i][j].first) * num;
      }
      if (g[i][j].second) num++;
    }
    for (int j = 0; j < len; j++) {
      ans = min(ans, ansi - cost[i] + st[i][j] + en[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}
