#include <bits/stdc++.h>
using namespace std;
int n, m;
long long data[100010], diff[100010], change[100010];
vector<vector<long long> > adj;
vector<pair<long long, int> > vp;
int main(void) {
  scanf("%d%d", &n, &m);
  adj.resize(n + 1, vector<long long>());
  for (int i = 0; i < m; i++) scanf("%I64d", &data[i]);
  if (m == 1) {
    puts("0");
    return 0;
  }
  for (int i = 0; i < m; i++) {
    if (i == 0 && data[i] != data[i + 1])
      adj[data[i]].push_back(data[i + 1]);
    else if (i == m - 1 && data[i] != data[i - 1])
      adj[data[i]].push_back(data[i - 1]);
    else if (i > 0 && i < m - 1) {
      if (data[i] != data[i - 1]) adj[data[i]].push_back(data[i - 1]);
      if (data[i] != data[i + 1]) adj[data[i]].push_back(data[i + 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (adj[i].empty()) continue;
    long long sum = 0;
    sort(adj[i].begin(), adj[i].end());
    long long avg1 = adj[i][adj[i].size() / 2];
    long long newMid = adj[i].size() / 2 - 1;
    if (newMid < 0) newMid = 0;
    long long avg2 = adj[i][newMid];
    long long sum1 = 0, sum2 = 0;
    for (int j = 0; j < adj[i].size(); j++) sum += abs(adj[i][j] - i);
    for (int j = 0; j < adj[i].size(); j++) sum1 += abs(adj[i][j] - avg1);
    for (int j = 0; j < adj[i].size(); j++) sum2 += abs(adj[i][j] - avg2);
    if (sum1 < sum2) {
      vp.push_back(make_pair(sum - sum1, i));
      change[i] = avg1;
    } else {
      vp.push_back(make_pair(sum - sum2, i));
      change[i] = avg2;
    }
  }
  sort(vp.begin(), vp.end());
  if (vp.empty()) {
    puts("0");
    return 0;
  }
  int target = vp[vp.size() - 1].second;
  for (int i = 0; i < m; i++)
    if (data[i] == target) data[i] = change[target];
  long long sol = 0;
  for (int i = 0; i < m - 1; i++) sol += abs(data[i] - data[i + 1]);
  printf("%I64d\n", sol);
}
