Both Sides

View as PDF

Submit solution

Points: 100
Time limit: 2.0s
Memory limit: 1G

Author:
Problem type

Natasha 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 1 and 2, and both start with zero points. During the match, there are N scoring events. In event i, team T_i earns P_i 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 N (1 \le N \le 1000), the number of scoring events.

Each of the next N lines contains two integers T_i and P_i (T_i \in \{1, 2\}, P_i \in \{1, 2, 3\}), the team that scores and the number of points it earns in event i. 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 1, 4, 5, 7, and 8. Event 2 only extends a lead, while events 3 and 6 bring the scores level.

Example 2

Input
1
2 3
Output
1
Explanation

Both teams start tied at zero. Team 2 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 1 and 3. Team 1 takes the lead twice, even though team 2 never leads. The match ends in a tie.


Comments

There are no comments at the moment.