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

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

for n in range(N):

  A.append(-(K-A[n]))

A.sort()

res = K

for n in range(N):

  res = min(A[n+N-1] - A[n], res)

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

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

maxNum = 0

A.append(A[0] + K)

for i in range(N):

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

print((K - maxNum))
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
for n in range(N):
    A.append(-(K - A[n]))
A.sort()
res = K
for n in range(N):
    res = min(A[n + N - 1] - A[n], res)
print(res)

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

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