cho.sh
Notes
Loading...

Drawing Lines

Time limit

1s

Memory limit

192 MB

Problem

You draw several line segments on a very large sheet of paper. Each segment is drawn from one chosen point to another chosen point on a ruler.

A segment may overlap a part that has already been drawn. However, after drawing, there is no distinction between a part drawn once and a part drawn multiple times.

Compute the total length of the parts that are covered by at least one drawn segment. Overlapping parts are counted only once.

Input

The first line contains the number of drawn segments N (1 ≤ N ≤ 1,000,000).

Each of the next N lines contains two integers x and y (-1,000,000,000 ≤ x < y ≤ 1,000,000,000), the endpoints of one drawn segment.

Output

Print the total drawn length, counting overlapping parts only once.