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

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



ans = 0

maxi = 0



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

    ans += abs(A[i]-A[i-1])

    if maxi < abs(A[i]-A[i-1]): maxi = abs(A[i]-A[i-1])

    

if A[0]==0:

    if maxi < N-A[-1]: maxi = N-A[-1]

    ans += N-A[-1]

else :

    if maxi < N-A[-1]+A[0]: maxi = N-A[-1]+A[0]

    ans += N-A[-1]+A[0]

    

print((ans-maxi))

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

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



A.append(A[0]+N)

l = 0

for i in range(1, K+1):

    l = max(l,A[i]-A[i-1])



print((N-l))
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 0
maxi = 0
for i in range(1, len(A)):
    ans += abs(A[i] - A[i - 1])
    if maxi < abs(A[i] - A[i - 1]):
        maxi = abs(A[i] - A[i - 1])
if A[0] == 0:
    if maxi < N - A[-1]:
        maxi = N - A[-1]
    ans += N - A[-1]
else:
    if maxi < N - A[-1] + A[0]:
        maxi = N - A[-1] + A[0]
    ans += N - A[-1] + A[0]
print((ans - maxi))

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.append(A[0] + N)
l = 0
for i in range(1, K + 1):
    l = max(l, A[i] - A[i - 1])
print((N - l))

-ans = 0 -maxi = 0 -for i in range(1, len(A)): -    ans += abs(A[i] - A[i - 1]) -    if maxi < abs(A[i] - A[i - 1]): -        maxi = abs(A[i] - A[i - 1]) -if A[0] == 0: -    if maxi < N - A[-1]: -        maxi = N - A[-1] -    ans += N - A[-1] -else: -    if maxi < N - A[-1] + A[0]: -        maxi = N - A[-1] + A[0] -    ans += N - A[-1] + A[0] -print((ans - maxi)) +A.append(A[0] + N) +l = 0 +for i in range(1, K + 1): +    l = max(l, A[i] - A[i - 1]) +print((N - l))