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

m = list(map(float, input().split()))

half_k = k / 2

min = float(1000000)

for i in range(int(n)):

    first = m[i]

    last = m[i-1]

    len = last - first

    if len < 0:

        len = len + k

    if min > len:

        min = len

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

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



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

for i in range(len(A)-1):

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

  if d > max:

    max = d

print((K-max))
k, n = list(map(float, input().split()))
m = list(map(float, input().split()))
half_k = k / 2
min = float(1000000)
for i in range(int(n)):
    first = m[i]
    last = m[i - 1]
    len = last - first
    if len < 0:
        len = len + k
    if min > len:
        min = len
print((int(min)))

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

-k, n = list(map(float, input().split())) -m = list(map(float, input().split())) -half_k = k / 2 -min = float(1000000) -for i in range(int(n)): -    first = m[i] -    last = m[i - 1] -    len = last - first -    if len < 0: -        len = len + k -    if min > len: -        min = len -print((int(min))) +K, N = list(map(int, input().split())) +A = list(map(int, input().split())) +max = A[0] + (K - A[-1]) +for i in range(len(A) - 1): +    d = A[i + 1] - A[i] +    if d > max: +        max = d +print((K - max))