#include <bits/stdc++.h>   using namespace std;   template <typename T, size_t N>   struct ma : array<T, N> {     T &operator[](size_t n) { return (*static_cast<array<T, N> *>(this))[n]; }   };   template <class T>   string to_str(const T &a) {     ostringstream os;     os << a;     return os.str();   }   template <>   string to_str<long double>(const long double &a) {     ostringstream os;     os.precision(10);     os.setf(ios::fixed);     os << a;     return os.str();   }   template <class T>   T from_str(const string &s) {     istringstream is;     T val;     is >> val;     return val;   }   int cond = (long long)0;   map<int, vector<int> > nei;   int dat[(int)1e5 + 11];   void solve() {     int n, m;     scanf( %d%d , &n, &m);     for (auto i = (0); i < (m); ++i) {       scanf( %d , &dat[i]);     }     long long turn = 0;     for (auto i = (0); i < (m - 1); ++i) {       int a = dat[i], b = dat[i + 1];       turn += abs(a - b);       if (a != b) {         nei[a].push_back(b);         nei[b].push_back(a);       }     }     long long res = turn;     for (auto &it : (nei)) {       auto kol = it.second;       sort((kol).begin(), (kol).end());       int mid = kol.size() / 2;       long long cand1 = 0;       long long cand2 = 0;       for (auto &jt : (kol)) {         cand1 += abs(it.first - jt);         cand2 += abs(jt - kol[mid]);       }       res = min(res, turn + cand2 - cand1);     }     cout << res << endl;   }   void brute() {}   void gen(int i, int n, int k) {}   int main(int argc, char **argv) {     ios::sync_with_stdio(false);     vector<string> args;     int nr_test = -1;     int t = 1;     for (auto i = (1); i <= (argc - 1); ++i) args.push_back(argv[i]);     for (auto &it : (args))       if (it ==  q ) cond = 10000000;     for (auto &it : (args))       if (it ==  all ) t = 1000;     for (auto &it : (args))       if (atoi(it.c_str()) > 0) nr_test = atoi(it.c_str());     for (auto &it : (args))       if (it ==  b ) {         brute();         return 0;       }     for (auto &it : (args))       if (it ==  g  && args.size() >= 4) {         gen(atoi(args[1].c_str()), atoi(args[2].c_str()), atoi(args[3].c_str()));         return 0;       }     for (auto i = (1); i <= (t); ++i) {       if (nr_test == -1 || i <= nr_test) solve();     }     return 0;   }   