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

 

A=[]

 

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



D=[]



for i in range(N-1):

  D.append(int(A[i+1])-int(A[i]))

D.append(K-int(A[N-1])+int(A[0]))



Dmax=0

Sum=0

for i in range(N):

  if int(D[i])>Dmax:

    Dmax=int(D[i])

  Sum=Sum+int(D[i])   

  

print((Sum-Dmax))
K,N=list(map(int, input().split()))



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

D=[]

for i in range(1,N):

  D.append(A[i]-A[i-1])

D.append(A[0]+K-A[N-1])



print((sum(D)-max(D)))
K, N = list(map(int, input().split()))
A = []
A = list(map(int, input().split()))
D = []
for i in range(N - 1):
    D.append(int(A[i + 1]) - int(A[i]))
D.append(K - int(A[N - 1]) + int(A[0]))
Dmax = 0
Sum = 0
for i in range(N):
    if int(D[i]) > Dmax:
        Dmax = int(D[i])
    Sum = Sum + int(D[i])
print((Sum - Dmax))

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

-A = [] -for i in range(N - 1): -    D.append(int(A[i + 1]) - int(A[i])) -D.append(K - int(A[N - 1]) + int(A[0])) -Dmax = 0 -Sum = 0 -for i in range(N): -    if int(D[i]) > Dmax: -        Dmax = int(D[i]) -    Sum = Sum + int(D[i]) -print((Sum - Dmax)) +for i in range(1, N): +    D.append(A[i] - A[i - 1]) +D.append(A[0] + K - A[N - 1]) +print((sum(D) - max(D)))