CF BUDDY
← Problems·

1131B · Draw!

1400 · greedy, implementation

Problem: You still have partial information about the score during the historic football match. You are given a set of pairs (ai,bi)(a_i, b_i), indicating that at some point during the match the score was "aia_i: bib_i". It is known that if the current score is «xx:yy», then after the goal it will change to "x+1x+1:yy" or "xx:y+1y+1". What is the largest number of times a draw could appear on the scoreboard?

The pairs "aia_i:bib_i" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.

Input Format: The first line contains a single integer nn (1n100001 \le n \le 10000) — the number of known moments in the match.

Each of the next nn lines contains integers aia_i and bib_i (0ai,bi1090 \le a_i, b_i \le 10^9), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).

All moments are given in chronological order, that is, sequences xix_i and yjy_j are non-decreasing. The last score denotes the final result of the match.

Output Format: Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.

Note: In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.

Sample Cases

Case 1

Input

3
2 0
3 1
3 4

Output

2

Case 2

Input

3
0 0
0 0
0 0

Output

1

Case 3

Input

1
5 4

Output

5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback