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

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

ans = a[-1] - a[0]

for i in range(1, n):

    start = a[i]

    end = a[i-1]+k

    ans = min(ans, end-start)

print(ans)
def main():

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

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

    cost = 10**10

    for i in range(n-1):

        cost = min(cost, k - abs(a[i]-a[i+1]))

    print((min(cost, k-abs(a[0]+k-a[n-1]))))



if __name__ == "__main__":

    main()
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = a[-1] - a[0]
for i in range(1, n):
    start = a[i]
    end = a[i - 1] + k
    ans = min(ans, end - start)
print(ans)

def main():
    k, n = list(map(int, input().split()))
    a = list(map(int, input().split()))
    cost = 10**10
    for i in range(n - 1):
        cost = min(cost, k - abs(a[i] - a[i + 1]))
    print((min(cost, k - abs(a[0] + k - a[n - 1]))))


if __name__ == "__main__":
    main()

-k, n = list(map(int, input().split())) -a = list(map(int, input().split())) -ans = a[-1] - a[0] -for i in range(1, n): -    start = a[i] -    end = a[i - 1] + k -    ans = min(ans, end - start) -print(ans) +def main(): +    k, n = list(map(int, input().split())) +    a = list(map(int, input().split())) +    cost = 10**10 +    for i in range(n - 1): +        cost = min(cost, k - abs(a[i] - a[i + 1])) +    print((min(cost, k - abs(a[0] + k - a[n - 1])))) + + +if __name__ == "__main__": +    main()