#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
const int N = 100010;
int n, m;
long long v[N];
set<long long> s;
map<long long, long long> my;
vector<long long> g[N];
long long solve(long long id, long long med) {
  long long ret = 0;
  for (int i = 0; i < g[id].size(); ++i)
    ret += abs(id - g[id][i]) - abs(med - g[id][i]);
  return ret;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%lld", v + i);
    s.insert(v[i]);
  }
  for (int i = 0; i < m; ++i) {
    if (i && v[i] != v[i - 1]) g[v[i]].push_back(v[i - 1]);
    if (i + 1 < m && v[i] != v[i + 1]) g[v[i]].push_back(v[i + 1]);
  }
  long long gap = 0;
  long long value = 0;
  long long id = 0;
  for (int i = 1; i <= n; ++i) {
    if (g[i].size() == 0) continue;
    sort(g[i].begin(), g[i].end());
    int tam = g[i].size();
    long long gg = solve(i, g[i][tam / 2]);
    if (gg > gap) {
      gap = gg;
      value = g[i][tam / 2];
      id = i;
    }
    if (tam % 2 == 0) {
      gg = solve(i, g[i][tam / 2 - 1]);
      if (gg > gap) {
        gap = gg;
        value = g[i][tam / 2 - 1];
        id = i;
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < m; ++i) {
    if (v[i] == id) v[i] = value;
    if (i) ans += abs(v[i] - v[i - 1]);
  }
  printf("%lld\n", ans);
  return 0;
}
