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

a = list(map(int,input().split()))



ex = [0] * m

left = [0] * (2*m)

right = [[0,0] for _ in range(2*m)]



ans = 0

for i in range(n-1):

    a0 = a[i]-1

    a1 = a[i+1]-1

    ans += (a1-a0)%m

    left[a0+1] += 1

    right[a1 + (a0>a1)*m][0] += 1

    right[a1 + (a0>a1)*m][1] += (a1-a0)%m -1



now = left[0]

dif = 0

for i in range(1,2*m):

    dif += now

    ex[i%m] -= dif

    now += left[i] - right[i][0]

    dif -= right[i][1]



ans += min(ex)

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

a = list(map(int,input().split()))



imos = [0] * (2*m+2)

ans = 0

for i in range(n-1):

    a0 = a[i]-1

    a1 = a[i+1]-1

    dif = (a1-a0)%m

    ans += dif

    if(dif>=2):

        imos[a0+2] += 1

        imos[a0+dif+1] -= dif

        imos[a0+dif+2] += dif-1



# print(imos)



for i in range(1,2*m+2):

    imos[i] += imos[i-1]

# print(imos)



for i in range(1,2*m+2):

    imos[i] += imos[i-1]

# print(imos)



for i in range(m):

    imos[i] += imos[i+m]



ans -= max(imos[:m])

print(ans)
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
ex = [0] * m
left = [0] * (2 * m)
right = [[0, 0] for _ in range(2 * m)]
ans = 0
for i in range(n - 1):
    a0 = a[i] - 1
    a1 = a[i + 1] - 1
    ans += (a1 - a0) % m
    left[a0 + 1] += 1
    right[a1 + (a0 > a1) * m][0] += 1
    right[a1 + (a0 > a1) * m][1] += (a1 - a0) % m - 1
now = left[0]
dif = 0
for i in range(1, 2 * m):
    dif += now
    ex[i % m] -= dif
    now += left[i] - right[i][0]
    dif -= right[i][1]
ans += min(ex)
print(ans)

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
imos = [0] * (2 * m + 2)
ans = 0
for i in range(n - 1):
    a0 = a[i] - 1
    a1 = a[i + 1] - 1
    dif = (a1 - a0) % m
    ans += dif
    if dif >= 2:
        imos[a0 + 2] += 1
        imos[a0 + dif + 1] -= dif
        imos[a0 + dif + 2] += dif - 1
# print(imos)
for i in range(1, 2 * m + 2):
    imos[i] += imos[i - 1]
# print(imos)
for i in range(1, 2 * m + 2):
    imos[i] += imos[i - 1]
# print(imos)
for i in range(m):
    imos[i] += imos[i + m]
ans -= max(imos[:m])
print(ans)

-ex = [0] * m -left = [0] * (2 * m) -right = [[0, 0] for _ in range(2 * m)] +imos = [0] * (2 * m + 2) -    ans += (a1 - a0) % m -    left[a0 + 1] += 1 -    right[a1 + (a0 > a1) * m][0] += 1 -    right[a1 + (a0 > a1) * m][1] += (a1 - a0) % m - 1 -now = left[0] -dif = 0 -for i in range(1, 2 * m): -    dif += now -    ex[i % m] -= dif -    now += left[i] - right[i][0] -    dif -= right[i][1] -ans += min(ex) +    dif = (a1 - a0) % m +    ans += dif +    if dif >= 2: +        imos[a0 + 2] += 1 +        imos[a0 + dif + 1] -= dif +        imos[a0 + dif + 2] += dif - 1 +# print(imos) +for i in range(1, 2 * m + 2): +    imos[i] += imos[i - 1] +# print(imos) +for i in range(1, 2 * m + 2): +    imos[i] += imos[i - 1] +# print(imos) +for i in range(m): +    imos[i] += imos[i + m] +ans -= max(imos[:m])