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



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



start = 0

max_sa = 0

for i in range(n):

	if i+1 == n:

		sa = a[(i+1)%n] - (a[i%n]-k)

	else:

		sa = a[(i+1)%n] - a[i%n]



	if max_sa < sa:

		max_sa = sa

		start = (i+1)%n



ans = 0

for i in range(n-1):

	if start+i+1 == n:

		ans += a[(start+i+1)%n] - (a[(start+i)%n]-k)

	else:

		ans += a[(start+i+1)%n] - a[(start+i)%n]



print(ans)

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

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



r = [(a[0]-a[-1])%k]

l = [(a[-1]-a[0])%k]

for i in range(1,n):

	r.append(a[i] - a[i-1])

	l.append(abs(a[-i-1] - a[-i]))



sum_l = sum(l)

sum_r = sum(r)



print((min(sum_l-max(l),sum_r-max(r))))



k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
start = 0
max_sa = 0
for i in range(n):
    if i + 1 == n:
        sa = a[(i + 1) % n] - (a[i % n] - k)
    else:
        sa = a[(i + 1) % n] - a[i % n]
    if max_sa < sa:
        max_sa = sa
        start = (i + 1) % n
ans = 0
for i in range(n - 1):
    if start + i + 1 == n:
        ans += a[(start + i + 1) % n] - (a[(start + i) % n] - k)
    else:
        ans += a[(start + i + 1) % n] - a[(start + i) % n]
print(ans)

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
r = [(a[0] - a[-1]) % k]
l = [(a[-1] - a[0]) % k]
for i in range(1, n):
    r.append(a[i] - a[i - 1])
    l.append(abs(a[-i - 1] - a[-i]))
sum_l = sum(l)
sum_r = sum(r)
print((min(sum_l - max(l), sum_r - max(r))))

-start = 0 -max_sa = 0 -for i in range(n): -    if i + 1 == n: -        sa = a[(i + 1) % n] - (a[i % n] - k) -    else: -        sa = a[(i + 1) % n] - a[i % n] -    if max_sa < sa: -        max_sa = sa -        start = (i + 1) % n -ans = 0 -for i in range(n - 1): -    if start + i + 1 == n: -        ans += a[(start + i + 1) % n] - (a[(start + i) % n] - k) -    else: -        ans += a[(start + i + 1) % n] - a[(start + i) % n] -print(ans) +r = [(a[0] - a[-1]) % k] +l = [(a[-1] - a[0]) % k] +for i in range(1, n): +    r.append(a[i] - a[i - 1]) +    l.append(abs(a[-i - 1] - a[-i])) +sum_l = sum(l) +sum_r = sum(r) +print((min(sum_l - max(l), sum_r - max(r))))