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

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

for i in range(N):

  A.append(A[i]+K)

d = [0]*(N)

max_d, max_i = 0, -1

for i in range(N):

  d[i] = A[i+1] - A[i]

  if d[i] > max_d:

    max_d = d[i]

    max_i = i + 1

ans = 0

for i in range(N-1):

  ans += A[i+max_i+1] - A[i+max_i]

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

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

A.append(A[0] + K)

d = 0

for i in range(N):

	d = max(d, A[i+1] - A[i])

print((K - d))
K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
for i in range(N):
    A.append(A[i] + K)
d = [0] * (N)
max_d, max_i = 0, -1
for i in range(N):
    d[i] = A[i + 1] - A[i]
    if d[i] > max_d:
        max_d = d[i]
        max_i = i + 1
ans = 0
for i in range(N - 1):
    ans += A[i + max_i + 1] - A[i + max_i]
print(ans)

K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
A.append(A[0] + K)
d = 0
for i in range(N):
    d = max(d, A[i + 1] - A[i])
print((K - d))

+A.append(A[0] + K) +d = 0 -    A.append(A[i] + K) -d = [0] * (N) -max_d, max_i = 0, -1 -for i in range(N): -    d[i] = A[i + 1] - A[i] -    if d[i] > max_d: -        max_d = d[i] -        max_i = i + 1 -ans = 0 -for i in range(N - 1): -    ans += A[i + max_i + 1] - A[i + max_i] -print(ans) +    d = max(d, A[i + 1] - A[i]) +print((K - d))