#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T& num) {
  char CH;
  bool F = false;
  for (CH = getchar(); CH < '0' || CH > '9'; F = CH == '-', CH = getchar())
    ;
  for (num = 0; CH >= '0' && CH <= '9';
       num = num * 10 + CH - '0', CH = getchar())
    ;
  F && (num = -num);
}
int stk[70], tp;
template <class T>
inline void print(T p) {
  if (!p) {
    puts("0");
    return;
  }
  while (p) stk[++tp] = p % 10, p /= 10;
  while (tp) putchar(stk[tp--] + '0');
  putchar('\n');
}
const long long mod = 1e9 + 7;
const double PI = acos(-1.0);
const long long inf = 1e14;
const int N = 1e5 + 15;
int n, m;
int a[N], vis[N], c[N];
long long b[N], sum = 0;
vector<int> ve[N];
int main() {
  read(n);
  read(m);
  int cnt = 0;
  for (int i = 1; i <= m; i++) {
    read(c[i]);
    if (c[i] != c[i - 1]) a[++cnt] = c[i], vis[c[i]]++;
  }
  m = cnt;
  for (int i = 2; i <= m; i++) sum = sum + abs(a[i] - a[i - 1]);
  for (int i = 1; i <= m; i++) {
    if (i == 1)
      ve[a[i]].push_back(a[2]), b[a[i]] = b[a[i]] + abs(a[2] - a[1]);
    else if (i == m)
      ve[a[i]].push_back(a[m - 1]), b[a[i]] = b[a[i]] + abs(a[m] - a[m - 1]);
    else
      ve[a[i]].push_back(a[i - 1]), ve[a[i]].push_back(a[i + 1]),
          b[a[i]] = b[a[i]] + abs(a[i] - a[i - 1]) + abs(a[i] - a[i + 1]);
  }
  long long ans = sum;
  if (m > 1)
    for (int i = 1; i <= n; i++) {
      if (vis[i]) {
        sort(ve[i].begin(), ve[i].end());
        int len = ve[i].size();
        long long s = 0;
        for (int j = 0; j < len; j++) {
          s = s + abs(ve[i][j] - ve[i][len / 2]);
        }
        ans = min(ans, sum - b[i] + s);
      }
    }
  cout << ans << "\n";
  return 0;
}
