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

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

distance=max(A)-min(A)

for i in range(N):

    A.append(A[i]+K)

for i in range(N):

    distance=min(distance,A[i+N-1]-A[i])

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

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

A+=[A[0]+K]

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

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

-distance = max(A) - min(A) -for i in range(N): -    A.append(A[i] + K) -for i in range(N): -    distance = min(distance, A[i + N - 1] - A[i]) -print(distance) +A += [A[0] + K] +print((min(K - (A[i + 1] - A[i]) for i in range(N))))