#include <bits/stdc++.h>
using namespace std;
int v[100100];
vector<int> L[100100];
vector<int> u;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (m == 1) {
    printf("0\n");
    return 0;
  }
  long long sum = 0;
  scanf("%d", v);
  L[v[0]].push_back(0);
  for (int i = 1; i < m; i++) {
    scanf("%d", v + i);
    sum += abs(v[i] - v[i - 1]);
    L[v[i]].push_back(i);
  }
  long long ans = sum;
  for (int i = 1; i <= n; i++) {
    if (L[i].size() == 0) continue;
    long long cur = sum;
    for (vector<int>::iterator it = L[i].begin(); it < L[i].end(); it++) {
      if (*it == 0) {
        if (v[1] - i) u.push_back(v[1]);
      } else if (*it == m - 1) {
        if (v[m - 2] - i) u.push_back(v[m - 2]);
      } else {
        if (v[*it - 1] - i) u.push_back(v[*it - 1]);
        if (v[*it + 1] - i) u.push_back(v[*it + 1]);
      }
    }
    if (u.size() == 0) continue;
    sort(u.begin(), u.end());
    int val = u[(u.size() - 1) / 2];
    ;
    for (vector<int>::iterator it = L[i].begin(); it < L[i].end(); it++) {
      ;
      if (*it == 0) {
        if (v[1] - v[0]) cur = cur - abs(v[1] - v[0]) + abs(v[1] - val);
      } else if (*it == m - 1) {
        if (v[m - 1] - v[m - 2])
          cur = cur - abs(v[m - 1] - v[m - 2]) + abs(val - v[m - 2]);
      } else {
        if (v[*it] - v[*it - 1])
          cur = cur - abs(v[*it] - v[*it - 1]) + abs(val - v[*it - 1]);
        if (v[*it] - v[*it + 1])
          cur = cur - abs(v[*it] - v[*it + 1]) + abs(val - v[*it + 1]);
        ;
      }
    }
    if (n <= 10) {
      cur = 0;
      for (int j = 1; j < m; j++) {
        int a = v[j - 1];
        int b = v[j];
        if (a == i) a = val;
        if (b == i) b = val;
        cur += abs(b - a);
      }
    }
    ans = min(ans, cur);
    u.clear();
  }
  printf("%I64d\n", ans);
}
