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

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



diff = 0

total = 0

for i in range(n-1):

    diff = max(abs(a[i] - a[i+1]), diff)

    total += abs(a[i] - a[i+1])

    

diff_tmp = abs(a[n-1] - k) + a[0]

diff = max(diff_tmp, diff)

total += abs(a[n-1] - k) + a[0] - diff



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

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



bef = a[0]

max_diff = 0

for i in range(1,n):

    diff = abs(bef - a[i])

    max_diff = max(diff, max_diff)

    bef = a[i]



print((k - max(max_diff, k - abs(a[n-1] - a[0]))))
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
diff = 0
total = 0
for i in range(n - 1):
    diff = max(abs(a[i] - a[i + 1]), diff)
    total += abs(a[i] - a[i + 1])
diff_tmp = abs(a[n - 1] - k) + a[0]
diff = max(diff_tmp, diff)
total += abs(a[n - 1] - k) + a[0] - diff
print(total)

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
bef = a[0]
max_diff = 0
for i in range(1, n):
    diff = abs(bef - a[i])
    max_diff = max(diff, max_diff)
    bef = a[i]
print((k - max(max_diff, k - abs(a[n - 1] - a[0]))))

-diff = 0 -total = 0 -for i in range(n - 1): -    diff = max(abs(a[i] - a[i + 1]), diff) -    total += abs(a[i] - a[i + 1]) -diff_tmp = abs(a[n - 1] - k) + a[0] -diff = max(diff_tmp, diff) -total += abs(a[n - 1] - k) + a[0] - diff -print(total) +bef = a[0] +max_diff = 0 +for i in range(1, n): +    diff = abs(bef - a[i]) +    max_diff = max(diff, max_diff) +    bef = a[i] +print((k - max(max_diff, k - abs(a[n - 1] - a[0]))))