#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int arr[N], n, m;
vector<int> vec[N];
long long acc[N], out;
int main() {
  scanf("%d", &n), scanf("%d", &m);
  for (int i = 0; i < m; ++i) scanf("%d", &arr[i]), arr[i]--;
  for (int i = 0; i < m - 1; ++i) {
    if (arr[i] != arr[i + 1])
      vec[arr[i]].push_back(arr[i + 1]), vec[arr[i + 1]].push_back(arr[i]);
  }
  for (int i = 0; i < n; ++i) {
    int s = vec[i].size();
    if (!s) continue;
    sort(vec[i].begin(), vec[i].end());
    long long o = 0;
    for (int j = 0; j < s; ++j) o += abs(vec[i][j] - i);
    acc[0] = vec[i][0];
    for (int j = 1; j < s; j++) acc[j] = acc[j - 1] + vec[i][j];
    for (int j = 0; j < s; j++) {
      out = min(out, acc[s - 1] - acc[j] - 1LL * (s - j - 1) * vec[i][j] +
                         1LL * (j + 1) * vec[i][j] - acc[j] - o);
    }
  }
  for (int i = 0; i < m - 1; ++i) out += abs(arr[i] - arr[i + 1]);
  cout << out;
}
