from itertools import chain

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

S = 0

M = 0



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



prev = next(A)

for a in chain(A, [prev+K]):

  d = a - prev

  S += d

  M = max(M, d)

  prev = a



print((S-M))
from itertools import *

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

A, B = tee(list(map(int, input().split())), 2)



first = next(B)

M = max(b-a for a, b in zip_longest(A, B, fillvalue=first+K))



print((K-M))

from itertools import chain

K, N = list(map(int, input().split()))
S = 0
M = 0
A = list(map(int, input().split()))
prev = next(A)
for a in chain(A, [prev + K]):
    d = a - prev
    S += d
    M = max(M, d)
    prev = a
print((S - M))

from itertools import *

K, N = list(map(int, input().split()))
A, B = tee(list(map(int, input().split())), 2)
first = next(B)
M = max(b - a for a, b in zip_longest(A, B, fillvalue=first + K))
print((K - M))

-from itertools import chain +from itertools import * -S = 0 -M = 0 -A = list(map(int, input().split())) -prev = next(A) -for a in chain(A, [prev + K]): -    d = a - prev -    S += d -    M = max(M, d) -    prev = a -print((S - M)) +A, B = tee(list(map(int, input().split())), 2) +first = next(B) +M = max(b - a for a, b in zip_longest(A, B, fillvalue=first + K)) +print((K - M))