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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

fn dist(u: usize, v: usize, n: usize) -> usize {
    if u <= v {
        v - u
    } else {
        v + n - u
    }
}

fn add2(fr: usize, to: usize, d: i64, x2: &mut Vec<i64>) {
    let l = (to - fr + 1) as i64;
    x2[fr] += 1;
    x2[to+1] -= l + 1;
    x2[to+2] += l;

    x2[fr] += d - 1;
    x2[fr+1] -= d - 1;
    x2[to+1] -= d - 1;
    x2[to+2] += d - 1;
}

fn main() {
    let (n, m): (usize, usize) = read();
    let mut a: Vec<usize> = readnc();
    for i in 0..n {
        a[i] -= 1;
    }

    let mut sum = 0;
    for i in 1..n {
        sum += dist(a[i-1], a[i], m);
    }

    let mut x0 = vec![0; m+2];
    let mut x1 = vec![0; m+2];
    let mut x2 = vec![0; m+2];

    for i in 1..n {
        let fr = a[i-1];
        let to = a[i];
        if dist(fr, to, m) <= 1 {
            continue
        }
        let fr = (fr + 2) % m;
        if fr <= to {
            add2(fr, to, 1, &mut x2);
        } else {
            add2(fr, m-1, 1, &mut x2);
            add2(0, to, (m - fr + 1) as i64, &mut x2);
        }
    }

    x1[0] = x2[0];
    for i in 1..m+1 {
        x1[i] = x1[i-1] + x2[i];
    }
    x0[0] = x1[0];
    for i in 1..m+1 {
        x0[i] = x0[i-1] + x1[i];
    }

    let mut bestdec = 0;
    for i in 0..m {
        bestdec = max(bestdec, x0[i]);
    }
    println!("{}", sum as i64 - bestdec);
}