program main
  implicit none
  integer :: k, n, res
  integer, allocatable :: a(:)
  read (*, *) k, n
  allocate (a(0:n))
  read (*, *) a(1:n)
  a(0) = a(n) - k
  res = k - maxval(a(1:n) - a(0:n - 1))
  write (*, "(i0)") res
end program main
