#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
template <class T>
string tos(T n) {
  stringstream ss;
  ss << n;
  return ss.str();
}
template <typename T, typename TT>
ostream& operator<<(ostream& s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, vector<T> t) {
  for (int i = 0; i < ((int)((t).size())); i++) s << t[i] << " ";
  return s;
}
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long powmod(long long a, long long p, long long m) {
  long long r = 1;
  while (p) {
    if (p & 1) r = r * a % m;
    p >>= 1;
    a = a * a % m;
  }
  return r;
}
map<int, vector<int> > s;
void add(int x, int y) {
  if (s.find(x) == s.end()) {
    vector<int> a(1, y);
    s[x] = a;
  } else {
    s[x].push_back(y);
  }
}
int main(int argc, char* argv[]) {
  int(n), (m);
  scanf("%d %d", &(n), &(m));
  ;
  int x, y;
  long long diff = 0, minDiff = 1e12;
  scanf("%d", &x);
  for (int i = 1; i < m; i++) {
    scanf("%d", &y);
    if (x != y) {
      add(x, y);
      add(y, x);
    }
    diff += abs(x - y);
    x = y;
  }
  if (s.size() <= 1) {
    cout << 0 << endl;
    return 0;
  }
  for (map<int, vector<int> >::iterator i = s.begin(); i != s.end(); i++) {
    vector<int>& a = i->second;
    sort(a.begin(), a.end());
    int middle = a[a.size() / 2], pre = i->first;
    long long sumDiff = 0;
    for (int j = 0; j < a.size(); j++) {
      sumDiff += abs(a[j] - pre) - abs(a[j] - middle);
    }
    minDiff = min(diff - sumDiff, minDiff);
  }
  cout << minDiff << endl;
  return EXIT_SUCCESS;
}
