#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int num[maxn];
vector<int> v[maxn];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  long long sum = 0, decr = 0;
  int i, j;
  for (i = 0; i < m; i++) {
    scanf("%d", num + i);
  }
  if (m == 1) {
    printf("0\n");
    return 0;
  }
  if (num[0] != num[1]) v[num[0]].push_back(num[1]);
  if (num[m - 1] != num[m - 2]) v[num[m - 1]].push_back(num[m - 2]);
  for (i = 1; i < m - 1; i++) {
    if (num[i] != num[i - 1]) v[num[i]].push_back(num[i - 1]);
    if (num[i] != num[i + 1]) v[num[i]].push_back(num[i + 1]);
  }
  for (i = 1; i <= n; i++) {
    for (j = 0; j < v[i].size(); j++) {
      sum += abs(v[i][j] - i);
    }
    sort(v[i].begin(), v[i].end());
  }
  sum /= 2;
  for (i = 1; i <= n; i++) {
    if (v[i].size() != 0) {
      if (v[i].size() % 2 != 0) {
        long long tmp = 0, tmp1 = 0, tmp2 = 0;
        long long mid = v[i][v[i].size() / 2];
        for (j = 0; j < v[i].size(); j++) {
          tmp1 += abs(mid - v[i][j]);
          tmp2 += abs(i - v[i][j]);
        }
        tmp = abs(tmp2 - tmp1);
        if (tmp > decr && mid != i) decr = tmp;
      } else {
        long long tmp = 0, tmp1 = 0, tmp2 = 0;
        ;
        long long mid = (v[i][v[i].size() / 2] + v[i][v[i].size() / 2 - 1]) / 2;
        for (j = 0; j < v[i].size(); j++) {
          tmp1 += abs(mid - v[i][j]);
          tmp2 += abs(i - v[i][j]);
        }
        tmp = abs(tmp2 - tmp1);
        if (tmp > decr && mid != i) decr = tmp;
      }
    }
  }
  printf("%I64d\n", sum - decr);
  return 0;
}
