   program abc160c2
      implicit none
      integer::K,N,i,maxA,x,y,index,Cmin=0
      integer,allocatable,dimension(:)::A

        read *,K,N
        allocate(A(N))
        read *,A

        maxA=0
        do i=1,N-1
           x=A(i+1)-A(i)
           if (maxA<x) then
              maxA=x
              index=i
           end if
        end do
        y=K-A(N)+A(1)
        if (maxA<y) then
           maxA=y
           index=0
        end if

        do i=1,N-1
           if (i==index) cycle
           Cmin=Cmin+(A(i+1)-A(i))
        end do

        if (index/=0) then
           Cmin=Cmin+(K-A(N)+A(1))
        end if

        print *,Cmin
        stop

   end program abc160c2