#include <bits/stdc++.h>
using namespace std;
long long int tmod(long long int x, long long int m) { return (x % m + m) % m; }
vector<vector<long long int>> amp(100001);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  if (m == 1) {
    cout << 0;
    return 0;
  }
  long long int a[m];
  for (int i = 0; i < m; i++) cin >> a[i];
  long long int ans = 0;
  for (int i = 1; i < m; i++) ans += abs(a[i] - a[i - 1]);
  for (int i = 1; i < m - 1; i++) {
    if (a[i] != a[i - 1]) amp[a[i]].push_back(a[i - 1]);
    if (a[i] != a[i + 1]) amp[a[i]].push_back(a[i + 1]);
  }
  if (a[0] != a[1]) amp[a[0]].push_back(a[1]);
  if (a[m - 1] != a[m - 2]) amp[a[m - 1]].push_back(a[m - 2]);
  for (int i = 1; i < 100001; i++)
    if (amp[i].size() != 0) sort(amp[i].begin(), amp[i].end());
  long long int initsum = ans;
  for (int i = 1; i < 100001; i++) {
    if (amp[i].size() != 0) {
      long long int pos =
          lower_bound(amp[i].begin(), amp[i].end(), i) - amp[i].begin();
      long long int sum = 0;
      for (int j = 0; j < pos; j++) sum += amp[i][j];
      long long int initial =
          2 * pos * i - ((long long int)amp[i].size()) * ((long long int)i) -
          2 * sum;
      long long int best = initial;
      amp[i].push_back(0);
      sum = 0;
      for (int j = 1; j < amp[i].size(); j++) {
        sum += amp[i][j - 1];
        if (amp[i][j] != amp[i][j - 1]) {
          long long int temp = amp[i][j - 1];
          best = min(best, 2 * j * temp -
                               ((long long int)(amp[i].size() - 1)) * temp -
                               2 * sum);
        }
      }
      ans = min(ans, initsum + best - initial);
    }
  }
  cout << ans << "\n";
  return 0;
}
