#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int ara[N];
int n, m;
vector<int> g[N];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) cin >> ara[i];
  long long M = 0;
  for (int i = 1; i <= m - 1; i++)
    M = M + ((ara[i + 1] - ara[i]) < 0 ? -(ara[i + 1] - ara[i])
                                       : (ara[i + 1] - ara[i]));
  for (int i = 1; i <= m; i++) {
    int in = ara[i];
    if (i - 1 >= 1 and ara[i] != ara[i - 1]) g[in].push_back(ara[i - 1]);
    if (i + 1 <= m and ara[i] != ara[i + 1]) g[in].push_back(ara[i + 1]);
  }
  long long ans = M;
  for (int i = 1; i <= n; i++) {
    if (g[i].empty()) continue;
    sort(g[i].begin(), g[i].end());
    int m = g[i].size() / 2;
    long long expected = g[i][m];
    long long now = M;
    for (int j = 0; j < g[i].size(); j++) {
      now = now - ((g[i][j] - i) < 0 ? -(g[i][j] - i) : (g[i][j] - i));
    }
    for (int j = 0; j < g[i].size(); j++) {
      now = now + ((g[i][j] - expected) < 0 ? -(g[i][j] - expected)
                                            : (g[i][j] - expected));
    }
    ans = min(ans, now);
  }
  cout << ans << "\n";
}
