def main():

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



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



        dist = []



        for i in range(N):

                if i!=N-1:

                        dist.append(A[i+1]-A[i])

                else:

                        dist.append(K-A[i]+A[0])



        ans = sum(dist)-max(dist)



        print(("{}".format(ans)))



if __name__=="__main__":

        main()
def main():

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

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



        dist = []



        for i in range(N-1):

                dist.append(A[i+1]-A[i])

        dist.append(K-A[N-1]+A[0])



        ans = sum(dist) - max(dist)



        print(("{}".format(ans)))



if __name__=="__main__":

        main()
def main():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    dist = []
    for i in range(N):
        if i != N - 1:
            dist.append(A[i + 1] - A[i])
        else:
            dist.append(K - A[i] + A[0])
    ans = sum(dist) - max(dist)
    print(("{}".format(ans)))


if __name__ == "__main__":
    main()

def main():
    K, N = list(map(int, input().split()))
    A = list(map(int, input().split()))
    dist = []
    for i in range(N - 1):
        dist.append(A[i + 1] - A[i])
    dist.append(K - A[N - 1] + A[0])
    ans = sum(dist) - max(dist)
    print(("{}".format(ans)))


if __name__ == "__main__":
    main()

-    for i in range(N): -        if i != N - 1: -            dist.append(A[i + 1] - A[i]) -        else: -            dist.append(K - A[i] + A[0]) +    for i in range(N - 1): +        dist.append(A[i + 1] - A[i]) +    dist.append(K - A[N - 1] + A[0])