program name
    implicit none
    integer(4):: k,n,i
    integer(4),allocatable:: a(:), dist(:)

    read*, k, n
    allocate(a(n), dist(n))
    read*, a(:)

    dist(1) = a(1) + k-a(n)

    do i=2, n
        dist(i) = a(i) - a(i-1)
    end do

    print*, sum(dist) - maxval(dist)
end program name