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

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



x = [0]*n

for i in range(n):

  t = a[i] - a[i-1]

  if i == 0:

    t += k

    x[i] = t

  x[i] = t



print((sum(i for i in sorted(x)[:-1])))
k,n = list(map(int, input().split()))

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



l = [0]*n

l[-1] = k+a[0]-a[-1]

for i in range(n-1):

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

print((sum(sorted(l)[:-1])))
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
x = [0] * n
for i in range(n):
    t = a[i] - a[i - 1]
    if i == 0:
        t += k
        x[i] = t
    x[i] = t
print((sum(i for i in sorted(x)[:-1])))

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

-x = [0] * n -for i in range(n): -    t = a[i] - a[i - 1] -    if i == 0: -        t += k -        x[i] = t -    x[i] = t -print((sum(i for i in sorted(x)[:-1]))) +l = [0] * n +l[-1] = k + a[0] - a[-1] +for i in range(n - 1): +    l[i] = a[i + 1] - a[i] +print((sum(sorted(l)[:-1])))