program ABC160C
    implicit none
    integer(8)::K,N,i,ans
    integer(8),allocatable,dimension(:)::A,diff,dsum
    read*,K,N
    allocate(A(N))
    allocate(diff(N))
    allocate(dsum(N))
    read*,A
    ans=99999999

    do i=1,N
        if(i==N)then
            diff(i)=A(1)+K-A(N)
        else
            diff(i)=A(i+1)-A(i)
        end if
    end do

    ans=sum(diff)-maxval(diff)

    print'(i0)',ans
end program ABC160C