program atcoder0328_3
  integer, dimension(:), allocatable:: A,D
  read(5,*)K,N
  allocate(A(N),D(N))
  read(5,*)A
  do I=1,N-1
     D(I)=A(I+1)-A(I)
  end do
  D(N)=K+A(1)-A(N)
  L=maxval(D)
  write(6,*)int(K-L)
end program atcoder0328_3