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

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



A.sort()



tmp = 0

for i in range(N-1):

  tmp = max(tmp, A[i+1]-A[i])

  

tmp = max(tmp, K + A[0] - A[-1])



print((K - tmp))

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

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



tmp = [0 for _ in range(N)]

for i in range(N-1):

    tmp[i] = A[i+1] - A[i]



tmp[-1] = K - A[-1] + A[0]



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

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

-A.sort() -tmp = 0 +tmp = [0 for _ in range(N)] -    tmp = max(tmp, A[i + 1] - A[i]) -tmp = max(tmp, K + A[0] - A[-1]) -print((K - tmp)) +    tmp[i] = A[i + 1] - A[i] +tmp[-1] = K - A[-1] + A[0] +print((K - max(tmp)))