#include <bits/stdc++.h>
using namespace std;
vector<long long> c[200000];
long long cost[200000];
long long v[200000];
int main() {
  int n_i, m_i;
  scanf("%d %d", &n_i, &m_i);
  long long n = n_i, m = m_i;
  long long rm = 0;
  long long pre = -1;
  for (long long i = 0; i < m; i++) {
    int cur_i;
    scanf("%d", &cur_i);
    long long cur = cur_i;
    cur--;
    if (cur != pre) {
      pre = cur;
      v[rm++] = cur;
    }
  }
  long long totalCost = 0;
  for (long long i = 1; i < rm; i++) {
    totalCost += abs(v[i] - v[i - 1]);
  }
  for (long long i = 0; i < rm; i++) {
    long long cur = v[i];
    if (i - 1 >= 0) {
      cost[cur] += abs(v[i - 1] - cur);
      c[cur].push_back(v[i - 1]);
    }
    if (i + 1 < rm) {
      cost[cur] += abs(v[i + 1] - cur);
      c[cur].push_back(v[i + 1]);
    }
  }
  long long bestCost = totalCost;
  for (long long i = 0; i < n; i++) {
    sort(c[i].begin(), c[i].end());
    if (c[i].size() == 0) continue;
    long long median = c[i][c[i].size() / 2];
    long long newCost = 0;
    for (long long j = 0; j < c[i].size(); j++) {
      newCost += abs(c[i][j] - median);
    }
    bestCost = min(bestCost, totalCost - cost[i] + newCost);
  }
  printf("%I64d\n", bestCost);
  return 0;
}
