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

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

house = house + house

min = house[N - 1] - house[0]

 

for i in range(N - 1):

  if K - (house[N + 1 + i] - house[i]) < min:

    min = K - (house[N + 1 + i] - house[i])

 

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

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

min = house[N - 1] - house[0]

 

for i in range(N - 1):

  if K - (house[i + 1] - house[i]) < min:

    min = K - (house[i + 1] - house[i])

 

print(min)
K, N = list(map(int, input().split()))
house = list(map(int, input().split()))
house = house + house
min = house[N - 1] - house[0]
for i in range(N - 1):
    if K - (house[N + 1 + i] - house[i]) < min:
        min = K - (house[N + 1 + i] - house[i])
print(min)

K, N = list(map(int, input().split()))
house = list(map(int, input().split()))
min = house[N - 1] - house[0]
for i in range(N - 1):
    if K - (house[i + 1] - house[i]) < min:
        min = K - (house[i + 1] - house[i])
print(min)

-house = house + house -    if K - (house[N + 1 + i] - house[i]) < min: -        min = K - (house[N + 1 + i] - house[i]) +    if K - (house[i + 1] - house[i]) < min: +        min = K - (house[i + 1] - house[i])