#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> num[100005];
  int t1, t2, count, temp, m, n, q, i, j, k, v, a[100005], count1;
  long long sum, sum1, sum2;
  while (scanf("%d%d", &n, &m) != EOF) {
    vector<int> num[100005];
    count = 0;
    sum = 0;
    for (i = 1; i <= m; ++i) {
      scanf("%d", &a[i]);
      if (i > 1 && a[i] != a[i - 1]) {
        sum += abs(a[i] - a[i - 1]);
        num[a[i]].push_back(a[i - 1]);
        num[a[i - 1]].push_back(a[i]);
      }
    }
    int temp = 0, s = 0;
    sum1 = sum;
    for (i = 1; i <= n; ++i) {
      sum2 = sum1;
      s = num[i].size();
      if (s == 0) continue;
      sort(num[i].begin(), num[i].end());
      temp = num[i][s / 2];
      for (k = 0; k < s; ++k) {
        sum2 += abs(temp - num[i][k]) - abs(i - num[i][k]);
      }
      sum = sum2 < sum ? sum2 : sum;
    }
    printf("%lld\n", sum);
  }
  return 0;
}
