program main
      implicit none
      integer(4) :: k, n, i, dis
      integer(4), allocatable :: a(:), diff(:)

      read(*,*) k, n
      allocate( a(n) )
      read(*,*) a
      allocate( diff(n) )

      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

      dis = k - maxval( diff )
      print *, dis 

end program main
