program main
    implicit none
    integer k,n
    integer, allocatable, dimension(:) :: a
    integer min,d,i

    read(*,*) k,n
    allocate(a(n))
    read(*,*) a
 
    min = a(n) - a(1)
    
    do i = 2, n
        d = k - (a(i) - a(i-1))
        if (d <= min) then
            min = d
        end if
    end do

    write(*,*) min

end program