program abc160c
   implicit none
   integer :: K, N, i
   integer, allocatable :: A(:)
   integer, allocatable :: interval(:)
   integer :: way_min, pass, dont_pass

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

   ! write(*,*) K, N
   ! write(*,*) A(:)
   way_min = K
   i = 1
   dont_pass = A(i) + (K-A(N))
   pass = K - dont_pass
   way_min = min(way_min, pass)

   do i = 2, N
      dont_pass = A(i) - A(i-1)
      pass = K -  dont_pass
      way_min = min(way_min, pass)
   end do

   write(*,*) way_min

end program abc160c
