(K, N) = list(map(int, input().split()))

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

A.append(A[0]+K)

A_next = []



max = 0

result = 0

j = 0

k = 0

#print(A)

for x in range(1, len(A)):

  #print(x)

  #print(A[x]-A[x-1])

  #print(A[x-1])

  #print(A_next)

  A_next.append(abs(A[x] - A[x-1]))

  if max < A_next[j]:

    max = A_next[j]

    #print(max)

    k = j

  j += 1

#print(k)

#print(A_next)

for x in range(len(A_next)):

  if x == k:

    pass

  else:

    result += A_next[x]

print(result)





    
N, K = list(map(int, input().split()))

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

a.append(N+a[0])

#print(a)

max = 0

for i in range(len(a)-1):

  p = abs(a[i]-a[i+1])

  max = p if max < p else max

print((N-max))

(K, N) = list(map(int, input().split()))
A = list(map(int, input().split()))
A.append(A[0] + K)
A_next = []
max = 0
result = 0
j = 0
k = 0
# print(A)
for x in range(1, len(A)):
    # print(x)
    # print(A[x]-A[x-1])
    # print(A[x-1])
    # print(A_next)
    A_next.append(abs(A[x] - A[x - 1]))
    if max < A_next[j]:
        max = A_next[j]
        # print(max)
        k = j
    j += 1
# print(k)
# print(A_next)
for x in range(len(A_next)):
    if x == k:
        pass
    else:
        result += A_next[x]
print(result)

N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
a.append(N + a[0])
# print(a)
max = 0
for i in range(len(a) - 1):
    p = abs(a[i] - a[i + 1])
    max = p if max < p else max
print((N - max))

-(K, N) = list(map(int, input().split())) -A = list(map(int, input().split())) -A.append(A[0] + K) -A_next = [] +N, K = list(map(int, input().split())) +a = list(map(int, input().split())) +a.append(N + a[0]) +# print(a) -result = 0 -j = 0 -k = 0 -# print(A) -for x in range(1, len(A)): -    # print(x) -    # print(A[x]-A[x-1]) -    # print(A[x-1]) -    # print(A_next) -    A_next.append(abs(A[x] - A[x - 1])) -    if max < A_next[j]: -        max = A_next[j] -        # print(max) -        k = j -    j += 1 -# print(k) -# print(A_next) -for x in range(len(A_next)): -    if x == k: -        pass -    else: -        result += A_next[x] -print(result) +for i in range(len(a) - 1): +    p = abs(a[i] - a[i + 1]) +    max = p if max < p else max +print((N - max))