/* 中位数到所有数的距离之和最小 因为只能改一个数,所以我们找一个数,将其改为和其相邻的数的中位数,使答案最小 先求一次原答案 把每个数相邻的数用vector存下来,然后排序找中位数,计算减小的量 */ #include<bits/stdc++.h> #include<vector> using namespace std; #define ll long long #define N 200005 vector<int> v[N]; int n,m,a[N]; int main(){ cin>>n>>m; for(int i=1;i<=m;i++)cin>>a[i]; long long sum=0; for(int i=1;i<m;i++) sum+=abs(a[i]-a[i+1]); /*if(a[1]!=a[2]){ v[a[1]].push_back(a[2]); v[a[2]].push_back(a[1]); }*/ for(int i=1;i<m;i++)if(a[i]!=a[i+1]){ v[a[i]].push_back(a[i+1]); v[a[i+1]].push_back(a[i]); } for(int i=1;i<=200000;i++)sort(v[i].begin(),v[i].end()); ll ans=sum; for(int i=1;i<=200000;i++)if(v[i].size()){ ll last=0; for(int j=0;j<v[i].size();j++) last+=abs(i-v[i][j]); ll now=0,tmp=v[i].size()/2; for(int j=0;j<v[i].size();j++) now+=abs(v[i][tmp]-v[i][j]); ans=min(ans,sum-last+now); } cout<<ans<<' '; }