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

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



for i in range(0,List[1]):

    A.append(A[i]+List[0])



D = []

for i in range(0,List[1]):

    D.append(A[i+List[1]-1]-A[i])



print((min(D)))
List = list(map(int, input().split()))

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



D = []

for i in range(1,List[1]):

    D.append(A[i]-A[i-1])



D.append(A[0]+List[0]-A[List[1]-1])



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

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

-for i in range(0, List[1]): -    A.append(A[i] + List[0]) -for i in range(0, List[1]): -    D.append(A[i + List[1] - 1] - A[i]) -print((min(D))) +for i in range(1, List[1]): +    D.append(A[i] - A[i - 1]) +D.append(A[0] + List[0] - A[List[1] - 1]) +print((List[0] - max(D)))