Problem:
Score : 700 points

Problem Statement

Snuke is buying a lamp.
The light of the lamp can be adjusted to m levels of brightness, represented by integers from 1 through m, by the two buttons on the remote control.

The first button is a "forward" button. When this button is pressed, the brightness level is increased by 1, except when the brightness level is m, in which case the brightness level becomes 1.

The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level x, which is set when the lamp is purchased.

Snuke is thinking of setting the favorite brightness level x so that he can efficiently adjust the brightness.
He is planning to change the brightness n-1 times. In the i-th change, the brightness level is changed from a_i to a_{i+1}. The initial brightness level is a_1.
Find the number of times Snuke needs to press the buttons when x is set to minimize this number.

Constraints

2 \leq n,m \leq 10^5

1 \leq a_i\leq m

a_i \neq a_{i+1}

n, m and a_i are integers.

Input

Input is given from Standard Input in the following format:

n m
a_1 a_2 … a_n

Output

Print the minimum number of times Snuke needs to press the buttons.

Sample Input 1

4 6
1 5 1 4

Sample Output 1

5

When the favorite brightness level is set to 1, 2, 3, 4, 5 and 6, Snuke needs to press the buttons 8, 9, 7, 5, 6 and 9 times, respectively.
Thus, Snuke should set the favorite brightness level to 4.
In this case, the brightness is adjusted as follows:

In the first change, press the favorite button once, then press the forward button once.

In the second change, press the forward button twice.

In the third change, press the favorite button once.

Sample Input 2

10 10
10 9 8 7 6 5 4 3 2 1

Sample Output 2

45

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    static int N;
    static int M;
    static int[] A;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = sc.nextInt() - 1;
        }

        System.out.println(solve());
    }

    static long solve() {
        // a -> b の間のどこにxがあった場合に何点得になるか、をimosなどで何とかする
        // 例 a = 2, b = 7
        // => 3 -> 0, 4 -> 1, 5 -> 2, 6 -> 3, 7 -> 4

        int[] imos = new int[M*2];
        int[] fin = new int[M*2];
        for (int i = 0; i < N-1; i++) {
            int a = A[i];
            int b = A[i+1];
            if( b < a ) {
                b += M;
            }
            if( b - a == 1 ) continue;
            imos[a+2]++;
            imos[b+1]--;
            fin[b+1] += b - a - 1;
        }

        int[] kouka = new int[M];
        int q = 0;
        int r = 0;
        for (int i = 0; i < M*2; i++) {
            q += imos[i];
            r += q;
            r -= fin[i];
            kouka[i%M] += r;
        }

        int high = kouka[0];
        int x = 0;
        for (int i = 1; i < M; i++) {
            if( kouka[i] > high ) {
                x = i;
                high = kouka[i];
            }
        }

        int x2 = x + M;
        long ans = 0;
        for (int i = 0; i < N-1; i++) {
            int a = A[i];
            int b = A[i+1];
            if( b < a ) {
                b += M;
            }

            // a = 2, b = 7, x = 5の場合、
            // 2 -> 5 -> 6 -> 7 と 3回で到達する
            // -> b - x + 1
            if( a + 1 < x && x <= b ) {
                ans += b - x + 1;
            } else if (a + 1 < x2 && x2 <= b) {
                ans += b - x2 + 1;
            } else {
                ans += b - a;
            }
        }
        return ans;
    }

    @SuppressWarnings("unused")
    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }
}