program tozan_and_gezan
  implicit none
  integer :: n, i
  integer(8) :: a, b, m, s
  logical :: same
  m = 10000000000000_8
  s = 0_8
  same = .true.
  read(*,*) n
  do i = 1, n
    read(*,*) a, b
    if (a.ne.b) same = .false.
    if (a.gt.b.and.m.gt.b) m = b
    s = s+a
  end do
  if (same) then
    write(*,'(i0)') 0
  else
    write(*,'(i0)') s-m
  end if
  stop
end program tozan_and_gezan