#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1000;
const long long int inf = 0x3f3f3f3f;
void read(long long &x) {
  char ch;
  bool flag = 0;
  for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1);
       ch = getchar())
    ;
  for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar())
    ;
  x *= 1 - 2 * flag;
}
int n, m;
vector<long long> vec[maxn];
long long arr[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    read(arr[i]);
  }
  for (int i = 1; i < m; i++) {
    if (arr[i] == arr[i - 1]) continue;
    vec[arr[i]].push_back(arr[i - 1]);
    vec[arr[i - 1]].push_back(arr[i]);
  }
  long long sum = 0;
  for (int i = 0; i < m - 1; i++) sum += abs(arr[i] - arr[i + 1]);
  long long minn = inf * 1000;
  for (int i = 1; i <= n; i++) {
    int siz = vec[i].size();
    if (!siz) continue;
    sort(vec[i].begin(), vec[i].begin() + siz);
    int tmp = vec[i][siz / 2];
    long long sub = sum;
    long long s1 = 0;
    for (int j = 0; j < siz; j++) {
      sub += (abs(tmp - vec[i][j]) - abs(i - vec[i][j]));
    }
    minn = min(minn, sub);
  }
  if (minn == inf * 1000) minn = 0;
  cout << minn << endl;
  return 0;
}
