fn main() {
    let (_, m): (i32, usize) = readln();
    let a: Vec<usize> = readln::<Vec<usize>>().iter().map(|x| x-1).collect();
    let mut cost: Vec<i64> = vec![0; m*2 + 2];
    let mut num: Vec<i64> = vec![0; m*2 + 2];
    let mut all_cost: i64 = 0;
    for w in a.windows(2) {
        let (x, y) = (w[0], w[1]);
        let (u, v) = if x <= y {
            (x, y)
        } else {
            (x, y + m)
        };
        if u != v {
            let d = (v - u) as i64;
            all_cost += d;
            num[u+1] += 1;
            num[v+1] -= 1;
        }
    }
    let num_sum: Vec<i64> = num.iter()
        .scan(0, |s, &x| { *s = *s + x; Some(*s) })
        .collect();

    let mut cost_diff = num_sum.clone();

    for w in a.windows(2) {
        let (x, y) = (w[0], w[1]);
        let (u, v) = if x <= y {
            (x, y)
        } else {
            (x, y + m)
        };
        if u != v {
            let d = (v - u) as i64;
            cost_diff[v+1] -= d;
        }
    }

    let cost_sum: Vec<i64> = cost_diff.iter()
        .scan(0, |s, &x| { *s = *s + x; Some(*s) })
        .collect();
    //println!("{:?}", cost_sum);
    //println!("{:?}", num_sum);

    let mut ans = all_cost;
    //println!("allcost:{}", all_cost);
    let ans = all_cost - (0..m).map(|i| {
        cost_sum[i] + cost_sum[i+m] - num_sum[i] - num_sum[i+m]
    }).max().unwrap();
    println!("{}", ans);
}
/*pub struct XorShift4 {
    x: u32,
    y: u32,
    z: u32,
    w: u32,
}

impl XorShift4 {
    pub fn new() -> XorShift4 {
        XorShift4 { 
            x: 123456789,
             y: 362436069,
              z: 521288629,
               w: 88675123 }
    }

    fn next_u32(&mut self) -> u32 {
        let x = self.x;
        let t = x ^ (x << 11);
        self.x = self.y;
        self.y = self.z;
        self.z = self.w;
        let w = self.w;
        self.w = w ^ (w >> 19) ^ (t ^ (t >> 8));
        self.w
    }
}
fn f(from: i32, to: i32, m: i32) -> i32 {
    if from <= to {
        to - from
    } else {
        to + m - from
    }
}
fn compute(a: &Vec<i32>, base: i32, m: i32) -> i64 {
    let mut ans: i64 = 0;
    for i in 0..(a.len()-1) {
        let (x, y) = (a[i], a[i+1]);
        let (a1, a2) = (f(x, y, m), 1+f(base, y, m));
        ans += min(a1, a2) as i64;
    }
    ans
}
fn main() {
    let mut xorshift = XorShift4::new();
    let (_, m): (i32, i32) = readln();
    let a_: Vec<i32> = readln();
    let a: Vec<i32> = a_.iter().map(|x| x-1).collect();
    let mut answer: i64 = i64::max_value();
    let mut occur: HashMap<i32, i32> = HashMap::new();
    for x in a {
        let counter = occur.entry(x).or_insert(0);
        *counter += 1;
    }
    let mut v: Vec<(&i32, &i32)> = occur.iter().collect();
    v.sort_by_key(|&(x, y)| -y);

    for i in 0..5 {
        let mut cur = v[i].0;
        let mut d = 1;
        while d * 2 <= m {
            d *= 2;
        }
        loop {
            let (x, y, z) = (
                compute(&a, (cur+m-1)%m, m),
                compute(&a, cur, m),
                compute(&a, (cur+1)%m, m)
            );
            answer = min(min(answer, x), min(y, z));
            if x >= y && y <= z {
                break
            } else if y - x > y - z {
                // go to left
                cur = (cur + m - d) % m;
            } else {
                // go to right
                cur = (cur + m + d) % m;
            }
            if d != 1 {
                d /= 2;
            }
        }
    }
    println!("{}", answer);
}
*/

// --- template ---
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::HashMap;

pub trait FromLn {
    fn fromln(s: &str) -> Self;
}
pub fn readln<T: FromLn>() -> T {
    let mut buf = String::new();
    let _ = ::std::io::stdin().read_line(&mut buf).unwrap();
    T::fromln(buf.trim())
}
pub fn readlns<T: FromLn>(n: usize) -> Vec<T> {
    let mut vs = vec![];
    for _ in 0..n { vs.push(readln()); }
    vs
}
macro_rules! fromln_primitives {
    ($($t:ty),*) => { $(
        impl FromLn for $t {
            fn fromln(s: &str) -> $t {
                s.parse().unwrap()
            }
        }
    )* }
}
fromln_primitives!(String, bool, f32, f64, isize, i8, i16, i32, i64, usize, u8, u16, u32, u64);
impl<T> FromLn for Vec<T> where T: FromLn {
    fn fromln(s: &str) -> Vec<T> {
        s.split_whitespace().map(T::fromln).collect()
    }
}
impl FromLn for Vec<char> {
    fn fromln(s: &str) -> Vec<char> {
        s.chars().collect()
    }
}
macro_rules! fromln_tuple {
    ($($t:ident),*) => {
        impl<$($t),*> FromLn for ($($t),*) where $($t: FromLn),* {
            fn fromln(s: &str) -> ($($t),*) {
                let mut it = s.split_whitespace();
                let t = ($($t::fromln(it.next().unwrap())),*);
                assert_eq!(it.next(), None);
                t
            }
        }
    }
}
fromln_tuple!(A, B);
fromln_tuple!(A, B, C);
fromln_tuple!(A, B, C, D);
fromln_tuple!(A, B, C, D, E);
fromln_tuple!(A, B, C, D, E, F);

Measured memory_bytes: 18680