Language: C++

#include <iostream> // cin, cout, cerr, clog
#include <algorithm> // minmax, sort, swap
#include <numeric> // iota, accumulate, inner_product
#include <cstdio> // printf, scanf
#include <climits> // INT_MIN, LLONG_MIN
#include <cmath> // long, trig, pow
#include <string> // string, stoi, to_string
#include <vector> // vector
#include <queue> // queue, priority_queue
#include <deque> // deque
#include <stack> // stack
#include <map> // key-value pairs sorted by keys
#include <set> // set
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <iomanip> // cout<<setprecision(n)
#include <functional> // std::function<void(int)>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define ENDL '\n'
#define print(i) std::cout << (i) << '\n'

#define int long long // at least int64 > 9*10^18
#define all(v) (v).begin(), (v).end()
/* libraries */

#include <sstream>

#define debug(args...) { std::string _s = #args; std::replace(_s.begin(), _s.end(), ',', ' '); std::stringstream _ss(_s); std::istream_iterator<std::string> _it(_ss); _err(std::cerr, _it, args); }
#define WHATIS(x) std::cerr << #x << " is " << x << '\n'

template<typename T>
std::ostream& operator<<(std::ostream&o,const std::vector<T>&v) {
	o<<'['; for(int i=0;i<v.size();i++) o<<v[i]<<","[i==v.size()-1]; o<<']'; return o;
}

void _err(std::ostream& cerr,std::istream_iterator<std::string> it) { cerr << '\n'; }
template<typename T, typename... Args>
void _err(std::ostream& cerr, std::istream_iterator<std::string> it, T a, Args... args) {
    cerr << *it << " = " << a << "  ";
    _err(cerr,++it, args...);
}


signed main() {
	int n,m;
	std::cin >> n >> m;
	std::vector<int> a(n);
	rep(i,n) std::cin >> a[i];
	rep(i,n) a[i]--;
	
	std::vector<int> cycle(m,0);
	rep(i,n-1) {
		int dist = ((a[i+1]-a[i])%m+m)%m;
		if(dist<=1) continue;
		if((a[i+1]+1)%m>(a[i]+2)%m) {
			cycle[(a[i]+2)%m] += 1;
			cycle[(a[i+1]+1)%m] -= dist;
			if((a[i+1]+2)%m!=0) cycle[(a[i+1]+2)%m] += dist-1;
		} else {
			cycle[(a[i]+2)%m] += 1;
			cycle[0] += m-1-a[i];
			cycle[1] -= m-2-a[i];
			cycle[(a[i+1]+1)%m] -= dist;
			if((a[i+1]+2)%m!=0) cycle[(a[i+1]+2)%m] += dist-1;
		}
	}
	rep(i,m-1) cycle[i+1] += cycle[i];
	rep(i,m-1) cycle[i+1] += cycle[i];
	int max = 0;
	rep(i,m) max = std::max(max,cycle[i]);
	int cycles = 0;
	rep(i,n-1) {
		if(((a[i+1]-a[0])%m+m)%m<((a[i]-a[0])%m+m)%m) cycles++;
	}
	int count = cycles*m+((a[n-1]-a[0])%m+m)%m;
	print(count-max);

	return 0;
}


Compiler output:
a.cc:37:30: error: 'std::istream_iterator' has not been declared
   37 | void _err(std::ostream& cerr,std::istream_iterator<std::string> it) { cerr << '\n'; }
      |                              ^~~
a.cc:37:51: error: expected ',' or '...' before '<' token
   37 | void _err(std::ostream& cerr,std::istream_iterator<std::string> it) { cerr << '\n'; }
      |                                                   ^
a.cc:39:31: error: 'std::istream_iterator' has not been declared
   39 | void _err(std::ostream& cerr, std::istream_iterator<std::string> it, T a, Args... args) {
      |                               ^~~
a.cc:39:52: error: expected ',' or '...' before '<' token
   39 | void _err(std::ostream& cerr, std::istream_iterator<std::string> it, T a, Args... args) {
      |                                                    ^
a.cc: In function 'void _err(std::ostream&, int)':
a.cc:40:14: error: 'it' was not declared in this scope; did you mean 'int'?
   40 |     cerr << *it << " = " << a << "  ";
      |              ^~
      |              int
a.cc:40:29: error: 'a' was not declared in this scope
   40 |     cerr << *it << " = " << a << "  ";
      |                             ^
a.cc:41:21: error: 'args' was not declared in this scope; did you mean 'Args'?
   41 |     _err(cerr,++it, args...);
      |                     ^~~~
      |                     Args