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

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

A.sort()

A+=[A[0]+K]

minn=K+1

for i in range(N):

    minn=min(minn,K-(A[i+1]-A[i]))

print(minn)
#



import sys

input=sys.stdin.readline



def main():

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

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

    mind=K-(A[0]-A[-1]+K)

    for i in range(1,N):

        mind=min(mind,K-(A[i]-A[i-1]))

    print(mind)

    

    

if __name__=="__main__":

    main()

K, N = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
A += [A[0] + K]
minn = K + 1
for i in range(N):
    minn = min(minn, K - (A[i + 1] - A[i]))
print(minn)

#
import sys

input = sys.stdin.readline


def main():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    mind = K - (A[0] - A[-1] + K)
    for i in range(1, N):
        mind = min(mind, K - (A[i] - A[i - 1]))
    print(mind)


if __name__ == "__main__":
    main()

-K, N = list(map(int, input().split())) -A = list(map(int, input().split())) -A.sort() -A += [A[0] + K] -minn = K + 1 -for i in range(N): -    minn = min(minn, K - (A[i + 1] - A[i])) -print(minn) +# +import sys + +input = sys.stdin.readline + + +def main(): +    K, N = list(map(int, input().split())) +    A = list(map(int, input().split())) +    mind = K - (A[0] - A[-1] + K) +    for i in range(1, N): +        mind = min(mind, K - (A[i] - A[i - 1])) +    print(mind) + + +if __name__ == "__main__": +    main()