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

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



for i in range(n-1):

	arr.append(arr[i]+k)



min_=99999999999



for i in range(n):

	if arr[i+n-1]-arr[i]<min_:

		min_=arr[i+n-1]-arr[i]





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

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





min_=99999999999



for i in range(n):

	if i==0:

		x=arr[-1]-arr[0]

	else:

		x=k-(arr[i]-arr[i-1])

	if x<min_:

		min_=x





print(min_)
k, n = list(map(int, input().split()))
arr = list(map(int, input().split()))
for i in range(n - 1):
    arr.append(arr[i] + k)
min_ = 99999999999
for i in range(n):
    if arr[i + n - 1] - arr[i] < min_:
        min_ = arr[i + n - 1] - arr[i]
print(min_)

k, n = list(map(int, input().split()))
arr = list(map(int, input().split()))
min_ = 99999999999
for i in range(n):
    if i == 0:
        x = arr[-1] - arr[0]
    else:
        x = k - (arr[i] - arr[i - 1])
    if x < min_:
        min_ = x
print(min_)

-for i in range(n - 1): -    arr.append(arr[i] + k) -    if arr[i + n - 1] - arr[i] < min_: -        min_ = arr[i + n - 1] - arr[i] +    if i == 0: +        x = arr[-1] - arr[0] +    else: +        x = k - (arr[i] - arr[i - 1]) +    if x < min_: +        min_ = x