import numpy as np



K, N = [int(i) for i in input().rstrip().split(' ')]

A = [int(i) for i in input().rstrip().split(' ')]



x = A[0]

y = K - A[-1]

n = [int(A[i+1] - A[i]) for i in range(N-1)]

n.append(x+y)



np_list = np.array(n)

n.pop(np_list.argmax())

print((sum(n)))
k,n = list(map(int,input().split()))

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



dp = [0 for i in range(n)]

dp[0] = abs(k-a[-1]) + a[0]



for i in range(1,n):

    dp[i] = abs(a[i]-a[i-1])



print((sum(dp) - max(dp)))
import numpy as np

K, N = [int(i) for i in input().rstrip().split(" ")]
A = [int(i) for i in input().rstrip().split(" ")]
x = A[0]
y = K - A[-1]
n = [int(A[i + 1] - A[i]) for i in range(N - 1)]
n.append(x + y)
np_list = np.array(n)
n.pop(np_list.argmax())
print((sum(n)))

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
dp = [0 for i in range(n)]
dp[0] = abs(k - a[-1]) + a[0]
for i in range(1, n):
    dp[i] = abs(a[i] - a[i - 1])
print((sum(dp) - max(dp)))

-import numpy as np - -K, N = [int(i) for i in input().rstrip().split(" ")] -A = [int(i) for i in input().rstrip().split(" ")] -x = A[0] -y = K - A[-1] -n = [int(A[i + 1] - A[i]) for i in range(N - 1)] -n.append(x + y) -np_list = np.array(n) -n.pop(np_list.argmax()) -print((sum(n))) +k, n = list(map(int, input().split())) +a = list(map(int, input().split())) +dp = [0 for i in range(n)] +dp[0] = abs(k - a[-1]) + a[0] +for i in range(1, n): +    dp[i] = abs(a[i] - a[i - 1]) +print((sum(dp) - max(dp)))