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

A_ls = list(map(int, input().split(' ')))

max_l = -1

for i in range(N):

    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]

    if l < 0:

        l = K - A_ls[i] + A_ls[(i + 1) % len(A_ls)]

    if max_l == -1:

        max_l = l

    else:

        max_l = max(max_l, l)

print((K - max_l))
K, N = list(map(int, input().split(' ')))

A_ls = list(map(int, input().split(' ')))

max_l = 0

for i in range(N):

    l = A_ls[(i + 1) % N] - A_ls[i]

    if l < 0:

        l = K - A_ls[i] + A_ls[(i + 1) % N]

    max_l = max(max_l, l)

print((K - max_l))
K, N = list(map(int, input().split(" ")))
A_ls = list(map(int, input().split(" ")))
max_l = -1
for i in range(N):
    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % len(A_ls)]
    if max_l == -1:
        max_l = l
    else:
        max_l = max(max_l, l)
print((K - max_l))

K, N = list(map(int, input().split(" ")))
A_ls = list(map(int, input().split(" ")))
max_l = 0
for i in range(N):
    l = A_ls[(i + 1) % N] - A_ls[i]
    if l < 0:
        l = K - A_ls[i] + A_ls[(i + 1) % N]
    max_l = max(max_l, l)
print((K - max_l))

-max_l = -1 +max_l = 0 -    l = A_ls[(i + 1) % len(A_ls)] - A_ls[i] +    l = A_ls[(i + 1) % N] - A_ls[i] -        l = K - A_ls[i] + A_ls[(i + 1) % len(A_ls)] -    if max_l == -1: -        max_l = l -    else: -        max_l = max(max_l, l) +        l = K - A_ls[i] + A_ls[(i + 1) % N] +    max_l = max(max_l, l)