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

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

s=0

b=[0]*(n-1)

for i in range(n-1):

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

  

b.append(k+a[0]-a[n-1])



c=sorted(b,reverse=True)

for i in range(1,n):

  s+=c[i]

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

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

b=[]

for i in range(len(a)-1):

  b.append(a[i+1]-a[i])

  

b.append(abs(a[-1]-a[0]-k))



b.sort()

b[-1]=0

print((sum(b)))
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
s = 0
b = [0] * (n - 1)
for i in range(n - 1):
    b[i] = a[i + 1] - a[i]
b.append(k + a[0] - a[n - 1])
c = sorted(b, reverse=True)
for i in range(1, n):
    s += c[i]
print(s)

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
for i in range(len(a) - 1):
    b.append(a[i + 1] - a[i])
b.append(abs(a[-1] - a[0] - k))
b.sort()
b[-1] = 0
print((sum(b)))

-s = 0 -b = [0] * (n - 1) -for i in range(n - 1): -    b[i] = a[i + 1] - a[i] -b.append(k + a[0] - a[n - 1]) -c = sorted(b, reverse=True) -for i in range(1, n): -    s += c[i] -print(s) +b = [] +for i in range(len(a) - 1): +    b.append(a[i + 1] - a[i]) +b.append(abs(a[-1] - a[0] - k)) +b.sort() +b[-1] = 0 +print((sum(b)))