#include <bits/stdc++.h>
using namespace std;
int kkaass = 1;
long long ar[100009];
vector<int> vec[100009];
int n, m;
long long func(int pivot, vector<int> &con, int id) {
  long long ans = 0;
  for (int i = 0; i < (int)con.size(); i++) {
    if (id == con[i])
      ;
    else
      ans += abs(con[i] - pivot);
  }
  return ans;
}
long long findGood(vector<int> &container, int id) {
  int l = container[0], r = container.back();
  int l1, l2;
  for (int i = 0; i < 40; i++) {
    l1 = (l * 2 + r) / 3;
    l2 = (l + 2 * r) / 3;
    if (func(l1, container, id) < func(l2, container, id))
      r = l2;
    else
      l = l1;
  }
  int ans = l;
  long long ans1 = func(l, container, id);
  for (int i = l + 1; i <= r; i++) {
    long long ans2 = func(i, container, id);
    if (ans2 < ans1) {
      ans1 = ans2;
      ans = i;
    }
  }
  return ans1;
}
int main() {
  ios_base::sync_with_stdio(false);
  while (cin >> n >> m) {
    int i, j, k;
    for (i = 0; i < (n + 1); i++) vec[i].clear();
    for (i = 0; i < (m); i++) cin >> ar[i];
    for (i = 0; i < (m); i++) {
      if (i - 1 >= 0) vec[ar[i]].push_back(ar[i - 1]);
      if (i + 1 < m) vec[ar[i]].push_back(ar[i + 1]);
    }
    for (i = 1; i <= n; i++) sort(vec[i].begin(), vec[i].end());
    long long tm = 0;
    for (i = 1; i <= n; i++) {
      if (!(int)vec[i].size()) continue;
      if ((int)vec[i].size() == 1) {
        tm = max(tm, (long long)abs(vec[i][0] - i));
      } else {
        long long get1 = func(i, vec[i], i);
        long long get2 = findGood(vec[i], i);
        if (get2 < get1) {
          tm = max(tm, get1 - get2);
        }
      }
    }
    long long ans = 0;
    for (int i = 0; i < m - 1; i++) {
      ans += abs(ar[i] - ar[i + 1]);
    }
    cout << ans - tm << endl;
  }
  return 0;
}
