Both Sides
View as PDFNatasha has arrived at the basketball match just in time to discover that she does not know which team her friends are supporting. Too embarrassed to ask, she decides to cheer whenever either team takes the lead. Surely nobody will notice if she sounds enthusiastic enough.
There are two teams, numbered and
, and both start with zero points. During the match,
there are
scoring events. In event
, team
earns
points, which are added to
its score. The events occur in the order given.
Natasha cheers once after a scoring event if the team that scored was tied with or behind the other team immediately before the event, and is strictly ahead immediately afterwards. She does not cheer when a team merely extends its lead or brings the scores level.
How many times does Natasha cheer during the match?
Input
The first line contains an integer (
), the number of scoring events.
Each of the next lines contains two integers
and
(
,
), the team that scores and the number of points it earns in event
.
The events are listed in chronological order.
Output
Print a single integer: the number of times Natasha cheers.
Example 1
Input
8
1 2
1 1
2 3
2 1
1 2
2 1
1 1
2 3
Output
5
Explanation
Natasha cheers after events ,
,
,
, and
. Event
only extends a
lead, while events
and
bring the scores level.
Example 2
Input
1
2 3
Output
1
Explanation
Both teams start tied at zero. Team takes the lead with the only scoring event.
Example 3
Input
4
1 1
2 1
1 1
2 1
Output
2
Explanation
Natasha cheers after events and
. Team
takes the lead twice, even though
team
never leads. The match ends in a tie.
Comments