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: Time Limit Exceeded)

import static java.lang.Math.max;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;

public class Main {
    public static void main(String[] args) {
        FastScanner fsc = new FastScanner();
        int n = fsc.nextInt();
        int m = fsc.nextInt();
        LazySegmentTree t = new LazySegmentTree(new long[m], Monoid.ADD, Monoid.ADD, Long::sum, (laz, l) -> laz * l);
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = fsc.nextInt() - 1;
        }
        for (int i = 1; i < n; i++) {
            int to = a[i];
            int fr = a[i - 1];
            if (fr < to) {
                t.applyForSegment(fr + 2, to + 1, 1);
                t.applyForSegment(to + 1, to + 2, -(to - fr - 1));
            } else {
                t.applyForSegment(fr + 2, m, 1);
                t.applyForSegment(0, 1, m - fr - 1);
                t.applyForSegment(1, to + 1, 1);
                t.applyForSegment(to + 1, to + 2, -(to + m - fr - 1));
            }

            if (n % 100000 == 0) {
                System.gc();
            }
        }
        System.gc();
        long max = 0;
        for (int x = 0; x < m; x++) {
            max = max(max, t.query(0, x + 1));
        }
        long sum = 0;
        for (int i = 1; i < n; i++) {
            if (a[i] > a[i - 1]) {
                sum += a[i] - a[i - 1];
            } else {
                sum += a[i] + m - a[i - 1];
            }
        }
        System.out.println(sum - max);
    }
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte())
            return buffer[ptr++];
        else
            return -1;
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr]))
            ptr++;
        return hasNextByte();
    }

    public String next() {
        if (!hasNext())
            throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext())
            throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
            throw new NumberFormatException();
        return (int) nl;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}

class LazySegmentTree {
    private long[] tree;
    private long[] lazy;
    private final long e;
    private final long lazyIdElm;
    private final BinaryOperator<Long> operator;
    private final BinaryOperator<Long> lazyOperator;
    private final BinaryOperator<Long> appFunc;
    private final BiFunction<Long, Integer, Long> mergeLazy;
    private final int size;
    private final int leafs;
    private final int height;
    private final int baseOfLeaf;

    public LazySegmentTree(long[] t, Monoid<Long> monoid, Monoid<Long> lazyMonoid, BinaryOperator<Long> appFunc,
            BiFunction<Long, Integer, Long> mergeLazy) {
        int n = t.length;
        this.operator = monoid.operator;
        this.e = monoid.e;
        this.lazyOperator = lazyMonoid.operator;
        this.lazyIdElm = lazyMonoid.e;
        this.appFunc = appFunc;
        this.mergeLazy = mergeLazy;
        this.height = minimumExponent(n);
        this.leafs = 1 << this.height;
        this.baseOfLeaf = this.leafs - 1;
        this.size = 2 * this.leafs - 1;
        this.lazy = new long[this.size];
        Arrays.fill(this.lazy, this.lazyIdElm);
        this.tree = new long[this.size];
        for (int i = this.size - 1; i >= 0; i--) {
            if (i >= this.baseOfLeaf + n) {
                this.tree[i] = this.e;
            } else if (i >= baseOfLeaf) {
                this.tree[i] = t[i - this.baseOfLeaf];
            } else {
                this.tree[i] = this.operator.apply(this.tree[2 * i + 1], this.tree[2 * i + 2]);
            }
        }
    }

    public void applyForSegment(int l, int r, long v) {
        applyForSegment(l, r, 0, 0, leafs, v);
    }

    private void applyForSegment(int ql, int qr, int k, int tl, int tr, long v) {
        evaluate(k, tl, tr);
        if (qr > tl && tr > ql) {
            if (ql <= tl && tr <= qr) {
                lazy[k] = lazyOperator.apply(lazy[k], v);
                evaluate(k, tl, tr);
            } else {
                int left = 2 * k + 1;
                int right = 2 * k + 2;
                applyForSegment(ql, qr, left, tl, (tl + tr) >> 1, v);
                applyForSegment(ql, qr, right, (tl + tr) >> 1, tr, v);
                tree[k] = operator.apply(tree[left], tree[right]);
            }
        }
    }

    public long query(int l, int r) {
        return query(l, r, 0, 0, leafs);
    }

    private long query(int ql, int qr, int k, int tl, int tr) {
        evaluate(k, tl, tr);
        if (tr <= ql || qr <= tl) {
            return e;
        } else if (ql <= tl && tr <= qr) {
            return tree[k];
        } else {
            long left = query(ql, qr, 2 * k + 1, tl, (tl + tr) >> 1);
            long right = query(ql, qr, 2 * k + 2, (tl + tr) >> 1, tr);
            return operator.apply(left, right);
        }
    }

    private void evaluate(int k, int l, int r) {
        if (lazy[k] != lazyIdElm) {
            if (k < baseOfLeaf) {
                lazy[2 * k + 1] = lazyOperator.apply(lazy[2 * k + 1], lazy[k]);
                lazy[2 * k + 2] = lazyOperator.apply(lazy[2 * k + 2], lazy[k]);
            }
            tree[k] = appFunc.apply(tree[k], mergeLazy.apply(lazy[k], r - l));
            lazy[k] = lazyIdElm;
        }
    }

    private static int minimumExponent(int n) {
        return 31 - Integer.numberOfLeadingZeros(n) + ((-n & n) == n ? 0 : 1);
    }
}

class Monoid<T> {
    public static final Monoid<Long> ADD = new Monoid<>(0l, (u, v) -> u + v);
    public T e;
    public BinaryOperator<T> operator;

    public Monoid(T e, BinaryOperator<T> operator) {
        this.e = e;
        this.operator = operator;
    }
}