import sys

input = sys.stdin.readline



class BIT:

    def __init__(self, size):

        self.bit = [0] * (size + 1)

        self.size = size



    def sum(self, i):

        i += 1

        s = 0

        while i > 0:

            s += self.bit[i]

            i -= i & -i

        return s



    def add(self, i, x):

        i += 1

        while i <= self.size:

            self.bit[i] += x

            i += i & -i



n, m = list(map(int, input().split()))

a = list([int(x) - 1 for x in input().split()])



bit0 = BIT(m)

bit1 = BIT(m)

for i in range(n-1):

    x, y = a[i], a[i+1]

    bit1.add(0, (y - x) % m)

    if (y - x) % m < 2:

        continue

    x = (x + 2) % m

    if x <= y:

        bit0.add(x, -1)

        bit1.add(x, x - 1)

        bit0.add(y + 1, 1)

        bit1.add(y + 1, -(x - 1))

    else:

        bit0.add(x, -1)

        bit1.add(x, x - 1)



        b = (0 - (x - 1)) % m

        bit0.add(0, -1)

        bit1.add(0, -b)

        bit0.add(y + 1, 1)

        bit1.add(y + 1, b)



ans = min(bit0.sum(i) * i + bit1.sum(i) for i in range(m))

print(ans)

from itertools import accumulate

import sys

input = sys.stdin.readline



n, m = list(map(int, input().split()))

a = list([int(x) - 1 for x in input().split()])



cnt0 = [0] * m

cnt1 = [0] * m

for i in range(n-1):

    cnt0[a[i+1]] += (a[i+1] - a[i]) % m - 1

    if (a[i] + 1) % m <= a[i+1]:

        cnt1[(a[i]+1) % m] += 1

        cnt1[a[i+1]] -= 1

    else:

        cnt1[(a[i]+1) % m] += 1

        cnt1[0] += 1

        cnt1[a[i+1]] -= 1

cnt1 = list(accumulate(cnt1))

ans = float('inf')

tmp = 0

for i in range(n-1):

    tmp += min((a[i+1] - a[i]) % m, a[i+1] + 1)

ans = tmp

for i in range(m-1):

    tmp += cnt0[i] - cnt1[i]

    ans = min(ans, tmp)

print(ans)



import sys

input = sys.stdin.readline


class BIT:
    def __init__(self, size):
        self.bit = [0] * (size + 1)
        self.size = size

    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.bit[i]
            i -= i & -i
        return s

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.bit[i] += x
            i += i & -i


n, m = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
bit0 = BIT(m)
bit1 = BIT(m)
for i in range(n - 1):
    x, y = a[i], a[i + 1]
    bit1.add(0, (y - x) % m)
    if (y - x) % m < 2:
        continue
    x = (x + 2) % m
    if x <= y:
        bit0.add(x, -1)
        bit1.add(x, x - 1)
        bit0.add(y + 1, 1)
        bit1.add(y + 1, -(x - 1))
    else:
        bit0.add(x, -1)
        bit1.add(x, x - 1)
        b = (0 - (x - 1)) % m
        bit0.add(0, -1)
        bit1.add(0, -b)
        bit0.add(y + 1, 1)
        bit1.add(y + 1, b)
ans = min(bit0.sum(i) * i + bit1.sum(i) for i in range(m))
print(ans)

from itertools import accumulate
import sys

input = sys.stdin.readline
n, m = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
cnt0 = [0] * m
cnt1 = [0] * m
for i in range(n - 1):
    cnt0[a[i + 1]] += (a[i + 1] - a[i]) % m - 1
    if (a[i] + 1) % m <= a[i + 1]:
        cnt1[(a[i] + 1) % m] += 1
        cnt1[a[i + 1]] -= 1
    else:
        cnt1[(a[i] + 1) % m] += 1
        cnt1[0] += 1
        cnt1[a[i + 1]] -= 1
cnt1 = list(accumulate(cnt1))
ans = float("inf")
tmp = 0
for i in range(n - 1):
    tmp += min((a[i + 1] - a[i]) % m, a[i + 1] + 1)
ans = tmp
for i in range(m - 1):
    tmp += cnt0[i] - cnt1[i]
    ans = min(ans, tmp)
print(ans)

+from itertools import accumulate - - -class BIT: -    def __init__(self, size): -        self.bit = [0] * (size + 1) -        self.size = size - -    def sum(self, i): -        i += 1 -        s = 0 -        while i > 0: -            s += self.bit[i] -            i -= i & -i -        return s - -    def add(self, i, x): -        i += 1 -        while i <= self.size: -            self.bit[i] += x -            i += i & -i - - -bit0 = BIT(m) -bit1 = BIT(m) +cnt0 = [0] * m +cnt1 = [0] * m -    x, y = a[i], a[i + 1] -    bit1.add(0, (y - x) % m) -    if (y - x) % m < 2: -        continue -    x = (x + 2) % m -    if x <= y: -        bit0.add(x, -1) -        bit1.add(x, x - 1) -        bit0.add(y + 1, 1) -        bit1.add(y + 1, -(x - 1)) +    cnt0[a[i + 1]] += (a[i + 1] - a[i]) % m - 1 +    if (a[i] + 1) % m <= a[i + 1]: +        cnt1[(a[i] + 1) % m] += 1 +        cnt1[a[i + 1]] -= 1 -        bit0.add(x, -1) -        bit1.add(x, x - 1) -        b = (0 - (x - 1)) % m -        bit0.add(0, -1) -        bit1.add(0, -b) -        bit0.add(y + 1, 1) -        bit1.add(y + 1, b) -ans = min(bit0.sum(i) * i + bit1.sum(i) for i in range(m)) +        cnt1[(a[i] + 1) % m] += 1 +        cnt1[0] += 1 +        cnt1[a[i + 1]] -= 1 +cnt1 = list(accumulate(cnt1)) +ans = float("inf") +tmp = 0 +for i in range(n - 1): +    tmp += min((a[i + 1] - a[i]) % m, a[i + 1] + 1) +ans = tmp +for i in range(m - 1): +    tmp += cnt0[i] - cnt1[i] +    ans = min(ans, tmp)