import sys

input = sys.stdin.readline



def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))



def main():

    mod=10**9+7

    K,N=MI()

    A=LI()

    diff=[0]*N

    for i in range(N-1):

        diff[i]=A[i+1]-A[i]

        

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

    print((K-max(diff)))



main()

import sys

input = sys.stdin.readline



def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))



def main():

    mod=10**9+7

    K,N=MI()

    A=LI()

    S=sum(A)

    L=[]

    for i in range(N-1):

        temp=A[i+1]-A[i]

        L.append(temp)

        

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

    M=max(L)

    

    print((K-M))



main()

import sys

input = sys.stdin.readline


def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def main():
    mod = 10**9 + 7
    K, N = MI()
    A = LI()
    diff = [0] * N
    for i in range(N - 1):
        diff[i] = A[i + 1] - A[i]
    diff[-1] = A[0] + (K - A[-1])
    print((K - max(diff)))


main()

import sys

input = sys.stdin.readline


def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def main():
    mod = 10**9 + 7
    K, N = MI()
    A = LI()
    S = sum(A)
    L = []
    for i in range(N - 1):
        temp = A[i + 1] - A[i]
        L.append(temp)
    L.append(A[0] + K - A[-1])
    M = max(L)
    print((K - M))


main()

-    diff = [0] * N +    S = sum(A) +    L = [] -        diff[i] = A[i + 1] - A[i] -    diff[-1] = A[0] + (K - A[-1]) -    print((K - max(diff))) +        temp = A[i + 1] - A[i] +        L.append(temp) +    L.append(A[0] + K - A[-1]) +    M = max(L) +    print((K - M))