program main
 implicit none
 integer K,N
 integer,allocatable :: A(:)
 integer :: minA=1000000
 integer :: rouA=0
 integer i

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

 read *, A

 rouA=A(N)-A(1)
 if ( rouA < minA ) minA=rouA

 do i=2,N
  rouA=A(i-1)+K-A(i)
  if ( rouA < minA ) minA=rouA
 end do

 print '(i0)', minA

endprogram main
