Under construction

Still being built. Things may change

Drawing Lines

Interview

Time limit1sMemory limit256 MB

Summary
Given N segments on a number line, find the total length of their union, counting overlaps once and printing it as an integer.
Level

Medium4 of 10

Topics
Sorting, Intervals, Greedy, Array
Solved
No attempts yet

Problem

Junyong's nephew Junseop was drawing several line segments parallel to one straight line with a crayon.

Watching Junseop, Junyong became curious about the total length of the segments after projecting every line Junseop drew onto the number line.

You, who need to stay on Junyong's good side, must write a program that answers Junyong's question.

Input

The first line gives NN, the number of lines Junseop drew.

Lines 2 through N+1N+1 give the start coordinate XiX_i and the end coordinate YiY_i of each line Junseop drew, in that order. XiX_i and YiY_i are integers separated by a space.

NN ranges from 1 to 10,000. The start and end coordinates of a line are natural numbers from 1 to 10,000.

Output

Print the total length of the lines projected onto the number line as an integer.

Hint

Sample explanation

  • Orange lines: the lines Junseop drew
  • Green lines: the lines projected onto the number line

Examples1

  1. Example 1

    Input
    4
    1 3
    2 5
    4 9
    11 13
    
    Expected output
    10