K, N = list(map(int, input().split()))

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



dA = []

for i in range(1, N):

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

    if i == N - 1:

        dA.append(A[0] - (A[i] - K))



dA = sorted(dA)

print((sum(dA[:N-1])))
# C - Traveling Salesman around Lake

K, N = list(map(int, input().split()))

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



dist = []

for i in range(N-1):

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

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

dist = sorted(dist, reverse=True)

print((sum(dist[1:])))
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
dA = []
for i in range(1, N):
    dA.append(A[i] - A[i - 1])
    if i == N - 1:
        dA.append(A[0] - (A[i] - K))
dA = sorted(dA)
print((sum(dA[: N - 1])))

# C - Traveling Salesman around Lake
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
dist = []
for i in range(N - 1):
    dist.append(abs(A[i] - A[i + 1]))
dist.append(abs(K - A[-1] + A[0]))
dist = sorted(dist, reverse=True)
print((sum(dist[1:])))

+# C - Traveling Salesman around Lake -dA = [] -for i in range(1, N): -    dA.append(A[i] - A[i - 1]) -    if i == N - 1: -        dA.append(A[0] - (A[i] - K)) -dA = sorted(dA) -print((sum(dA[: N - 1]))) +dist = [] +for i in range(N - 1): +    dist.append(abs(A[i] - A[i + 1])) +dist.append(abs(K - A[-1] + A[0])) +dist = sorted(dist, reverse=True) +print((sum(dist[1:])))