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

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



res = []

for i in range(n-1):

  j = a[i+1] - a[i]

  res.append(j)

l = k + a[0] - a[n-1]

res.append(l)

m = max(res)

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

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

a.sort()

data=[0]*n

for i in range(n-1):

  data[i]=a[i+1]-a[i]

data[n-1]=k+a[0]-a[n-1]

print((sum(data)-max(data)))
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
res = []
for i in range(n - 1):
    j = a[i + 1] - a[i]
    res.append(j)
l = k + a[0] - a[n - 1]
res.append(l)
m = max(res)
print((k - m))

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
data = [0] * n
for i in range(n - 1):
    data[i] = a[i + 1] - a[i]
data[n - 1] = k + a[0] - a[n - 1]
print((sum(data) - max(data)))

-res = [] +a.sort() +data = [0] * n -    j = a[i + 1] - a[i] -    res.append(j) -l = k + a[0] - a[n - 1] -res.append(l) -m = max(res) -print((k - m)) +    data[i] = a[i + 1] - a[i] +data[n - 1] = k + a[0] - a[n - 1] +print((sum(data) - max(data)))