program traveling_salesman_around_lake
  implicit none
  integer :: k, n, a(200000) = 0
  read(*,*) k, n
  read(*,*) a(1:n)
  write(*,'(i0)') k-max(a(1)+k-a(n),maxval(a(2:n)-a(1:n-1)))
end program traveling_salesman_around_lake