#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(100005);
int a[MAXN];
vector<int> pos[MAXN];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
    pos[a[i]].push_back(i);
  }
  long long sum = 0;
  for (int i = 0; i < m - 1; i++) sum += abs(a[i] - a[i + 1]);
  long long ans = sum;
  for (int i = 1; i <= n; i++) {
    if (pos[i].size() == 0) continue;
    vector<int> t;
    for (int j = 0; j < pos[i].size(); j++) {
      int p = pos[i][j];
      if (p < m - 1 && a[p] != a[p + 1]) t.push_back(a[p + 1]);
      if (p > 0 && a[p] != a[p - 1]) t.push_back(a[p - 1]);
    }
    if (t.size() == 0) continue;
    int median = t.size() / 2;
    nth_element(t.begin(), t.begin() + median, t.end());
    int val = t[median];
    long long tmp = sum;
    for (int j = 0; j < t.size(); j++) {
      tmp -= abs(i - t[j]);
      tmp += abs(val - t[j]);
    }
    ans = min(ans, tmp);
  }
  cout << ans;
}
