Lecture Halls Reservation

No attempts yetTime limit1sMemory limit256 MB

Problem

We have a single lecture hall available. Each lecturer submits an order specifying the time their lecture starts and the time it ends. We want to use the hall as much as possible. We accept some orders and reject others, aiming to maximize the total length of the lectures held in the hall. We assume that the moment one lecture ends, another may begin right away (each lecture occupies the hall only during an open time interval).

Write a program that:

  • reads the lecturers' orders from standard input,
  • computes the maximum possible total lecture time,
  • writes the result to standard output.

Input

The first line contains one positive integer nn (n10000n \le 10000), the number of orders.

Each of the next nn lines contains two integers pp and kk separated by a single space (0p<k300000 \le p < k \le 30000). Such a pair represents one order: the lecture starts at time pp and ends at time kk (the lecturer uses the hall during the open interval (p,k)(p, k)).

Output

Print a single line containing the maximum possible total lecture time.