program main
integer :: k,n,ans
integer,allocatable :: a(:),dis(:)
read(*,*)k,n
allocate(a(n),dis(n))
read(*,*)a(1:n)
dis(n) = k + a(1) - a(n)
do i = 1,n-1
dis(i) = a(i+1) - a(i)
end do
ans = sum(dis) - maxval(dis)
write(*,*)ans
end program main