Given two operations on a variable $$$a$$$: Replace $$$a$$$ with $$$(a + 1) \mod m$$$, where $$$\%$$$ represents the modulo operation;
Assign $$$a$$$ the value $$$X$$$.
Mr. Tung wants to sequentially generate $$$n$$$ non-negative integers $$$b_1, b_2, \ldots, b_n$$$ starting from the initial value $$$a = b_1$$$, using the above operations and choosing a suitable value $$$X$$$ to minimize the number of operations needed.
Find the minimum number of operations required to obtain the given sequence $$$b$$$.
The first line contains two integers $$$n$$$ and $$$m$$$ $$$(2 \leq n, m \leq 10^5)$$$;
The second line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ $$$(0 \leq b_i < m; 1 \leq i \leq n)$$$.
A single integer, which is the result of the problem.