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

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

maxd = (K - A[-1]) + A[0]

for i in range(1, N):

    d = A[i] - A[i - 1]

    maxd = max(maxd, d)

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

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



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

for i in range(1, N):

    d = A[i] - A[i-1]

    maxd = max(maxd, d)

ans = K - maxd

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

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

-A = list(map(int, input().split())) -maxd = (K - A[-1]) + A[0] +A = tuple(map(int, input().split())) +maxd = K - A[-1] + A[0] -print((K - maxd)) +ans = K - maxd +print(ans)