#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> m >> n;
  vector<long long> pages(n);
  for (int i = 0; i < n; i++) {
    cin >> pages[i];
  }
  map<long long, vector<long long>> neighbors;
  for (int i = 0; i < n; i++) {
    long long val = pages[i];
    if (i - 1 >= 0 && pages[i - 1] != pages[i])
      neighbors[val].push_back(pages[i - 1]);
    if (i + 1 < n && pages[i + 1] != pages[i])
      neighbors[val].push_back(pages[i + 1]);
  }
  long long mxsave = 0, sum = 0;
  for (auto &p : neighbors) {
    sort(p.second.begin(), p.second.end());
    long long median = p.second[p.second.size() / 2];
    long long val = p.first;
    long long ncost = 0, cost = 0;
    for (long long x : p.second) {
      ncost += abs(median - x);
      cost += abs(val - x);
    }
    mxsave = max(mxsave, cost - ncost);
    sum += cost;
  }
  cout << sum / 2 - mxsave;
}
