import sys



K,N = list(map(int, sys.stdin.readline().strip().split()))

A = list(map(int, sys.stdin.readline().strip().split()))

# K = 20

# N = 3

# A =[0, 5, 15]

# A =[5, 10, 15]



distance = []

# 家間距離

for i,a in enumerate(A):

    if i == len(A) - 1:

        distance.append(K - A[i] + A[0])

    else:

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



# print(distance)

print((sum(distance) - max(distance)))

K,N,*A = list(map(int, open(0).read().split()))



ans = []

for i in range(N-1):

    ans.append(abs(A[i+1] - A[i]))



ans.append(A[0] + K - A[-1])

ans.sort()

ans.pop()



print((sum(ans)))
import sys

K, N = list(map(int, sys.stdin.readline().strip().split()))
A = list(map(int, sys.stdin.readline().strip().split()))
# K = 20
# N = 3
# A =[0, 5, 15]
# A =[5, 10, 15]
distance = []
# 家間距離
for i, a in enumerate(A):
    if i == len(A) - 1:
        distance.append(K - A[i] + A[0])
    else:
        distance.append(A[i + 1] - A[i])
# print(distance)
print((sum(distance) - max(distance)))

K, N, *A = list(map(int, open(0).read().split()))
ans = []
for i in range(N - 1):
    ans.append(abs(A[i + 1] - A[i]))
ans.append(A[0] + K - A[-1])
ans.sort()
ans.pop()
print((sum(ans)))

-import sys - -K, N = list(map(int, sys.stdin.readline().strip().split())) -A = list(map(int, sys.stdin.readline().strip().split())) -# K = 20 -# N = 3 -# A =[0, 5, 15] -# A =[5, 10, 15] -distance = [] -# 家間距離 -for i, a in enumerate(A): -    if i == len(A) - 1: -        distance.append(K - A[i] + A[0]) -    else: -        distance.append(A[i + 1] - A[i]) -# print(distance) -print((sum(distance) - max(distance))) +K, N, *A = list(map(int, open(0).read().split())) +ans = [] +for i in range(N - 1): +    ans.append(abs(A[i + 1] - A[i])) +ans.append(A[0] + K - A[-1]) +ans.sort() +ans.pop() +print((sum(ans)))