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

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

min_num = k

for i in range(len(lst)):

    if i == 0:

        tmp = min(lst[0] + (k - lst[1]), lst[-1] - lst[0])

    elif i == n - 1:

        tmp = min(lst[-1] - lst[0], (k - lst[-1]) + lst[n - 2])

    else:

        tmp = min(lst[i] + (k - lst[i + 1]), (k - lst[i]) + lst[i - 1])

    if tmp < min_num:

        min_num = tmp



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

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

dis = [0] * n

for i in range(n):

    if i == n - 1:

        l = k + A[0] - A[i]

        dis[i] = l

    else:

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

        dis[i] = l



print((k - max(dis)))
k, n = list(map(int, input().split()))
lst = list(map(int, input().split()))
min_num = k
for i in range(len(lst)):
    if i == 0:
        tmp = min(lst[0] + (k - lst[1]), lst[-1] - lst[0])
    elif i == n - 1:
        tmp = min(lst[-1] - lst[0], (k - lst[-1]) + lst[n - 2])
    else:
        tmp = min(lst[i] + (k - lst[i + 1]), (k - lst[i]) + lst[i - 1])
    if tmp < min_num:
        min_num = tmp
print(min_num)

k, n = list(map(int, input().split()))
A = list(map(int, input().split()))
dis = [0] * n
for i in range(n):
    if i == n - 1:
        l = k + A[0] - A[i]
        dis[i] = l
    else:
        l = A[i + 1] - A[i]
        dis[i] = l
print((k - max(dis)))

-lst = list(map(int, input().split())) -min_num = k -for i in range(len(lst)): -    if i == 0: -        tmp = min(lst[0] + (k - lst[1]), lst[-1] - lst[0]) -    elif i == n - 1: -        tmp = min(lst[-1] - lst[0], (k - lst[-1]) + lst[n - 2]) +A = list(map(int, input().split())) +dis = [0] * n +for i in range(n): +    if i == n - 1: +        l = k + A[0] - A[i] +        dis[i] = l -        tmp = min(lst[i] + (k - lst[i + 1]), (k - lst[i]) + lst[i - 1]) -    if tmp < min_num: -        min_num = tmp -print(min_num) +        l = A[i + 1] - A[i] +        dis[i] = l +print((k - max(dis)))