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

A = [int(i) for i in input().split()]



A_dist = [0 for i in range(N)]

for i in range(N):

    if i == 0:

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

    else:

        A_dist[i] = A[i] - A[i-1]



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

A = [int(i) for i in input().split()]



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

for i in range(1, N):

    if A[i] - A[i-1] > max_dist:

        max_dist = A[i] - A[i-1]



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

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

-A_dist = [0 for i in range(N)] -for i in range(N): -    if i == 0: -        A_dist[0] = A[0] + (K - A[-1]) -    else: -        A_dist[i] = A[i] - A[i - 1] -print((K - max(A_dist))) +max_dist = A[0] + (K - A[-1]) +for i in range(1, N): +    if A[i] - A[i - 1] > max_dist: +        max_dist = A[i] - A[i - 1] +print((K - max_dist))