#include <bits/stdc++.h>
int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1};
int dy[] = {0, -1, 1, 0, -1, 1, -1, 1};
using namespace std;
vector<long long> v;
vector<long long> neig[100009];
int main() {
  int n = ({
    int t;
    scanf("%d", &t);
    t;
  }),
      m = ({
        int t;
        scanf("%d", &t);
        t;
      }),
      ans = 0;
  for (int i = 0; i < m; i++) {
    int a = ({
      int t;
      scanf("%d", &t);
      t;
    });
    v.push_back(a);
  }
  if (m == 1) {
    cout << "0";
    return 0;
  }
  if (v[0] != v[1]) neig[v[0]].push_back(v[1]);
  for (int i = 1; i < m - 1; i++) {
    if (v[i] != v[i - 1]) neig[v[i]].push_back(v[i - 1]);
    if (v[i] != v[i + 1]) neig[v[i]].push_back(v[i + 1]);
  }
  if (v[m - 1] != v[m - 2]) neig[v[m - 1]].push_back(v[m - 2]);
  long long current = 0, max_red = 0;
  for (int i = 0; i < m - 1; i++) current += abs(v[i] - v[i + 1]);
  for (int i = 1; i <= n; i++) {
    if (neig[i].size() == 0) continue;
    sort(neig[i].begin(), neig[i].end());
    long long cost = 0, red = 0;
    long long mid = neig[i].size() / 2;
    for (int j = 0; j < neig[i].size(); j++) cost += abs(neig[i][j] - i);
    for (int j = 0; j < neig[i].size(); j++)
      red += abs(neig[i][j] - neig[i][mid]);
    max_red = max(max_red, cost - red);
  }
  cout << current - max_red << "\n";
  return 0;
}
