first = eval(input())

first = first.split()



K = int(first[0])

N = int(first[1])



second = eval(input())

second = second.split()



distance = []

diff = 0



for i in range(0, N):

    if i == N-1:

        diff0 = 0

        diff1 = 0

        diff0 = int(second[0])

        diff1 = K - int(second[i])

        diff = diff0 + diff1

        distance.append(diff)

    else:

        diff = int(second[i+1]) - int(second[i])

        distance.append(diff)

        

MaxIndex = distance.index(max(distance))

distance.pop(MaxIndex)

Max = sum(distance)

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

 

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



distance = []

diff = 0



for i in range(0, N):

    

    if i == N-1:

        diff = K + A_list[0] - A_list[i]

        distance.append(diff)

        

    else:

        diff = A_list[i+1] - A_list[i]

        distance.append(diff)

        



print((sum(distance) - max(distance)))
first = eval(input())
first = first.split()
K = int(first[0])
N = int(first[1])
second = eval(input())
second = second.split()
distance = []
diff = 0
for i in range(0, N):
    if i == N - 1:
        diff0 = 0
        diff1 = 0
        diff0 = int(second[0])
        diff1 = K - int(second[i])
        diff = diff0 + diff1
        distance.append(diff)
    else:
        diff = int(second[i + 1]) - int(second[i])
        distance.append(diff)
MaxIndex = distance.index(max(distance))
distance.pop(MaxIndex)
Max = sum(distance)
print(Max)

K, N = list(map(int, input().split()))
A_list = list(map(int, input().split()))
distance = []
diff = 0
for i in range(0, N):
    if i == N - 1:
        diff = K + A_list[0] - A_list[i]
        distance.append(diff)
    else:
        diff = A_list[i + 1] - A_list[i]
        distance.append(diff)
print((sum(distance) - max(distance)))

-first = eval(input()) -first = first.split() -K = int(first[0]) -N = int(first[1]) -second = eval(input()) -second = second.split() +K, N = list(map(int, input().split())) +A_list = list(map(int, input().split())) -        diff0 = 0 -        diff1 = 0 -        diff0 = int(second[0]) -        diff1 = K - int(second[i]) -        diff = diff0 + diff1 +        diff = K + A_list[0] - A_list[i] -        diff = int(second[i + 1]) - int(second[i]) +        diff = A_list[i + 1] - A_list[i] -MaxIndex = distance.index(max(distance)) -distance.pop(MaxIndex) -Max = sum(distance) -print(Max) +print((sum(distance) - max(distance)))