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

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

ans = k

b = a[0] + (k-a[-1])



prev = a[0]



for i, v in enumerate(a):

    if i == 0:

        ans = min(ans, k-b)

    ans = min(ans, k-(v-prev))

    prev = v

    

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

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

ans = k

b = a[0] + (k-a[-1])



prev = a[0]



for v in a[1:]:

    ans = min(ans, k-(v-prev))

    prev = v

ans = min(ans, k-b)

    

print(ans)
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = k
b = a[0] + (k - a[-1])
prev = a[0]
for i, v in enumerate(a):
    if i == 0:
        ans = min(ans, k - b)
    ans = min(ans, k - (v - prev))
    prev = v
print(ans)

k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = k
b = a[0] + (k - a[-1])
prev = a[0]
for v in a[1:]:
    ans = min(ans, k - (v - prev))
    prev = v
ans = min(ans, k - b)
print(ans)

-for i, v in enumerate(a): -    if i == 0: -        ans = min(ans, k - b) +for v in a[1:]: +ans = min(ans, k - b)