#include <bits/stdc++.h>
using namespace std;
int di[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dj[] = {1, -1, 0, 0, 1, -1, 1, -1};
inline long long calcMedian(vector<long long> &vec) {
  return vec[((int)vec.size()) / 2];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long init = 0, ans = 0;
  int n, m;
  cin >> n >> m;
  vector<long long> vec(m);
  vector<vector<long long> > adj(n);
  for (int i = 0; i < m; ++i) {
    cin >> vec[i];
    --vec[i];
    if (i && vec[i] != vec[i - 1]) {
      ans = init += abs(vec[i] - vec[i - 1]);
      adj[vec[i]].push_back(vec[i - 1]);
      adj[vec[i - 1]].push_back(vec[i]);
    }
  }
  for (int i = 0; i < n; ++i)
    if (((int)adj[i].size())) {
      sort(adj[i].begin(), adj[i].end());
      long long median = calcMedian(adj[i]);
      long long tmp = 0;
      for (int j = 0; j < ((int)adj[i].size()); ++j) {
        tmp -= abs(adj[i][j] - i);
        tmp += abs(adj[i][j] - median);
      }
      if (init + tmp < ans) ans = init + tmp;
    }
  cout << ans << '\n';
  return 0;
}
