k, n = list(map(int, input().split()))

a = list(map(int, input().rstrip().split()))



temp = a[0] + k - a[n-1]



for i in range(n-1):

  temp = max(temp, a[i+1] - a[i])



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

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

B = []

for i in range(N-1):

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

  B.append(temp)

B.append(K-A[-1]+A[0])

B.sort()

#print(B)

ans = K-B[-1]

print(ans)

k, n = list(map(int, input().split()))
a = list(map(int, input().rstrip().split()))
temp = a[0] + k - a[n - 1]
for i in range(n - 1):
    temp = max(temp, a[i + 1] - a[i])
print((k - temp))

K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
B = []
for i in range(N - 1):
    temp = A[i + 1] - A[i]
    B.append(temp)
B.append(K - A[-1] + A[0])
B.sort()
# print(B)
ans = K - B[-1]
print(ans)

-k, n = list(map(int, input().split())) -a = list(map(int, input().rstrip().split())) -temp = a[0] + k - a[n - 1] -for i in range(n - 1): -    temp = max(temp, a[i + 1] - a[i]) -print((k - temp)) +K, N = list(map(int, input().split())) +A = list(map(int, input().split())) +B = [] +for i in range(N - 1): +    temp = A[i + 1] - A[i] +    B.append(temp) +B.append(K - A[-1] + A[0]) +B.sort() +# print(B) +ans = K - B[-1] +print(ans)