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

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

a2 = a + [aa + k for aa in a]

max_inter = 0

for i in range(n*2 - 1):

    max_inter = max(max_inter, a2[i+1] - a2[i])

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

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

max_inter = 0

max_inter = max(max_inter, arr[0]+k - arr[-1])

for i in range(n-1):

    max_inter = max(max_inter, arr[i+1] - arr[i])

print((k - max_inter))
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a2 = a + [aa + k for aa in a]
max_inter = 0
for i in range(n * 2 - 1):
    max_inter = max(max_inter, a2[i + 1] - a2[i])
print((k - max_inter))

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

-a = list(map(int, input().split())) -a2 = a + [aa + k for aa in a] +arr = list(map(int, input().split())) -for i in range(n * 2 - 1): -    max_inter = max(max_inter, a2[i + 1] - a2[i]) +max_inter = max(max_inter, arr[0] + k - arr[-1]) +for i in range(n - 1): +    max_inter = max(max_inter, arr[i + 1] - arr[i])