K,N=[int(x) for x in input().rstrip().split()]

A=[int(x) for x in input().rstrip().split()]



flag=1

now=0

mae=[]

back=[]

for i in range(N-1):

  mae.append(A[i+1]-A[i])

mae.append(A[len(A)-1]-A[0])



for i in range(N-1,0,-1):

  back.append(A[i]-A[i-1])

back.append(abs(A[0])+abs(K-A[N-1]))



back.sort()

mae.sort()

ans=min(sum(mae[:len(mae)-1]),sum(back[:len(back)-1]))

print(ans)
K,N=[int(x) for x in input().rstrip().split()]

A=[int(x) for x in input().rstrip().split()]



A14=K-A[N-1]+A[0]

sa=[A14]



for i in range(N-1):

  sa.append(A[i+1]-A[i])

sa.sort()

ans=0

for i in range(N-1):

  ans+=sa[i]

print(ans)
K, N = [int(x) for x in input().rstrip().split()]
A = [int(x) for x in input().rstrip().split()]
flag = 1
now = 0
mae = []
back = []
for i in range(N - 1):
    mae.append(A[i + 1] - A[i])
mae.append(A[len(A) - 1] - A[0])
for i in range(N - 1, 0, -1):
    back.append(A[i] - A[i - 1])
back.append(abs(A[0]) + abs(K - A[N - 1]))
back.sort()
mae.sort()
ans = min(sum(mae[: len(mae) - 1]), sum(back[: len(back) - 1]))
print(ans)

K, N = [int(x) for x in input().rstrip().split()]
A = [int(x) for x in input().rstrip().split()]
A14 = K - A[N - 1] + A[0]
sa = [A14]
for i in range(N - 1):
    sa.append(A[i + 1] - A[i])
sa.sort()
ans = 0
for i in range(N - 1):
    ans += sa[i]
print(ans)

-flag = 1 -now = 0 -mae = [] -back = [] +A14 = K - A[N - 1] + A[0] +sa = [A14] -    mae.append(A[i + 1] - A[i]) -mae.append(A[len(A) - 1] - A[0]) -for i in range(N - 1, 0, -1): -    back.append(A[i] - A[i - 1]) -back.append(abs(A[0]) + abs(K - A[N - 1])) -back.sort() -mae.sort() -ans = min(sum(mae[: len(mae) - 1]), sum(back[: len(back) - 1])) +    sa.append(A[i + 1] - A[i]) +sa.sort() +ans = 0 +for i in range(N - 1): +    ans += sa[i]